From 95766fd68a58d44d14a65ba60227057a4bb711d9 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Mon, 10 May 2021 10:22:18 -0400 Subject: [PATCH] Add clear to infinite list, fixup tests --- src/redux/infinite-list.js | 10 ++++++---- test/unit/redux/infinite-list.test.js | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/redux/infinite-list.js b/src/redux/infinite-list.js index b09e10b12..90f664577 100644 --- a/src/redux/infinite-list.js +++ b/src/redux/infinite-list.js @@ -35,17 +35,16 @@ */ const InfiniteList = key => { - const initialState = { + const getInitialState = () => ({ items: [], - offset: 0, error: null, loading: true, moreToLoad: false - }; + }); const reducer = (state, action) => { if (typeof state === 'undefined') { - state = initialState; + state = getInitialState(); } switch (action.type) { @@ -88,6 +87,8 @@ const InfiniteList = key => { loading: false, moreToLoad: false }; + case `${key}_CLEAR`: + return getInitialState(); default: return state; } @@ -100,6 +101,7 @@ const InfiniteList = key => { error: error => ({type: `${key}_ERROR`, error}), loading: () => ({type: `${key}_LOADING`}), append: (items, moreToLoad) => ({type: `${key}_APPEND`, items, moreToLoad}), + clear: () => ({type: `${key}_CLEAR`}), /** * Load more action returns a thunk. It takes a function to call to get more items. diff --git a/test/unit/redux/infinite-list.test.js b/test/unit/redux/infinite-list.test.js index 9e9995a3d..991caa0ae 100644 --- a/test/unit/redux/infinite-list.test.js +++ b/test/unit/redux/infinite-list.test.js @@ -104,6 +104,19 @@ describe('Infinite List redux module', () => { }); }); + describe('CLEAR', () => { + test('resets everything back to the initial state', () => { + const state = { + error: new Error(), + items: [1, 2, 3], + loading: 'something not initial', + moreToLoad: 'something not initial' + }; + const newState = module.reducer(state, module.actions.clear()); + expect(newState).toEqual(initialState); + }); + }); + describe('ERROR', () => { let action; let error = new Error(); @@ -167,7 +180,7 @@ describe('Infinite List redux module', () => { describe('selector', () => { test('will return the slice of state defined by the key', () => { const state = { - [module.key]: module.reducer(undefined, {}) // eslint-disable-line no-undefined + [module.key]: initialState }; expect(module.selector(state)).toBe(initialState); });