Skip to content

Commit 2dc1b13

Browse files
dominic-farquharsoncode-forger
authored andcommitted
feat(fetchye-one-app): only execute streamFetchye action on server (#113)
* feat(fetchye-one-app): only execute streamFetchye action on server * refactor(fetchye-one-app): prevent dispatching stream action on client
1 parent 6e30af6 commit 2dc1b13

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

packages/fetchye-one-app/__tests__/streaming/streamFetchye.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ const streamActionSpy = jest.spyOn(actions, 'stream');
2727
const mockDispatch = jest.fn();
2828

2929
describe('streamFetchye', () => {
30+
const originalWindow = global.window;
31+
32+
beforeEach(() => {
33+
jest.clearAllMocks();
34+
Object.defineProperty(global, 'window', {
35+
value: undefined,
36+
configurable: true,
37+
writable: true,
38+
});
39+
});
40+
41+
afterEach(() => {
42+
global.window = originalWindow;
43+
});
44+
3045
it('should return a one-app-thunk that dispatches an action resolving to the passed in thunk', async () => {
3146
expect.assertions(2);
3247

@@ -63,4 +78,21 @@ describe('streamFetchye', () => {
6378
);
6479
expect(fetchyeThunk).toHaveBeenCalledWith(...fetchyeParams.slice(1));
6580
});
81+
82+
it('should not dispatch the stream action on the client', async () => {
83+
expect.assertions(1);
84+
85+
global.window = originalWindow;
86+
87+
const fetchyeThunk = jest.fn();
88+
fetchyeThunk.mockResolvedValue(Symbol('fetchRequest'));
89+
90+
const fetchyeParams = [fetchyeThunk, Symbol('fetchyeArgs - key'), Symbol('fetchyeArgs - options'), Symbol('fetchyeArgs - fetcher')];
91+
const streamFetchyeThunk = streamFetchye(...fetchyeParams);
92+
const thunkParams = [mockDispatch, Symbol('getState'), Symbol('fetchClient')];
93+
await streamFetchyeThunk(
94+
fetchyeThunk, thunkParams[0], thunkParams[1], { fetchClient: thunkParams[2] }
95+
);
96+
expect(streamActionSpy).not.toHaveBeenCalled();
97+
});
6698
});

packages/fetchye-one-app/src/streaming/streamFetchye.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ export const streamFetchye = (thunk, key, opts = {}, fetcher = undefined) => asy
2323
const { hash: computedKey } = computeKey(key, opts);
2424
const promise = dispatch(thunk(key, opts, fetcher));
2525

26-
dispatch(
27-
stream([
28-
{
29-
key: computedKey,
30-
domain: STREAM_DOMAIN,
31-
promise,
32-
},
33-
])
34-
);
26+
if (!global.window) {
27+
dispatch(
28+
stream([
29+
{
30+
key: computedKey,
31+
domain: STREAM_DOMAIN,
32+
promise,
33+
},
34+
])
35+
);
36+
}
3537

3638
return promise;
3739
};

0 commit comments

Comments
 (0)