Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reduxProvider): dependency array had bad entry #73

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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