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

Framework: add schema for current user #3356

Merged
merged 1 commit into from
Feb 17, 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
7 changes: 6 additions & 1 deletion client/state/current-user/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { combineReducers } from 'redux';
* Internal dependencies
*/
import { CURRENT_USER_ID_SET, SERIALIZE, DESERIALIZE } from 'state/action-types';
import { isValidStateWithSchema } from 'state/utils';
import { idSchema } from './schema';

/**
* Tracks the current user ID.
Expand All @@ -23,7 +25,10 @@ export function id( state = null, action ) {
case SERIALIZE:
return state;
case DESERIALIZE:
return state;
if ( isValidStateWithSchema( state, idSchema ) ) {
return state;
}
return null;
}

return state;
Expand Down
4 changes: 4 additions & 0 deletions client/state/current-user/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const idSchema = {
type: [ 'integer', 'null' ],
minimum: 0
};
13 changes: 11 additions & 2 deletions client/state/current-user/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { expect } from 'chai';
import sinon from 'sinon';

/**
* Internal dependencies
Expand All @@ -27,13 +28,21 @@ describe( 'reducer', () => {
} );

describe( 'persistence', () => {
it.skip( 'should validate ID is positive', () => {
before( () => {
sinon.stub( console, 'warn' );
} );

after( () => {
console.warn.restore();
} );

it( 'should validate ID is positive', () => {
const state = id( -1, {
type: DESERIALIZE
} );
expect( state ).to.equal( null );
} );
it.skip( 'should validate ID is a number', () => {
it( 'should validate ID is a number', () => {
const state = id( 'foobar', {
type: DESERIALIZE
} );
Expand Down
11 changes: 4 additions & 7 deletions client/state/sites/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@
* External dependencies
*/
import { expect } from 'chai';
import mockery from 'mockery';
import sinon from 'sinon';
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
import { SITE_RECEIVE, SERIALIZE, DESERIALIZE } from 'state/action-types';
import { items } from '../reducer';

let items;
describe( 'reducer', () => {
before( function() {
mockery.registerMock( 'lib/warn', () => {} );
mockery.enable( { warnOnReplace: false, warnOnUnregistered: false } );
items = require( '../reducer' ).items;
sinon.stub( console, 'warn' );
} );
after( function() {
mockery.deregisterAll();
mockery.disable();
console.warn.restore();
} );
describe( '#items()', () => {
it( 'should default to an empty object', () => {
Expand Down