-
-
Notifications
You must be signed in to change notification settings - Fork 436
[utils] Import Store changes from x-internals
#4765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
romgrk
wants to merge
6
commits into
mui:master
Choose a base branch
from
romgrk:refactor-store-merge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
909714c
refactor: import x-internals changes
romgrk d94f27b
lint
romgrk 195b8a7
lint
romgrk 704c96b
Merge branch 'master' into refactor-store-merge
romgrk 0f635f9
refactor: address comments
romgrk acd0f0c
Merge branch 'refactor-store-merge' of github.com:romgrk/base-ui into…
romgrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import { expect, vi } from 'vitest'; | ||
| import { lruMemoize } from 'reselect'; | ||
| import { Store } from './Store'; | ||
| import { createSelector } from './createSelector'; | ||
| import { createSelectorMemoized, createSelectorMemoizedWithOptions } from './createSelectorMemoized'; | ||
|
|
||
| describe('Store', () => { | ||
| describe('Store.create', () => { | ||
| it('returns a Store instance seeded with the given state', () => { | ||
| const store = Store.create({ value: 1, label: 'a' }); | ||
|
|
||
| expect(store).toBeInstanceOf(Store); | ||
| expect(store.state).toEqual({ value: 1, label: 'a' }); | ||
| }); | ||
|
|
||
| it('produces an independent instance per call', () => { | ||
| const first = Store.create({ value: 0 }); | ||
| const second = Store.create({ value: 0 }); | ||
|
|
||
| first.set('value', 1); | ||
|
|
||
| expect(first.state.value).toBe(1); | ||
| expect(second.state.value).toBe(0); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createSelector', () => { | ||
| it('returns the input function when called with a single selector', () => { | ||
| const fn = (state: { value: number }) => state.value; | ||
| const selector = createSelector(fn); | ||
|
|
||
| expect(selector).toBe(fn); | ||
| expect(selector({ value: 5 })).toBe(5); | ||
| }); | ||
|
|
||
| it('supports six selectors plus a combiner', () => { | ||
| type S = { a: number; b: number; c: number; d: number; e: number; f: number }; | ||
| const state: S = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }; | ||
|
|
||
| const selector = createSelector( | ||
| (s: S) => s.a, | ||
| (s: S) => s.b, | ||
| (s: S) => s.c, | ||
| (s: S) => s.d, | ||
| (s: S) => s.e, | ||
| (s: S) => s.f, | ||
| (a, b, c, d, e, f) => a + b + c + d + e + f, | ||
| ); | ||
|
|
||
| expect(selector(state)).toBe(21); | ||
| }); | ||
|
|
||
| it('supports seven selectors plus a combiner', () => { | ||
| type S = { a: number; b: number; c: number; d: number; e: number; f: number; g: number }; | ||
| const state: S = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7 }; | ||
|
|
||
| const selector = createSelector( | ||
| (s: S) => s.a, | ||
| (s: S) => s.b, | ||
| (s: S) => s.c, | ||
| (s: S) => s.d, | ||
| (s: S) => s.e, | ||
| (s: S) => s.f, | ||
| (s: S) => s.g, | ||
| (a, b, c, d, e, f, g) => a + b + c + d + e + f + g, | ||
| ); | ||
|
|
||
| expect(selector(state)).toBe(28); | ||
| }); | ||
|
|
||
| it('passes extra args through to every input selector and to the combiner', () => { | ||
| type S = { value: number }; | ||
| const state: S = { value: 10 }; | ||
|
|
||
| const selector = createSelector( | ||
| (s: S, multiplier: number) => s.value * multiplier, | ||
| (s: S, _multiplier: number, offset: number) => s.value + offset, | ||
| (scaled, shifted, multiplier, offset) => ({ scaled, shifted, multiplier, offset }), | ||
| ); | ||
|
|
||
| expect(selector(state, 3, 7)).toEqual({ scaled: 30, shifted: 17, multiplier: 3, offset: 7 }); | ||
| }); | ||
|
|
||
| it('throws when given more selectors than are supported', () => { | ||
| const fn = (s: any) => s; | ||
|
|
||
| expect(() => | ||
| // @ts-expect-error intentionally over the supported arity | ||
| createSelector(fn, fn, fn, fn, fn, fn, fn, fn, fn), | ||
| ).toThrow('Unsupported number of selectors'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createSelectorMemoized', () => { | ||
| it('uses an identity input selector when only a combiner is provided', () => { | ||
| type S = { value: number }; | ||
| const combiner = vi.fn((s: S) => ({ doubled: s.value * 2 })); | ||
|
|
||
| const selector = createSelectorMemoized(combiner); | ||
|
|
||
| const state: S = { value: 4 }; | ||
| expect(selector(state)).toEqual({ doubled: 8 }); | ||
| expect(combiner).toHaveBeenCalledTimes(1); | ||
|
|
||
| expect(selector(state)).toEqual({ doubled: 8 }); | ||
| expect(combiner).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('re-runs the combiner when input selector results change', () => { | ||
| type S = { a: number; b: number }; | ||
| const combiner = vi.fn((a: number, b: number) => ({ sum: a + b })); | ||
|
|
||
| const selector = createSelectorMemoized( | ||
| (state: S) => state.a, | ||
| (state: S) => state.b, | ||
| combiner, | ||
| ); | ||
|
|
||
| const state: S = { a: 1, b: 2 }; | ||
| expect(selector(state)).toEqual({ sum: 3 }); | ||
| expect(combiner).toHaveBeenCalledTimes(1); | ||
|
|
||
| expect(selector(state)).toEqual({ sum: 3 }); | ||
| expect(combiner).toHaveBeenCalledTimes(1); | ||
|
|
||
| const next: S = { a: 5, b: 2 }; | ||
| expect(selector(next)).toEqual({ sum: 7 }); | ||
| expect(combiner).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('caches separately per state identity', () => { | ||
| type S = { value: number }; | ||
| const combiner = vi.fn((value: number) => ({ value })); | ||
|
|
||
| const selector = createSelectorMemoized((state: S) => state.value, combiner); | ||
|
|
||
| const a: S = { value: 1 }; | ||
| const b: S = { value: 1 }; | ||
|
|
||
| selector(a); | ||
| selector(b); | ||
|
|
||
| expect(combiner).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createSelectorMemoizedWithOptions', () => { | ||
| it('produces a working factory when no options are provided', () => { | ||
| type S = { value: number }; | ||
| const combiner = vi.fn((value: number) => ({ value })); | ||
|
|
||
| const selector = createSelectorMemoizedWithOptions()((state: S) => state.value, combiner); | ||
|
|
||
| const state: S = { value: 3 }; | ||
| expect(selector(state)).toEqual({ value: 3 }); | ||
| expect(selector(state)).toEqual({ value: 3 }); | ||
| expect(combiner).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('forwards options through to the underlying reselect creator', () => { | ||
| type S = { value: number }; | ||
| const inputSelector = vi.fn((state: S) => state.value); | ||
| const combiner = vi.fn((value: number) => ({ value })); | ||
|
|
||
| const selector = createSelectorMemoizedWithOptions({ | ||
| argsMemoize: lruMemoize, | ||
| argsMemoizeOptions: { equalityCheck: () => false, maxSize: 1 }, | ||
| devModeChecks: { inputStabilityCheck: 'never', identityFunctionCheck: 'never' }, | ||
| })(inputSelector, combiner); | ||
|
|
||
| const state: S = { value: 7 }; | ||
| selector(state); | ||
| const callsAfterFirst = inputSelector.mock.calls.length; | ||
| selector(state); | ||
|
|
||
| // The custom argsMemoize never considers args equal, so input selectors | ||
| // re-run on every call (defaults would have produced a cache hit instead). | ||
| expect(inputSelector.mock.calls.length).toBeGreaterThan(callsAfterFirst); | ||
| // The combiner result is still memoized because the input value is unchanged. | ||
| expect(combiner).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSDoc is stale after the arity change |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New public store behavior is effectively untested.
The only changed test line widens an existing nested-store type. The PR adds/exports
useStoreEffect,Store.create,createSelectorMemoizedWithOptions, single-combiner handling increateSelectorMemoized, and expandedcreateSelectorarity, but none of those behaviors have focused regression tests.