Skip to content

Commit e6ed8ab

Browse files
committed
add test that fails
1 parent 640cceb commit e6ed8ab

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,46 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
624624
expect(container.textContent).toEqual('NaN');
625625
});
626626

627+
test('selector is not called when snaphot has not changed', async () => {
628+
const store = createExternalStore({a: 0});
629+
630+
function selector(state) {
631+
Scheduler.log('Selector');
632+
return state.a;
633+
}
634+
635+
function isEqual(a, b) {
636+
return a === b;
637+
}
638+
639+
function App() {
640+
Scheduler.log('App');
641+
const a = useSyncExternalStoreWithSelector(
642+
store.subscribe,
643+
store.getState,
644+
null,
645+
selector,
646+
isEqual,
647+
);
648+
return <Text text={'A' + a} />;
649+
}
650+
651+
const container = document.createElement('div');
652+
const root = createRoot(container);
653+
await act(() => root.render(<App />));
654+
655+
assertLog(['App', 'Selector', 'A0']);
656+
expect(container.textContent).toEqual('A0');
657+
658+
await act(() => store.set(store.getState()));
659+
await act(() => store.set({a: 0}));
660+
assertLog(['Selector']);
661+
662+
await act(() => store.set(store.getState()));
663+
await act(() => store.set(store.getState()));
664+
assertLog([]);
665+
});
666+
627667
describe('extra features implemented in user-space', () => {
628668
// The selector implementation uses the lazy ref initialization pattern
629669
// @gate !(enableUseRefAccessWarning && __DEV__)

0 commit comments

Comments
 (0)