diff --git a/README.md b/README.md index 5a402f9..64d94b9 100644 --- a/README.md +++ b/README.md @@ -633,13 +633,13 @@ export default BookList; #### One App SSR -Using `makeOneServerFetchye` from `fetchye-one-app` ensures that the cache will +Using `oneFetchye` from `fetchye-one-app` ensures that the cache will always be configured correctly. ```jsx import React from 'react'; import { useFetchye } from 'fetchye'; -import { makeOneServerFetchye } from 'fetchye-one-app'; +import { oneFetchye } from 'fetchye-one-app'; const BookList = () => { const { isLoading, data } = useFetchye('http://example.com/api/books/'); @@ -654,19 +654,14 @@ const BookList = () => { }; BookList.holocron = { - loadModuleData: async ({ store: { dispatch, getState }, fetchClient }) => { + loadModuleData: async ({ store: { dispatch } }) => { if (global.BROWSER) { return; } - const fetchye = makeOneServerFetchye({ - // Redux store - store: { dispatch, getState }, - fetchClient, - }); - // async/await fetchye has same arguments as useFetchye + // oneFetchye has same arguments as useFetchye // dispatches events into the server side Redux store - await fetchye('http://example.com/api/books/'); + await dispatch(oneFetchye('http://example.com/api/books/')); }, }; diff --git a/packages/fetchye-one-app/__tests__/oneFetchye.spec.js b/packages/fetchye-one-app/__tests__/oneFetchye.spec.js new file mode 100644 index 0000000..7693910 --- /dev/null +++ b/packages/fetchye-one-app/__tests__/oneFetchye.spec.js @@ -0,0 +1,33 @@ +import oneFetchye from '../src/oneFetchye'; + +const mockOneCacheSymbol = Symbol('oneCache'); +jest.mock('../src/OneCache', () => jest.fn(() => mockOneCacheSymbol)); +jest.mock('fetchye', () => ({ + makeServerFetchye: jest.fn((...makeFetchyeArgs) => jest.fn((...fetcheArgs) => Promise.resolve( + { makeFetchyeArgs, fetcheArgs } + ))), +})); +describe('oneFetchye', () => { + it('should return a one-app-thunk that calls fetchye', async () => { + expect.assertions(1); + const fetchyeParams = [Symbol('fetchyeArgs 1'), Symbol('fetchyeArgs 2')]; + const fetchyeThunk = oneFetchye(...fetchyeParams); + const thunkParams = [Symbol('dispatch'), Symbol('getState'), Symbol('fetchClient')]; + const response = await fetchyeThunk( + thunkParams[0], thunkParams[1], { fetchClient: thunkParams[2] } + ); + expect(response).toStrictEqual({ + fetcheArgs: fetchyeParams, + makeFetchyeArgs: [ + { + cache: mockOneCacheSymbol, + fetchClient: thunkParams[2], + store: { + dispatch: thunkParams[0], + getState: thunkParams[1], + }, + }, + ], + }); + }); +}); diff --git a/packages/fetchye-one-app/src/index.js b/packages/fetchye-one-app/src/index.js index e40b1cf..cf15f74 100644 --- a/packages/fetchye-one-app/src/index.js +++ b/packages/fetchye-one-app/src/index.js @@ -17,3 +17,4 @@ export { makeOneServerFetchye } from './makeOneServerFetchye'; export OneCache, { oneCacheSelector } from './OneCache'; export OneFetchyeProvider from './OneFetchyeProvider'; +export oneFetchye from './oneFetchye'; diff --git a/packages/fetchye-one-app/src/oneFetchye.js b/packages/fetchye-one-app/src/oneFetchye.js new file mode 100644 index 0000000..8fdc484 --- /dev/null +++ b/packages/fetchye-one-app/src/oneFetchye.js @@ -0,0 +1,13 @@ +import { makeServerFetchye } from 'fetchye'; +import OneCache from './OneCache'; + +const oneFetchye = (...fetchyeArgs) => async (dispatch, getState, { fetchClient }) => { + const fetchye = makeServerFetchye({ + store: { getState, dispatch }, + fetchClient, + cache: OneCache(), + }); + return fetchye(...fetchyeArgs); +}; + +export default oneFetchye;