-
Notifications
You must be signed in to change notification settings - Fork 25
Fix/is loading/multi render behaviour weirdness #99
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
b29ae5f
b00d90d
6e80500
fd44eb6
bbfb724
15be263
286db6c
c75cceb
e45edff
cb0ffc1
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 |
|---|---|---|
|
|
@@ -19,29 +19,49 @@ import { | |
| } from '../src/queryHelpers'; | ||
|
|
||
| describe('isLoading', () => { | ||
| it('should return false defer is true', () => { | ||
| expect(isLoading({ | ||
| loading: false, data: undefined, options: { defer: true }, error: undefined, refs: {}, | ||
| })).toEqual(false); | ||
|
||
| }); | ||
|
|
||
| it('should return true if loading cache true', () => { | ||
| expect(isLoading({ | ||
| loading: true, data: undefined, numOfRenders: 2, options: {}, | ||
| loading: true, data: undefined, options: {}, error: undefined, refs: {}, | ||
| })).toEqual(true); | ||
| }); | ||
| it('should return true if first render is true', () => { | ||
|
|
||
| it('should return true if all args are false', () => { | ||
| expect(isLoading({ | ||
| loading: false, data: undefined, numOfRenders: 1, options: { }, | ||
| loading: false, options: { defer: false }, error: undefined, refs: {}, | ||
| })).toEqual(true); | ||
| }); | ||
| it('should return true if first render and forceInitialFetch option is true', () => { | ||
|
|
||
| it('should return true if refs.forceInitialFetch === true', () => { | ||
| expect(isLoading({ | ||
| loading: false, data: undefined, numOfRenders: 1, options: { forceInitialFetch: true }, | ||
| loading: false, | ||
| numOfRenders: 1, | ||
|
||
| data: {}, | ||
| options: { | ||
| defer: false, | ||
| forceInitialFetch: true, | ||
| }, | ||
| error: undefined, | ||
| refs: { | ||
| forceInitialFetch: true, | ||
| }, | ||
| })).toEqual(true); | ||
| }); | ||
| it('should return false if first render is true and defer is true', () => { | ||
|
|
||
| it('should return false if there are errors present', () => { | ||
| expect(isLoading({ | ||
| loading: false, data: undefined, numOfRenders: 1, options: { defer: true }, | ||
| loading: false, options: { defer: false }, error: { }, refs: {}, | ||
| })).toEqual(false); | ||
| }); | ||
| it('should return false if all args are false', () => { | ||
|
|
||
| it('should return false if there is data present', () => { | ||
| expect(isLoading({ | ||
| loading: false, numOfRenders: 2, options: { defer: false }, | ||
| loading: false, data: { }, numOfRenders: 2, options: { defer: false }, refs: {}, | ||
| })).toEqual(false); | ||
| }); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,31 +15,28 @@ | |
| */ | ||
|
|
||
| export const isLoading = ({ | ||
| loading, data, numOfRenders, options, | ||
| loading, data, options, error, refs, | ||
| }) => { | ||
| // If first render | ||
| if (numOfRenders === 1) { | ||
| // and defer is true | ||
| if (options.defer) { | ||
| // isLoading should be false | ||
| return false; | ||
| } | ||
| // and no data exists and presume loading state isn't present | ||
| if (!data) { | ||
| // isLoading should be true | ||
| return true; | ||
| } | ||
| // when we force fetch isLoading is always going to be true on first render | ||
| if (options.forceInitialFetch) { | ||
| // isLoading should be true | ||
| return true; | ||
| } | ||
| // if defer is true | ||
| if (options.defer) { | ||
| // isLoading should be false | ||
| return false; | ||
|
||
| } | ||
| // If not on first render and loading from cache is true | ||
|
|
||
| // If loading from cache is true | ||
| if (loading) { | ||
| // isLoading should be true | ||
| return true; | ||
| } | ||
|
|
||
| // Here to mimic what happens in the useEffect | ||
| if ( | ||
| (!data && !error) | ||
| || (refs.forceInitialFetch === true) | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| // else isLoading should be false | ||
| return false; | ||
| }; | ||
|
|
||
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.
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.
added in this commit e45edff