Skip to content

Commit

Permalink
fix: useStore selector was not working on derived stores
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Dec 10, 2024
1 parent f1ae38c commit 300adfd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/react/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function useStore<T, S>(store: Store<T>, argument1?: any, argument2?: any
const selector = makeSelector<T, S>(
typeof argument1 === 'function' || typeof argument1 === 'string' ? argument1 : undefined,
);

const allOptions = (
typeof argument1 === 'object' ? argument1 : (argument2 ?? {})
) as UseStoreOptions<S>;
Expand All @@ -51,7 +52,7 @@ export function useStore<T, S>(store: Store<T>, argument1?: any, argument2?: any
for (const selector of store.derivedFrom!.selectors) {
value = makeSelector(selector)(value);
}
return value;
return selector(value);
},
allOptions,
);
Expand Down
30 changes: 30 additions & 0 deletions test/react/registerReactMethods.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ describe('register react methods', () => {
expect(div.textContent).toBe('0');
});

test('useStore with selector', async () => {
const store = createStore({ x: 0 });

const Component = vi.fn(function Component() {
const value = store.useStore((x) => x.x + 1);

return <div data-testid="div">{value}</div>;
});

render(<Component />);
const div = screen.getByTestId('div');

expect(div.textContent).toBe('1');
});

test('useStore with selector and mapping', async () => {
const store = createStore({ x: 0 });

const Component = vi.fn(function Component() {
const value = store.map('x').useStore((x) => x + 1);

return <div data-testid="div">{value}</div>;
});

render(<Component />);
const div = screen.getByTestId('div');

expect(div.textContent).toBe('1');
});

test('useProp', async () => {
const store = createStore({ x: 0 });

Expand Down
43 changes: 43 additions & 0 deletions test/react/useStore.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,49 @@ describe('useStore', () => {
expect(Component.mock.calls.length).toBe(2);
});

test('with selector as argument', async () => {
const store = createStore({ a: 1, b: 2 });

const Component = vi.fn(function Component() {
const value = useStore(store, (s) => s.a);

return <div data-testid="div">{value}</div>;
});

render(<Component />);
const div = screen.getByTestId('div');

act(() => {
store.set({ a: 2, b: 2 });
});

expect(div.textContent).toBe('2');
expect(Component.mock.calls.length).toBe(2);
});

test('with mapping and selector as argument', async () => {
const store = createStore({ a: 1, b: 2 });

const Component = vi.fn(function Component() {
const value = useStore(
store.map((s) => s.a),
(s) => s + 1,
);

return <div data-testid="div">{value}</div>;
});

render(<Component />);
const div = screen.getByTestId('div');

act(() => {
store.set({ a: 2, b: 2 });
});

expect(div.textContent).toBe('3');
expect(Component.mock.calls.length).toBe(2);
});

test('fall back to store equals', async () => {
const store = createStore({ a: 1 }, { equals: strictEqual });

Expand Down

0 comments on commit 300adfd

Please sign in to comment.