Skip to content

Commit

Permalink
fix(reduxProvider): dependency array had bad entry (#73)
Browse files Browse the repository at this point in the history
* fix(reduxProvider): dependency array had bad entry

* test(reduxProvider): add unit tests
  • Loading branch information
Matthew-Mallimo committed Apr 5, 2023
1 parent 1ee5942 commit 69ae888
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,54 @@ describe('FetchyeReduxProvider', () => {
// The effect causes a third render that captures val2 as expected
expect(captureValue).toMatchSnapshot();
});
it('should have loading state change when data is populated', () => {
const fakeCacheSelector = jest.fn()
.mockReturnValueOnce({
data: {},
loading: {
key1: 'val1',
},
errors: {},
})
.mockReturnValueOnce({
data: {
key1: 'val1',
},
loading: {},
errors: {},
});
const cache = SimpleCache({ cacheSelector: fakeCacheSelector });
const captureValue = jest.fn();

const Component = ({ id }) => {
const { useFetchyeSelector } = useContext(FetchyeContext);
const selectedRef = useFetchyeSelector(id);
captureValue(selectedRef.current);
return <fake-element />;
};

Component.propTypes = {
id: PropTypes.string.isRequired,
};

const { rerender } = render(
<Provider store={store}>
<FetchyeReduxProvider cache={cache}>
<Component id="key1" />
</FetchyeReduxProvider>
</Provider>
);

act(() => {
rerender(
<Provider store={store}>
<FetchyeReduxProvider cache={cache}>
<Component id="key1" />
</FetchyeReduxProvider>
</Provider>
);
});

expect(captureValue).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FetchyeReduxProvider should have loading state change when data is populated 1`] = `
[MockFunction] {
"calls": Array [
Array [
Object {
"data": undefined,
"error": undefined,
"loading": true,
},
],
Array [
Object {
"data": "val1",
"error": undefined,
"loading": false,
},
],
Array [
Object {
"data": undefined,
"error": undefined,
"loading": false,
},
],
Array [
Object {
"data": undefined,
"error": undefined,
"loading": false,
},
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`FetchyeReduxProvider should return a stable response from useFetchyeSelector that changes properly with a changed input 1`] = `
[MockFunction] {
"calls": Array [
Expand Down
3 changes: 1 addition & 2 deletions packages/fetchye-redux-provider/src/FetchyeReduxProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const makeUseFetchyeSelector = ({
const store = useStore();
const selector = useCallback((state) => getCacheByKey(cacheSelector(state), key), [key]);
const initialValue = selector(store.getState());

const lastSelectorValue = useRef(initialValue);
const selectorValue = useRef(initialValue);

Expand All @@ -47,7 +46,7 @@ const makeUseFetchyeSelector = ({
}
checkForUpdates();
return subscribe(checkForUpdates);
}, [selector, store]);
}, [selector, store, initialValue]);

return selectorValue;
};
Expand Down

0 comments on commit 69ae888

Please sign in to comment.