mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-19 19:07:47 -05:00
Add clear to infinite list, fixup tests
This commit is contained in:
parent
cb7c60e8aa
commit
95766fd68a
2 changed files with 20 additions and 5 deletions
|
@ -35,17 +35,16 @@
|
||||||
*/
|
*/
|
||||||
const InfiniteList = key => {
|
const InfiniteList = key => {
|
||||||
|
|
||||||
const initialState = {
|
const getInitialState = () => ({
|
||||||
items: [],
|
items: [],
|
||||||
offset: 0,
|
|
||||||
error: null,
|
error: null,
|
||||||
loading: true,
|
loading: true,
|
||||||
moreToLoad: false
|
moreToLoad: false
|
||||||
};
|
});
|
||||||
|
|
||||||
const reducer = (state, action) => {
|
const reducer = (state, action) => {
|
||||||
if (typeof state === 'undefined') {
|
if (typeof state === 'undefined') {
|
||||||
state = initialState;
|
state = getInitialState();
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
@ -88,6 +87,8 @@ const InfiniteList = key => {
|
||||||
loading: false,
|
loading: false,
|
||||||
moreToLoad: false
|
moreToLoad: false
|
||||||
};
|
};
|
||||||
|
case `${key}_CLEAR`:
|
||||||
|
return getInitialState();
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@ -100,6 +101,7 @@ const InfiniteList = key => {
|
||||||
error: error => ({type: `${key}_ERROR`, error}),
|
error: error => ({type: `${key}_ERROR`, error}),
|
||||||
loading: () => ({type: `${key}_LOADING`}),
|
loading: () => ({type: `${key}_LOADING`}),
|
||||||
append: (items, moreToLoad) => ({type: `${key}_APPEND`, items, moreToLoad}),
|
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.
|
* Load more action returns a thunk. It takes a function to call to get more items.
|
||||||
|
|
|
@ -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', () => {
|
describe('ERROR', () => {
|
||||||
let action;
|
let action;
|
||||||
let error = new Error();
|
let error = new Error();
|
||||||
|
@ -167,7 +180,7 @@ describe('Infinite List redux module', () => {
|
||||||
describe('selector', () => {
|
describe('selector', () => {
|
||||||
test('will return the slice of state defined by the key', () => {
|
test('will return the slice of state defined by the key', () => {
|
||||||
const state = {
|
const state = {
|
||||||
[module.key]: module.reducer(undefined, {}) // eslint-disable-line no-undefined
|
[module.key]: initialState
|
||||||
};
|
};
|
||||||
expect(module.selector(state)).toBe(initialState);
|
expect(module.selector(state)).toBe(initialState);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue