Skip to content

Commit

Permalink
Merge pull request #3357 from Automattic/add/persist-sharing-publicize
Browse files Browse the repository at this point in the history
Framework: add schema for sharing/publicize
  • Loading branch information
gwwar committed Mar 8, 2016
2 parents 208ea86 + f725bd4 commit 0b2c11a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
7 changes: 6 additions & 1 deletion client/state/sharing/publicize/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
SERIALIZE,
DESERIALIZE
} from 'state/action-types';
import { connectionsSchema } from './schema';
import { isValidStateWithSchema } from 'state/utils';

/**
* Track the current status for fetching connections. Maps site ID to the
Expand Down Expand Up @@ -55,8 +57,11 @@ export function connections( state = {}, action ) {
case PUBLICIZE_CONNECTIONS_RECEIVE:
return Object.assign( {}, state, keyBy( action.data.connections, 'ID' ) );
case SERIALIZE:
return {};
return state;
case DESERIALIZE:
if ( isValidStateWithSchema( state, connectionsSchema ) ) {
return state;
}
return {};
}

Expand Down
31 changes: 31 additions & 0 deletions client/state/sharing/publicize/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const connectionsSchema = {
type: 'object',
patternProperties: {
'^\\d+$': {
type: 'object',
required: [ 'ID', 'site_ID' ],
properties: {
ID: { type: 'integer' },
site_ID: { type: 'integer' },
user_ID: { type: 'integer' },
keyring_connection_ID: { type: 'integer' },
keyring_connection_user_ID: { type: 'integer' },
shared: { type: 'boolean' },
service: { type: 'string' },
label: { type: 'string' },
issued: { type: 'string' },
expires: { type: 'string' },
external_ID: { type: [ 'string', 'null' ] },
external_name: { type: [ 'string', 'null' ] },
external_display: { type: [ 'string', 'null' ] },
external_profile_picture: { type: [ 'string', 'null' ] },
external_profile_URL: { type: [ 'string', 'null' ] },
external_follower_count: { type: [ 'integer', 'null' ] },
status: { type: 'string' },
refresh_URL: { type: 'string' },
meta: { type: 'object' }
}
}
},
additionalProperties: false
};
36 changes: 33 additions & 3 deletions client/state/sharing/publicize/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { expect } from 'chai';
import deepFreeze from 'deep-freeze';
import sinon from 'sinon';

/**
* Internal dependencies
Expand Down Expand Up @@ -122,23 +123,52 @@ describe( '#connections()', () => {
} );

describe( 'persistence', () => {
it( 'does not persist data because this is not implemented yet', () => {
before( () => {
sinon.stub( console, 'warn' );
} );
after( () => {
console.warn.restore();
} );

it( 'should persist data', () => {
const state = deepFreeze( {
1: { ID: 1, site_ID: 2916284 },
2: { ID: 2, site_ID: 2916284 }
} );
const persistedState = connections( state, { type: SERIALIZE } );
expect( persistedState ).to.eql( {} );
expect( persistedState ).to.eql( state );
} );

it( 'does not load persisted data because this is not implemented yet', () => {
it( 'should load valid data', () => {
const persistedState = deepFreeze( {
1: { ID: 1, site_ID: 2916284 },
2: { ID: 2, site_ID: 2916284 }
} );
const state = connections( persistedState, {
type: DESERIALIZE
} );
expect( state ).to.eql( persistedState );
} );

it( 'should ignore loading data with invalid keys', () => {
const persistedState = deepFreeze( {
foo: { ID: 1, site_ID: 2916284 },
bar: { ID: 2, site_ID: 2916284 }
} );
const state = connections( persistedState, {
type: DESERIALIZE
} );
expect( state ).to.eql( {} );
} );

it( 'should ignore loading data with invalid values', () => {
const persistedState = deepFreeze( {
1: { ID: 1, site_ID: 'foo' },
2: { ID: 2, site_ID: 2916284 }
} );
const state = connections( persistedState, {
type: DESERIALIZE
} );
expect( state ).to.eql( {} );
} );
} );
Expand Down

0 comments on commit 0b2c11a

Please sign in to comment.