Skip to content

Commit

Permalink
Framework: update initial-state test to not use ui tree, add case for…
Browse files Browse the repository at this point in the history
… localforageState but no serverState
  • Loading branch information
gwwar authored and aduth committed Feb 25, 2016
1 parent 0b0ec88 commit b03a15e
Showing 1 changed file with 104 additions and 10 deletions.
114 changes: 104 additions & 10 deletions client/state/test/initial-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe( 'initial-state', () => {
var configStub,
consoleSpy,
state,
serverState = { ui: { section: 'hello' } };
serverState = { currentUser: { id: 123456789 } };
before( ( done ) => {
window.initialReduxState = serverState;
configStub = sinon.stub( config, 'isEnabled' );
Expand All @@ -42,7 +42,7 @@ describe( 'initial-state', () => {
expect( state._timestamp ).to.equal( undefined );
} );
it( 'builds state using server state', () => {
expect( state.ui.section ).to.equal( 'hello' );
expect( state.currentUser.id ).to.equal( 123456789 );
} );
} );
} );
Expand All @@ -53,11 +53,31 @@ describe( 'initial-state', () => {
localforageGetItemStub,
state,
savedState = {
ui: { section: 'bar', hasSidebar: true },
currentUser: { id: 123456789 },
users: {
items: {
123456789: {
ID: 123456789,
username: 'testuser'
},
111111111: {
ID: 111111111,
username: 'testuser2'
}
}
},
_timestamp: Date.now()
},
serverState = { ui: { section: 'hello' } };
serverState = {
users: {
items: {
123456789: {
ID: 123456789,
username: 'updatedtestuser'
}
}
}
};
before( ( done ) => {
window.initialReduxState = serverState;
configStub = sinon.stub( config, 'isEnabled' );
Expand Down Expand Up @@ -91,8 +111,7 @@ describe( 'initial-state', () => {
expect( state._timestamp ).to.equal( undefined );
} );
it( 'server state shallowly overrides local forage state', () => {
expect( state.ui.section ).to.equal( 'hello' );
expect( state.hasSidebar ).to.equal( undefined );
expect( state.users ).to.equal( serverState.users );
} );
} );
describe( 'with stale persisted data and initial server data', () => {
Expand All @@ -101,11 +120,31 @@ describe( 'initial-state', () => {
localforageGetItemStub,
state,
savedState = {
ui: { section: 'bar', hasSidebar: true },
currentUser: { id: 123456789 },
users: {
items: {
123456789: {
ID: 123456789,
username: 'testuser'
},
111111111: {
ID: 111111111,
username: 'testuser2'
}
}
},
_timestamp: Date.now() - MAX_AGE
},
serverState = { ui: { section: 'hello' } };
serverState = {
users: {
items: {
123456789: {
ID: 123456789,
username: 'updatedtestuser'
}
}
}
};
before( ( done ) => {
window.initialReduxState = serverState;
configStub = sinon.stub( config, 'isEnabled' );
Expand Down Expand Up @@ -133,8 +172,7 @@ describe( 'initial-state', () => {
expect( consoleSpy.called ).to.equal( false );
} );
it( 'builds using server state', () => {
expect( state.ui.section ).to.equal( 'hello' );
expect( state.hasSidebar ).to.equal( undefined );
expect( state.users ).to.equal( serverState.users );
} );
it( 'does not build using local forage state', () => {
expect( state.currentUser.id ).to.equal( null );
Expand All @@ -143,6 +181,62 @@ describe( 'initial-state', () => {
expect( state._timestamp ).to.equal( undefined );
} );
} );
describe( 'with recently persisted data and no initial server data', () => {
var configStub,
consoleSpy,
localforageGetItemStub,
state,
savedState = {
currentUser: { id: 123456789 },
users: {
items: {
123456789: {
ID: 123456789,
username: 'testuser'
},
111111111: {
ID: 111111111,
username: 'testuser2'
}
}
},
_timestamp: Date.now()
},
serverState = {};
before( ( done ) => {
window.initialReduxState = serverState;
configStub = sinon.stub( config, 'isEnabled' );
configStub.withArgs( 'persist-redux' ).returns( true );
consoleSpy = sinon.spy( console, 'error' );
localforageGetItemStub = sinon.stub( localforage, 'getItem' )
.returns(
new Promise( function( resolve ) {
resolve( savedState );
} )
);
const reduxReady = function( reduxStore ) {
state = reduxStore.getState();
done();
};
createReduxStoreFromPersistedInitialState( reduxReady );
} );
after( () => {
window.initialReduxState = null;
configStub.restore();
consoleSpy.restore();
localforageGetItemStub.restore();
} );
it( 'builds store without errors', () => {
expect( consoleSpy.called ).to.equal( false );
} );
it( 'builds state using local forage state', () => {
expect( state.currentUser.id ).to.equal( 123456789 );
expect( state.users ).to.equal( savedState.users );
} );
it( 'does not add timestamp to store', () => {
expect( state._timestamp ).to.equal( undefined );
} );
} );
} );
} );
} );

0 comments on commit b03a15e

Please sign in to comment.