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

Add themeRequestError reducer and getThemeRequestError selector and tests. #9791

Merged
merged 2 commits into from
Dec 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions client/state/themes/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { combineReducers } from 'redux';
import { mapValues } from 'lodash';
import { mapValues, omit } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -170,14 +170,17 @@ export function themeRequests( state = {}, action ) {
* @param {Object} action Action payload
* @return {Object} Updated state
*/
export const themeRequestsError = createReducer( {}, {
export const themeRequestErrors = createReducer( {}, {
[ THEME_REQUEST_FAILURE ]: ( state, { siteId, themeId, error } ) => ( {
...state,
[ siteId ]: Object.assign( {}, state[ siteId ], { [ themeId ]: { error } } )
[ siteId ]: {
...state[ siteId ],
[ themeId ]: error
}
} ),
[ THEME_REQUEST_SUCCESS ]: ( state, { siteId, themeId } ) => ( {
...state,
[ siteId ]: Object.assign( {}, state[ siteId ], { [ themeId ]: null } )
[ siteId ]: omit( state[ siteId ], themeId ),
} )
} );

Expand Down Expand Up @@ -287,7 +290,7 @@ export default combineReducers( {
// queryRequests,
// lastQuery
themeRequests,
// themeRequestsError
// themeRequestErrors
activeThemes,
activeThemeRequests,
activationRequests,
Expand Down
8 changes: 2 additions & 6 deletions client/state/themes/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,8 @@ export const getTheme = createSelector(
* @param {Number} siteId Site ID
* @return {Object} error object if present or null otherwise
*/
export function getThemeRequestsError( state, themeId, siteId ) {
const themes = get( state.themes.themeRequestsError, siteId, null );
if ( ! themes ) {
return null;
}
return get( themes, themeId, null );
export function getThemeRequestErrors( state, themeId, siteId ) {
return get( state.themes.themeRequestErrors, [ siteId, themeId ], null );
}

/**
Expand Down
53 changes: 22 additions & 31 deletions client/state/themes/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import reducer, {
queries,
lastQuery,
themeRequests,
themeRequestsError,
themeRequestErrors,
activeThemes,
activationRequests,
activeThemeRequests,
Expand Down Expand Up @@ -465,29 +465,27 @@ describe( 'reducer', () => {
} );
} );

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

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

it( 'should map site ID, theme ID to null value if request was successful', () => {
const state = themeRequestsError( deepFreeze( {} ), {
it( 'should create empyt mapping on success if previous state was empty', () => {
const state = themeRequestErrors( deepFreeze( {} ), {
type: THEME_REQUEST_SUCCESS,
siteId: 2916284,
themeId: 841
themeId: 'twentysixteen'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good spot.

} );

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

it( 'should map site ID, theme ID to error object if request finishes with failure', () => {
const state = themeRequestsError( deepFreeze( {} ), {
it( 'should map site ID, theme ID to error if request finishes with failure', () => {
const state = themeRequestErrors( deepFreeze( {} ), {
type: THEME_REQUEST_FAILURE,
siteId: 2916284,
themeId: 'vivaro',
Expand All @@ -496,15 +494,13 @@ describe( 'reducer', () => {

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

it( 'should switch from error to null after successful request after a failure', () => {
const state = themeRequestsError( deepFreeze( {
it( 'should switch from error to no mapping after successful request after a failure', () => {
const state = themeRequestErrors( deepFreeze( {
2916284: {
pinboard: {
error: 'Request Error'
Expand All @@ -517,37 +513,32 @@ describe( 'reducer', () => {
} );

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

it( 'should accumulate mappings', () => {
const state = themeRequestsError( deepFreeze( {
const state = themeRequestErrors( deepFreeze( {
2916284: {
twentysixteennnnn: {
error: 'No such theme!'
}
twentysixteennnnn: 'No such theme!'
}
} ), {
type: THEME_REQUEST_SUCCESS,
type: THEME_REQUEST_FAILURE,
siteId: 2916284,
themeId: 'twentysixteen'
themeId: 'twentysixteen',
error: 'System error'
} );

expect( state ).to.deep.equal( {
2916284: {
twentysixteen: null,
twentysixteennnnn: {
error: 'No such theme!'
}
twentysixteennnnn: 'No such theme!',
twentysixteen: 'System error'
}
} );
} );

it( 'never persists state', () => {
const state = themeRequestsError( deepFreeze( {
const state = themeRequestErrors( deepFreeze( {
2916284: {
twentysixteen: null
}
Expand All @@ -559,7 +550,7 @@ describe( 'reducer', () => {
} );

it( 'never loads persisted state', () => {
const state = themeRequestsError( deepFreeze( {
const state = themeRequestErrors( deepFreeze( {
2916284: {
twentysixteen: null
}
Expand Down
18 changes: 7 additions & 11 deletions client/state/themes/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { values } from 'lodash';
import {
getThemes,
getTheme,
getThemeRequestsError,
getThemeRequestErrors,
isRequestingTheme,
getThemesForQuery,
getLastThemeQuery,
Expand Down Expand Up @@ -133,31 +133,27 @@ describe( 'themes selectors', () => {

describe( '#getThemesRequestError()', () => {
it( 'should return null if thre is not request error storred for that theme on site', () => {
const error = getThemeRequestsError( {
const error = getThemeRequestErrors( {
themes: {
themeRequestsError: {}
themeRequestErrors: {}
}
}, 'twentysixteen', 413 );

expect( error ).to.be.null;
} );

it( 'should return the error object for the site ID, theme ID pair', () => {
const error = getThemeRequestsError( {
const error = getThemeRequestErrors( {
themes: {
themeRequestsError: {
themeRequestErrors: {
2916284: {
twentysixteen: {
error: 'Request error'
}
twentysixteen: 'Request error'
}
}
}
}, 'twentysixteen', 2916284, );

expect( error ).to.deep.equal( {
error: 'Request error'
} );
expect( error ).to.equal( 'Request error' );
} );
} );

Expand Down