Skip to content

Commit

Permalink
set empty models as state when erroring (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgrant authored Jul 14, 2022
1 parent cdc768a commit d43cefa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
15 changes: 11 additions & 4 deletions lib/resourcerer.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,17 @@ export const useResources = (getResources, props) => {
});
});
},
onRequestFailure: (status, [name, config]) => loaderDispatch({
type: LoadingStates.ERROR,
payload: {name, config, status}
})
onRequestFailure: (status, [name, config]) => {
// request failed, which means this model should not be in the cache. but we still want to
// set our model state so that when we go back into a loading state, the empty model
// is present instead of any previously-loaded model
setModels(modelAggregator([[name, config]]));

loaderDispatch({
type: LoadingStates.ERROR,
payload: {name, config, status}
});
}
}).then(() => {
if (isMountedRef.current) {
// only get those from the cache. models in state might can empty models, but even so i
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.9",
"version": "1.0.10",
"repository": "github.com/noahgrant/resourcerer",
"description": "Declarative data-fetching and caching framework for React",
"keywords": [
Expand Down
18 changes: 12 additions & 6 deletions test/use-resources.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -600,19 +600,25 @@ describe('useResources', () => {
expect(dataChild.props.notesModel.get('pretend')).toBe(true);
expect(dataChild.props.notesModel instanceof NotesModel).toBe(true);

await unmountAndClearModelCache();

// the models are removed from the cache after erroring
shouldResourcesError = true;
dataChild = findDataChild(renderUseResources());
dataChild = findDataChild(renderUseResources({userId: 'zorah'}));

await waitsFor(() => dataChild.props.hasErrored);

expect(dataChild.props.decisionsCollection.isEmptyModel).toBe(true);
expect(dataChild.props.decisionsCollection instanceof DecisionsCollection).toBe(true);
expect(dataChild.props.userModel.isEmptyModel).toBe(true);
expect(dataChild.props.userModel instanceof UserModel).toBe(true);

shouldResourcesError = false;
// now request a different resource and assert that our model is still empty (because it
// is set as state)
dataChild = findDataChild(renderUseResources({userId: 'lopatron'}));

expect(dataChild.props.userModel.isEmptyModel).toBe(true);
expect(dataChild.props.userModel instanceof UserModel).toBe(true);

await waitsFor(() => dataChild.props.hasLoaded);
expect(dataChild.props.userModel.isEmptyModel).not.toBeDefined();
expect(dataChild.props.userModel instanceof UserModel).toBe(true);
});

it('that cannot be modified', async() => {
Expand Down
18 changes: 12 additions & 6 deletions test/with-resources.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -626,19 +626,25 @@ describe('withResources', () => {
expect(dataChild.props.notesModel.get('pretend')).toBe(true);
expect(dataChild.props.notesModel instanceof NotesModel).toBe(true);

await unmountAndClearModelCache();

// the models are removed from the cache after erroring
shouldResourcesError = true;
dataChild = findDataChild(renderWithResources());
dataChild = findDataChild(renderWithResources({userId: 'zorah'}));

await waitsFor(() => dataChild.props.hasErrored);

expect(dataChild.props.decisionsCollection.isEmptyModel).toBe(true);
expect(dataChild.props.decisionsCollection instanceof DecisionsCollection).toBe(true);
expect(dataChild.props.userModel.isEmptyModel).toBe(true);
expect(dataChild.props.userModel instanceof UserModel).toBe(true);

shouldResourcesError = false;
// now request a different resource and assert that our model is still empty (because it
// is set as state)
dataChild = findDataChild(renderWithResources({userId: 'lopatron'}));

expect(dataChild.props.userModel.isEmptyModel).toBe(true);
expect(dataChild.props.userModel instanceof UserModel).toBe(true);

await waitsFor(() => dataChild.props.hasLoaded);
expect(dataChild.props.userModel.isEmptyModel).not.toBeDefined();
expect(dataChild.props.userModel instanceof UserModel).toBe(true);
});

it('that cannot be modified', async() => {
Expand Down

0 comments on commit d43cefa

Please sign in to comment.