Skip to content

Commit

Permalink
#7 entry selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Aug 1, 2018
1 parent dbbb805 commit 680be73
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 64 deletions.
152 changes: 90 additions & 62 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/state/actions.entries.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Name of action to set a page of entries
*
* @type {string}
*/
export const SET_ENTRIES = 'SET_ENTRIES';

/**
Expand All @@ -13,6 +18,6 @@ export const setEntries = (formId, pageNumber, entries) => {
formId,
pageNumber,
entries
}
}
};
};

14 changes: 14 additions & 0 deletions src/state/selectors.entries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Get one page of a form's entries
* @param {String} formId ID of form these entries belong to.
* @param {Number} pageNumber Page number for these entries
* @param {Object} state Collection of entries, by form Id and page
* @return {*}
*/
export const getPageOfEntries = (formId,pageNumber,state) =>{
if( state.hasOwnProperty(formId) && state[formId].hasOwnProperty(pageNumber) ){
return state[formId][pageNumber];
}
return [];
};

3 changes: 3 additions & 0 deletions src/tests/reducers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import {
import {setEntries} from "../state/actions.entries";

describe( 'entriesReducer', () => {
it( 'does not effect other actions', () => {
expect( entriesReducer({a:1},{type:'INIT'})).toEqual({a:1});
});
it( 'adds a page of entries', () => {
const state = entriesReducer({}, setEntries(
'CF1',
Expand Down
35 changes: 35 additions & 0 deletions src/tests/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
getStyleIncludes,
getOtherSettings
} from "../state/selectors.settings";
import {entriesReducer} from "../state/reducers";
import {getPageOfEntries} from "../state/selectors.entries";

describe( 'settings selectors', () => {
const mockState = {
Expand Down Expand Up @@ -154,3 +156,36 @@ describe('Privacy settings selectors', () => {
});
});

describe( 'entries selector', () => {
it( 'finds entries', () => {
expect( getPageOfEntries(
'CF1',
2,
{
CF1 :{
2: {
a:1
}
}
}

) ).toEqual( {a:1})
});

it( 'Returns empty array for invalid page', () => {
expect( getPageOfEntries(
'CF1',
2,
{
CF1 :{
1: {
a:1
}
}
}

) ).toEqual([]);
})
});


0 comments on commit 680be73

Please sign in to comment.