-
Notifications
You must be signed in to change notification settings - Fork 93
Fix/Ensure the new selector execution #735
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
Changes from all commits
415b4d4
75f9bd1
b9e24a2
cad13f2
b6319e0
11c8b3a
3ac96c9
b3d4b3b
60d42ea
7b81568
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -426,7 +426,10 @@ describe('useOnyx', () => { | |||
| 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}`) as UseOnyxSelector<OnyxKey, string>; | ||||
| let selector: UseOnyxSelector<OnyxKey, string> = ((entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name}`) as UseOnyxSelector< | ||||
| OnyxKey, | ||||
| string | ||||
| >; | ||||
|
|
||||
| const {result, rerender} = renderHook(() => | ||||
| useOnyx(ONYXKEYS.TEST_KEY, { | ||||
|
|
@@ -437,10 +440,22 @@ describe('useOnyx', () => { | |||
| 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`) as UseOnyxSelector<OnyxKey, string>; | ||||
| selector = ((entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name} - selector changed synchronously`) as UseOnyxSelector<OnyxKey, string>; | ||||
|
|
||||
| rerender(undefined); | ||||
|
|
||||
| expect(result.current[0]).toEqual('id - test_id, name - test_name - selector changed'); | ||||
| expect(result.current[0]).toEqual('id - test_id, name - test_name - selector changed synchronously'); | ||||
| expect(result.current[1].status).toEqual('loaded'); | ||||
|
|
||||
| selector = ((entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name} - selector changed after macrotask`) as UseOnyxSelector<OnyxKey, string>; | ||||
|
|
||||
| await act(async () => { | ||||
| // This is necessary to avoid regressions of the selector interleaving bug, see https://github.com/Expensify/App/issues/79449 | ||||
| await waitForPromisesToResolve(); | ||||
|
Contributor
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.
Suggested change
This is not necessary
Contributor
Author
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. I disagree, we should still test it after a macrotask to avoid possible regressions in the future. We can get rid of it if the whole flow will be synchronous. This bug is hard to notice without an automated test.
Contributor
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. Could we leave a small comment about it then?
Contributor
Author
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. @fabioh8010 done ;) |
||||
| rerender(undefined); | ||||
| }); | ||||
|
|
||||
| expect(result.current[0]).toEqual('id - test_id, name - test_name - selector changed after macrotask'); | ||||
| expect(result.current[1].status).toEqual('loaded'); | ||||
| }); | ||||
|
|
||||
|
|
@@ -681,15 +696,16 @@ describe('useOnyx', () => { | |||
|
|
||||
| const dependencies = ['constant']; | ||||
| let selectorCallCount = 0; | ||||
| const selector = ((data) => { | ||||
| selectorCallCount++; | ||||
| return `${dependencies.join(',')}:${(data as {value?: string})?.value}`; | ||||
| }) as UseOnyxSelector<OnyxKey, string>; | ||||
|
|
||||
| const {result, rerender} = renderHook(() => | ||||
| useOnyx( | ||||
| ONYXKEYS.TEST_KEY, | ||||
| { | ||||
| selector: (data) => { | ||||
| selectorCallCount++; | ||||
| return `${dependencies.join(',')}:${(data as {value?: string})?.value}`; | ||||
| }, | ||||
| selector, | ||||
| }, | ||||
| dependencies, | ||||
| ), | ||||
|
|
||||
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.
Could you think of a way to add a failing test for this first? seems like we had this bug in place because this scenario was not covered by unit tests. For onyx I believe we should try to always create the failing test first and then work on the fix cc @fabioh8010
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.
You can see that there is an update in unit tests within this PR. Adding
act()makes its behavior closer to how React works in the browser, like in few other tests here, breaking the scenario (while it shouldn't). The test in its previous form was too loose.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.
Yeah, I saw that change, but it was not clear to me if that is all that we could / should cover here. Could you create a specific test for this behaviour that was broken?
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.
Yup, I'll do it.
Uh oh!
There was an error while loading. Please reload this page.
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.
@mountiny I've updated the tests and also dug deeper into the root cause.
On the main branch
rerender()is called synchronously, so updates scheduled asynchonously withprepareSubscriberUpdate()don't have time to run. As a result, the store subscriber callbackdoes not get a chance to toggle the
isFirstConnectionandshouldGetCachedValueflags, which allows the new callback to execute.On the other hand, if we call the next
useOnyxin an asynchronous workflow, those flags are toggled tofalse, effectively preventing the new selector from executing.The updated unit test ensures that replacing the selector works predictably in both scenarios.
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.
Nice thanks for digging into that