Skip to content

Commit

Permalink
fix(queryHelpers): fix ssr logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael A Tomcal committed Aug 29, 2020
1 parent c16229b commit b255fd5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
30 changes: 22 additions & 8 deletions __tests__/queryHelpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,48 @@ import {

describe('isLoading', () => {
it('should return true if loading cache true', () => {
expect(isLoading(true, {}, {})).toBeTruthy();
expect(isLoading({
loading: true, data: undefined, numOfRenders: 2, options: {},
})).toBeTruthy();
});
it('should return true if first render is true', () => {
expect(isLoading(false, { current: true }, {})).toBeTruthy();
expect(isLoading({
loading: false, data: undefined, numOfRenders: 1, options: { },
})).toBeTruthy();
});
it('should return false if first render is true and lazy is true', () => {
expect(isLoading(false, { current: true }, { lazy: true })).toBeFalsy();
expect(isLoading({
loading: false, data: undefined, numOfRenders: 1, options: { lazy: true },
})).toBeFalsy();
});
it('should return false if all args are false', () => {
expect(isLoading(false, { current: false }, { lazy: false })).toBeFalsy();
expect(isLoading({ loading: false, numOfRenders: 2, options: { lazy: false } })).toBeFalsy();
});
});

describe('getData', () => {
it('should return data if data exists', () => {
expect(getData({})).toBeTruthy();
expect(getData({ fakeData: true }, 0)).toEqual({ fakeData: true });
});
it('should return initialData data', () => {
expect(getData(undefined, { initialData: { data: { fakeData: true } } })).toBeTruthy();
expect(getData(undefined, 1, { initialData: { data: { fakeData: true } } }))
.toEqual({ fakeData: true });
});
it('should return not initialData data on >1 renders', () => {
expect(getData(undefined, 2, { initialData: { data: { fakeData: true } } })).toBeUndefined();
});
});

describe('getError', () => {
const error = new Error('fake error');
it('should return error if error exists', () => {
expect(getError(new Error('fake error'))).toBeTruthy();
expect(getError(error, 0)).toEqual(error);
});
it('should return initialData error', () => {
expect(getError(undefined, { initialData: { error: new Error('fake error') } })).toBeTruthy();
expect(getError(undefined, 1, { initialData: { error } })).toEqual(error);
});
it('should return not initialData error on >1 renders', () => {
expect(getError(undefined, 2, { initialData: { error } })).toBeUndefined();
});
});

Expand Down
14 changes: 8 additions & 6 deletions src/queryHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* permissions and limitations under the License.
*/

export const isLoading = (loading, isFirstRender, options) => {
export const isLoading = ({
loading, data, numOfRenders, options,
}) => {
if (loading) {
return true;
}
if (isFirstRender?.current) {
if (!data && numOfRenders === 1) {
if (options.lazy) {
return false;
}
Expand All @@ -27,15 +29,15 @@ export const isLoading = (loading, isFirstRender, options) => {
return false;
};

export const getData = (data, options) => {
if (!data) {
export const getData = (data, numOfRenders, options) => {
if (!data && numOfRenders === 1) {
return options?.initialData?.data;
}
return data;
};

export const getError = (error, options) => {
if (!error) {
export const getError = (error, numOfRenders, options) => {
if (!error && numOfRenders === 1) {
return options?.initialData?.error;
}
return error;
Expand Down
12 changes: 5 additions & 7 deletions src/useFetchye.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export const useFetchye = (
const selectedFetcher = typeof fetcher === 'function' ? fetcher : defaultFetcher;
const computedKey = computeKey(key, defaultMapOptionsToKey(mapOptionsToKey(options)));
const { data, loading, error } = useFetchyeSelector(computedKey.hash);
const isFirstRender = useRef(!data && !options?.initialData?.data);
const numOfRenders = useRef(0);
numOfRenders.current += 1;
useEffect(() => {
if (options.lazy || !computedKey) {
return;
}
if (isFirstRender.current !== false) {
isFirstRender.current = false;
}
if (!data && !error && !loading) {
(async () => {
await runAsync({
Expand All @@ -52,9 +50,9 @@ export const useFetchye = (
}
}, [data, loading, error, computedKey, selectedFetcher, options, dispatch, fetchClient]);
return {
isLoading: isLoading(loading, options),
error: getError(error, options),
data: getData(data, options),
isLoading: isLoading({ loading, numOfRenders: numOfRenders.current, options }),
error: getError(error, numOfRenders.current, options),
data: getData(data, numOfRenders.current, options),
run() {
return runAsync({
dispatch, computedKey, fetcher: selectedFetcher, fetchClient, options,
Expand Down

0 comments on commit b255fd5

Please sign in to comment.