Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Themes: Add queryRequestErrors reducer and tests #9839

Merged
merged 2 commits into from
Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions client/state/themes/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,35 @@ export function queryRequests( state = {}, action ) {
return state;
}

/**
* Returns the updated query request error state after an action has been
* dispatched. The state reflects a mapping of site ID, query ID pairing to an
* object containing the request error. If there is no error null is stored.
*
* @param {Object} state Current state
* @param {Object} action Action payload
* @return {Object} Updated state
*/
export const queryRequestErrors = createReducer( {}, {
[ THEMES_REQUEST_FAILURE ]: ( state, { siteId, query, error } ) => {
const serializedQuery = getSerializedThemesQuery( query, siteId );
return {
...state,
[ siteId ]: {
...state[ siteId ],
[ serializedQuery ]: error
}
};
},
[ THEMES_REQUEST_SUCCESS ]: ( state, { siteId, query } ) => {
const serializedQuery = getSerializedThemesQuery( query, siteId );
return {
...state,
[ siteId ]: omit( state[ siteId ], serializedQuery ),
};
}
} );

/**
* Returns the updated theme query state after an action has been dispatched.
* The state reflects a mapping of serialized query key to an array of theme IDs
Expand Down
100 changes: 97 additions & 3 deletions client/state/themes/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from 'state/action-types';
import reducer, {
queryRequests,
queryRequestErrors,
queries,
lastQuery,
themeRequests,
Expand Down Expand Up @@ -181,6 +182,101 @@ describe( 'reducer', () => {
} );
} );

describe( '#queryRequestErrors()', () => {
it( 'should default to an empty object', () => {
const state = queryRequestErrors( undefined, {} );

expect( state ).to.deep.equal( {} );
} );

it( 'should create empty mapping on success if previous state was empty', () => {
const state = queryRequestErrors( deepFreeze( {} ), {
type: THEMES_REQUEST_SUCCESS,
siteId: 2916284,
query: { search: 'Twenty' }
} );

expect( state ).to.deep.equal( {
2916284: {}
} );
} );

it( 'should map site ID, query to error if request finishes with failure', () => {
const state = queryRequestErrors( deepFreeze( {} ), {
type: THEMES_REQUEST_FAILURE,
siteId: 2916284,
query: { search: 'Twenty' },
error: 'Request error'
} );

expect( state ).to.deep.equal( {
2916284: {
'2916284:{"search":"Twenty"}': 'Request error'
}
} );
} );

it( 'should reset error state after successful request after a failure', () => {
const state = queryRequestErrors( deepFreeze( {
2916284: {
'2916284:{"search":"Twenty"}': 'Request Error'
}
} ), {
type: THEMES_REQUEST_SUCCESS,
siteId: 2916284,
query: { search: 'Twenty' }
} );

expect( state ).to.deep.equal( {
2916284: {}
} );
} );

it( 'should accumulate mappings', () => {
const state = queryRequestErrors( deepFreeze( {
2916284: {
'2916284:{"blerch":"Twenty"}': 'Invalid query!'
}
} ), {
type: THEMES_REQUEST_FAILURE,
siteId: 2916284,
query: { search: 'Twenty' },
error: 'System error'
} );

expect( state ).to.deep.equal( {
2916284: {
'2916284:{"blerch":"Twenty"}': 'Invalid query!',
'2916284:{"search":"Twenty"}': 'System error'
}
} );
} );

it( 'never persists state', () => {
const state = queryRequestErrors( deepFreeze( {
2916284: {
'2916284:{"search":"Twenty"}': null
}
} ), {
type: SERIALIZE
} );

expect( state ).to.deep.equal( {} );
} );

it( 'never loads persisted state', () => {
const state = queryRequestErrors( deepFreeze( {
2916284: {
'2916284:{"search":"Twenty"}': null
}
} ), {
type: DESERIALIZE
} );

expect( state ).to.deep.equal( {} );
} );
} );

describe( '#queries()', () => {
it( 'should default to an empty object', () => {
const state = queries( undefined, {} );
Expand Down Expand Up @@ -502,9 +598,7 @@ describe( 'reducer', () => {
it( 'should switch from error to no mapping after successful request after a failure', () => {
const state = themeRequestErrors( deepFreeze( {
2916284: {
pinboard: {
error: 'Request Error'
}
pinboard: 'Request Error'
}
} ), {
type: THEME_REQUEST_SUCCESS,
Expand Down