Skip to content

Commit

Permalink
Add another test for selector
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioh8010 committed Mar 7, 2024
1 parent 4927f38 commit 96da9f0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/unit/useOnyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,29 @@ describe('useOnyx', () => {
// must be the same reference
expect(oldResult).toBe(result.current);
});

it('should always use the current selector reference to return new data', async () => {
Onyx.set(ONYXKEYS.TEST_KEY, {id: 'test_id', name: 'test_name'});

let selector = (entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name}`;

const {result} = renderHook(() =>
useOnyx(ONYXKEYS.TEST_KEY, {
// @ts-expect-error bypass
selector,
}),
);

expect(result.current[0]).toEqual('id - test_id, name - test_name');
expect(result.current[1].status).toEqual('loaded');

selector = (entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name} - selector changed`;

await act(async () => Onyx.merge(ONYXKEYS.TEST_KEY, {id: 'changed_id', name: 'changed_name'}));

expect(result.current[0]).toEqual('id - changed_id, name - changed_name - selector changed');
expect(result.current[1].status).toEqual('loaded');
});
});

describe('initialValue', () => {
Expand Down

0 comments on commit 96da9f0

Please sign in to comment.