Skip to content

Commit 0deb176

Browse files
DyBevcode-forger
authored andcommitted
fix(isLoading): added catch for post render edge case
* test(isLoading): added tests * chore(clean-up): removed numOfRenders from isLoading function * chore(sync): added code to sync changes with pr#98 * feat(forceInitialFetch): added forceInitialFetch functionality * refactor(pr-comments): acconting for regression * test(queryHelpers): added extra tests and edge cases * chore(clean-up): updated tests to be grouped together
1 parent 529105d commit 0deb176

File tree

3 files changed

+127
-44
lines changed

3 files changed

+127
-44
lines changed

packages/fetchye/__tests__/queryHelpers.spec.js

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,118 @@ import {
1919
} from '../src/queryHelpers';
2020

2121
describe('isLoading', () => {
22-
it('should return true if loading cache true', () => {
23-
expect(isLoading({
24-
loading: true, data: undefined, numOfRenders: 2, options: {},
25-
})).toEqual(true);
26-
});
27-
it('should return true if first render is true', () => {
28-
expect(isLoading({
29-
loading: false, data: undefined, numOfRenders: 1, options: { },
30-
})).toEqual(true);
31-
});
32-
it('should return true if first render and forceInitialFetch option is true', () => {
33-
expect(isLoading({
34-
loading: false, data: undefined, numOfRenders: 1, options: { forceInitialFetch: true },
35-
})).toEqual(true);
36-
});
37-
it('should return false if first render is true and defer is true', () => {
38-
expect(isLoading({
39-
loading: false, data: undefined, numOfRenders: 1, options: { defer: true },
40-
})).toEqual(false);
22+
describe('should return false', () => {
23+
it('if defer is true, and not already loading', () => {
24+
expect(isLoading({
25+
loading: false,
26+
data: undefined,
27+
options: {
28+
defer: true,
29+
},
30+
error: undefined,
31+
refs: {},
32+
})).toEqual(false);
33+
});
34+
35+
it('if forceInitialFetch is true, but call is defered', () => {
36+
expect(isLoading({
37+
loading: false,
38+
data: {},
39+
options: {
40+
defer: true,
41+
},
42+
error: undefined,
43+
refs: {
44+
forceInitialFetch: true,
45+
},
46+
})).toEqual(false);
47+
});
48+
49+
it('if data is undefined and no error, but call is defered', () => {
50+
expect(isLoading({
51+
loading: false,
52+
data: undefined,
53+
options: {
54+
defer: true,
55+
},
56+
error: undefined,
57+
refs: {
58+
forceInitialFetch: false,
59+
},
60+
})).toEqual(false);
61+
});
62+
63+
it('if there are errors present', () => {
64+
expect(isLoading({
65+
loading: false,
66+
options: {
67+
defer: false,
68+
},
69+
error: {},
70+
refs: {},
71+
})).toEqual(false);
72+
});
73+
74+
it('if there is data present', () => {
75+
expect(isLoading({
76+
loading: false,
77+
data: { },
78+
options: {
79+
defer: false,
80+
},
81+
refs: {},
82+
})).toEqual(false);
83+
});
4184
});
42-
it('should return false if all args are false', () => {
43-
expect(isLoading({
44-
loading: false, numOfRenders: 2, options: { defer: false },
45-
})).toEqual(false);
85+
86+
describe('should return true', () => {
87+
it('if fetchye is loading', () => {
88+
expect(isLoading({
89+
loading: true,
90+
data: undefined,
91+
options: {},
92+
error: undefined,
93+
refs: {},
94+
})).toEqual(true);
95+
});
96+
97+
it('if fetchye is loading, even if defered is true', () => {
98+
expect(isLoading({
99+
loading: true,
100+
data: undefined,
101+
options: {
102+
defered: true,
103+
},
104+
error: undefined,
105+
refs: {},
106+
})).toEqual(true);
107+
});
108+
109+
it('if there is no data, no error and not defered', () => {
110+
expect(isLoading({
111+
data: undefined,
112+
loading: false,
113+
options: {
114+
defer: false,
115+
},
116+
error: undefined,
117+
refs: {},
118+
})).toEqual(true);
119+
});
120+
121+
it('if forceInitialFetch is true, even if data exists', () => {
122+
expect(isLoading({
123+
loading: false,
124+
data: {},
125+
options: {
126+
defer: false,
127+
},
128+
error: undefined,
129+
refs: {
130+
forceInitialFetch: true,
131+
},
132+
})).toEqual(true);
133+
});
46134
});
47135
});
48136

packages/fetchye/src/queryHelpers.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,23 @@
1515
*/
1616

1717
export const isLoading = ({
18-
loading, data, numOfRenders, options,
18+
loading, data, options, error, refs,
1919
}) => {
20-
// If first render
21-
if (numOfRenders === 1) {
22-
// and defer is true
23-
if (options.defer) {
24-
// isLoading should be false
25-
return false;
26-
}
27-
// and no data exists and presume loading state isn't present
28-
if (!data) {
29-
// isLoading should be true
30-
return true;
31-
}
32-
// when we force fetch isLoading is always going to be true on first render
33-
if (options.forceInitialFetch) {
34-
// isLoading should be true
35-
return true;
36-
}
37-
}
38-
// If not on first render and loading from cache is true
20+
// If loading from cache is true
3921
if (loading) {
4022
// isLoading should be true
4123
return true;
4224
}
25+
26+
// will fetch if no data and no error
27+
// will fetch if forceInitialFetch is true
28+
const willFetchIfNotDefered = (!data && !error)
29+
|| (refs.forceInitialFetch === true);
30+
31+
if (willFetchIfNotDefered && !options.defer) {
32+
return true;
33+
}
34+
4335
// else isLoading should be false
4436
return false;
4537
};

packages/fetchye/src/useFetchye.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ const useFetchye = (
8888
isLoading: isLoading({
8989
loading: selectorState.current.loading,
9090
data: selectorState.current.data || options.initialData?.data,
91-
numOfRenders: numOfRenders.current,
9291
options,
92+
error: selectorState.current.error,
93+
refs: {
94+
forceInitialFetch: forceInitialFetch.current,
95+
},
9396
}),
9497
error: passInitialData(
9598
{

0 commit comments

Comments
 (0)