Skip to content

Commit

Permalink
Allow lazy lazy (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgrant authored Mar 15, 2023
1 parent 186c59c commit 5d6ff67
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/resourcerer.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export const useResources = (getResources, props) => {
return (!previousCacheKey || previousCacheKey !== getCacheKey(config) ||
!hasAllDependencies(prevPropsRef.current, [, config]) || config.refetch ||
// make sure if we were lazy and are no longer lazy (from the same component) that we
// get included in the list to get updated
(prevConfig?.lazy && !config.lazy));
// get included in the list to get updated (and vice versa)
(prevConfig?.lazy !== config.lazy));
});

/**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "resourcerer",
"version": "1.0.11",
"version": "1.0.12",
"repository": "github.com/noahgrant/resourcerer",
"description": "Declarative data-fetching and caching framework for React",
"keywords": [
Expand Down
8 changes: 5 additions & 3 deletions test/use-resources.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const getResources = ({
params: {shouldError: props.analystsError}
},
[DECISIONS]: {
...(props.includeDeleted ? {params: {include_deleted: true}} : {}),
...props.includeDeleted ? {params: {include_deleted: true}} : {},
lazy: props.lazy,
measure
},
Expand Down Expand Up @@ -1226,19 +1226,21 @@ describe('useResources', () => {
]);

requestSpy.mockClear();
Collection.prototype.fetch.mockClear();
ModelCache.remove('decisions');

// now let's test the case where a single component changes its lazy status
dataChild = findDataChild(renderUseResources({lazy: true}));

expect(dataChild.props.decisionsLoadingState).toEqual(LoadingStates.LOADED);
expect(dataChild.props.hasLoaded).toBe(true);
expect(requestSpy.mock.calls.map(([name]) => name)).toEqual([]);
expect(Collection.prototype.fetch).not.toHaveBeenCalled();

dataChild = findDataChild(renderUseResources({lazy: false}));

await waitsFor(() => dataChild.props.hasLoaded);
expect(requestSpy.mock.calls.map(([name]) => name)).toEqual([ResourceKeys.DECISIONS]);
expect(Collection.prototype.fetch).toHaveBeenCalledTimes(1);
expect(Collection.prototype.fetch.mock.instances[0] instanceof DecisionsCollection).toBe(true);
});

it('isOrWillBeLoading is true for two cycles that props change and loading starts', async() => {
Expand Down
6 changes: 4 additions & 2 deletions test/with-resources.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1275,19 +1275,21 @@ describe('withResources', () => {
]);

requestSpy.mockClear();
Collection.prototype.fetch.mockClear();
ModelCache.remove('decisions');

// now let's test the case where a single component changes its lazy status
dataChild = findDataChild(renderWithResources({lazy: true}));

expect(dataChild.props.decisionsLoadingState).toEqual(LoadingStates.LOADED);
expect(dataChild.props.hasLoaded).toBe(true);
expect(requestSpy.mock.calls.map(([name]) => name)).toEqual([]);
expect(Collection.prototype.fetch).not.toHaveBeenCalled();

dataChild = findDataChild(renderWithResources({lazy: false}));

await waitsFor(() => dataChild.props.hasLoaded);
expect(requestSpy.mock.calls.map(([name]) => name)).toEqual([ResourceKeys.DECISIONS]);
expect(Collection.prototype.fetch).toHaveBeenCalledTimes(1);
expect(Collection.prototype.fetch.mock.instances[0] instanceof DecisionsCollection).toBe(true);
});

it('isOrWillBeLoading is true for two cycles that props change and loading starts', async() => {
Expand Down

0 comments on commit 5d6ff67

Please sign in to comment.