-
Notifications
You must be signed in to change notification settings - Fork 74
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
More minor fixes for the Onyx bump in E/App #561
Merged
mountiny
merged 12 commits into
Expensify:main
from
margelo:@chrispader/more-onyx-bump-fixes
Jun 11, 2024
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9324ca5
fix: initial loading state of useOnyx
chrispader 9cd73cf
fix: initial values and loading states
chrispader 43335ee
improve Onyx.clear, clearing values from cache
chrispader 7165f1c
update tests to reflect new Onyx cache behaviour
chrispader 1a983e5
undo unnecessary changes to one test
chrispader 0ca3476
fix: improve code
chrispader 5da5e50
rename test
chrispader a0991a9
fix: update useOnyx result when cache was set for the first time
chrispader 9c94b0c
remove unused check
chrispader ec1c9ba
add back getCachedValue function
chrispader 9134e7e
remove unnecessary check
chrispader cdbd255
fix: typo
chrispader 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 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 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 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 |
---|---|---|
|
@@ -97,7 +97,7 @@ describe('useOnyx', () => { | |
expect(result.current[1].status).toEqual('loaded'); | ||
}); | ||
|
||
it('should initially return null while loading non-cached key, and then return value and loaded state', async () => { | ||
it('should initially return `undefined` while loading non-cached key, and then return value and loaded state', async () => { | ||
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test'); | ||
|
||
const {result} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY)); | ||
|
@@ -244,8 +244,6 @@ describe('useOnyx', () => { | |
}); | ||
|
||
it('should return initial value if selected data is undefined', async () => { | ||
Onyx.set(ONYXKEYS.TEST_KEY, 'test_id_1'); | ||
|
||
const {result} = renderHook(() => | ||
useOnyx(ONYXKEYS.TEST_KEY, { | ||
// @ts-expect-error bypass | ||
|
@@ -257,6 +255,8 @@ describe('useOnyx', () => { | |
expect(result.current[0]).toEqual('initial value'); | ||
expect(result.current[1].status).toEqual('loaded'); | ||
|
||
Onyx.set(ONYXKEYS.TEST_KEY, 'test_id_1'); | ||
|
||
await act(async () => waitForPromisesToResolve()); | ||
|
||
expect(result.current[0]).toBeUndefined(); | ||
|
@@ -320,12 +320,6 @@ describe('useOnyx', () => { | |
}); | ||
|
||
it('should return initial value and loaded state while we have pending merges for the key, and then return updated value and loaded state', async () => { | ||
Onyx.set(ONYXKEYS.TEST_KEY, 'test1'); | ||
|
||
Onyx.merge(ONYXKEYS.TEST_KEY, 'test2'); | ||
Onyx.merge(ONYXKEYS.TEST_KEY, 'test3'); | ||
Onyx.merge(ONYXKEYS.TEST_KEY, 'test4'); | ||
|
||
const {result} = renderHook(() => | ||
useOnyx(ONYXKEYS.TEST_KEY, { | ||
initialValue: 'initial value', | ||
|
@@ -335,6 +329,12 @@ describe('useOnyx', () => { | |
expect(result.current[0]).toEqual('initial value'); | ||
expect(result.current[1].status).toEqual('loaded'); | ||
|
||
Onyx.set(ONYXKEYS.TEST_KEY, 'test1'); | ||
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. Same: Why did you move the operation to here? Shouldn't we have tests for both cases? 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. Same as above |
||
|
||
Onyx.merge(ONYXKEYS.TEST_KEY, 'test2'); | ||
Onyx.merge(ONYXKEYS.TEST_KEY, 'test3'); | ||
Onyx.merge(ONYXKEYS.TEST_KEY, 'test4'); | ||
|
||
await act(async () => waitForPromisesToResolve()); | ||
|
||
expect(result.current[0]).toEqual('test4'); | ||
|
@@ -361,7 +361,7 @@ describe('useOnyx', () => { | |
}); | ||
|
||
describe('initWithStoredValues', () => { | ||
it('should return null and loaded state, and after merge return updated value and loaded state', async () => { | ||
it('should return `undefined` and loaded state, and after merge return updated value and loaded state', async () => { | ||
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test1'); | ||
|
||
const {result} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY, {initWithStoredValues: false})); | ||
|
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.
Just curious: Why did you move the operation to here? Shouldn't we have tests for both cases?
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.
The moment we use
Onyx.set
orOnyx.merge
(or any setter function) it will store the value in cache synchronously and update thestorageKeys
Set inOnyxCache
. The storage will be updates asynchronously,If we set the key before asserting for the
initialValue
, we will always getundefined
, because there IS a value in cache already, even though the storage promise hasn't resolved yet.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.
Thanks for the explanation!