Skip to content

Commit 69ae888

Browse files
fix(reduxProvider): dependency array had bad entry (#73)
* fix(reduxProvider): dependency array had bad entry * test(reduxProvider): add unit tests
1 parent 1ee5942 commit 69ae888

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

packages/fetchye-redux-provider/__tests__/FetchyeReduxProvider.spec.jsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,54 @@ describe('FetchyeReduxProvider', () => {
131131
// The effect causes a third render that captures val2 as expected
132132
expect(captureValue).toMatchSnapshot();
133133
});
134+
it('should have loading state change when data is populated', () => {
135+
const fakeCacheSelector = jest.fn()
136+
.mockReturnValueOnce({
137+
data: {},
138+
loading: {
139+
key1: 'val1',
140+
},
141+
errors: {},
142+
})
143+
.mockReturnValueOnce({
144+
data: {
145+
key1: 'val1',
146+
},
147+
loading: {},
148+
errors: {},
149+
});
150+
const cache = SimpleCache({ cacheSelector: fakeCacheSelector });
151+
const captureValue = jest.fn();
152+
153+
const Component = ({ id }) => {
154+
const { useFetchyeSelector } = useContext(FetchyeContext);
155+
const selectedRef = useFetchyeSelector(id);
156+
captureValue(selectedRef.current);
157+
return <fake-element />;
158+
};
159+
160+
Component.propTypes = {
161+
id: PropTypes.string.isRequired,
162+
};
163+
164+
const { rerender } = render(
165+
<Provider store={store}>
166+
<FetchyeReduxProvider cache={cache}>
167+
<Component id="key1" />
168+
</FetchyeReduxProvider>
169+
</Provider>
170+
);
171+
172+
act(() => {
173+
rerender(
174+
<Provider store={store}>
175+
<FetchyeReduxProvider cache={cache}>
176+
<Component id="key1" />
177+
</FetchyeReduxProvider>
178+
</Provider>
179+
);
180+
});
181+
182+
expect(captureValue).toMatchSnapshot();
183+
});
134184
});

packages/fetchye-redux-provider/__tests__/__snapshots__/FetchyeReduxProvider.spec.jsx.snap

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`FetchyeReduxProvider should have loading state change when data is populated 1`] = `
4+
[MockFunction] {
5+
"calls": Array [
6+
Array [
7+
Object {
8+
"data": undefined,
9+
"error": undefined,
10+
"loading": true,
11+
},
12+
],
13+
Array [
14+
Object {
15+
"data": "val1",
16+
"error": undefined,
17+
"loading": false,
18+
},
19+
],
20+
Array [
21+
Object {
22+
"data": undefined,
23+
"error": undefined,
24+
"loading": false,
25+
},
26+
],
27+
Array [
28+
Object {
29+
"data": undefined,
30+
"error": undefined,
31+
"loading": false,
32+
},
33+
],
34+
],
35+
"results": Array [
36+
Object {
37+
"type": "return",
38+
"value": undefined,
39+
},
40+
Object {
41+
"type": "return",
42+
"value": undefined,
43+
},
44+
Object {
45+
"type": "return",
46+
"value": undefined,
47+
},
48+
Object {
49+
"type": "return",
50+
"value": undefined,
51+
},
52+
],
53+
}
54+
`;
55+
356
exports[`FetchyeReduxProvider should return a stable response from useFetchyeSelector that changes properly with a changed input 1`] = `
457
[MockFunction] {
558
"calls": Array [

packages/fetchye-redux-provider/src/FetchyeReduxProvider.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const makeUseFetchyeSelector = ({
3131
const store = useStore();
3232
const selector = useCallback((state) => getCacheByKey(cacheSelector(state), key), [key]);
3333
const initialValue = selector(store.getState());
34-
3534
const lastSelectorValue = useRef(initialValue);
3635
const selectorValue = useRef(initialValue);
3736

@@ -47,7 +46,7 @@ const makeUseFetchyeSelector = ({
4746
}
4847
checkForUpdates();
4948
return subscribe(checkForUpdates);
50-
}, [selector, store]);
49+
}, [selector, store, initialValue]);
5150

5251
return selectorValue;
5352
};

0 commit comments

Comments
 (0)