Skip to content

Commit

Permalink
Minimise diff
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 15, 2018
1 parent d1da833 commit 2217c1d
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions editor/utils/with-history/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,39 @@
import withHistory from '../';

describe( 'withHistory', () => {
const counter = ( state = { count: 0 }, { type } ) => {
return type === 'INCREMENT' ? { count: state.count + 1 } : state;
};
const counter = ( state = 0, { type } ) => (
type === 'INCREMENT' ? state + 1 : state
);

it( 'should return a new reducer', () => {
const reducer = withHistory( counter );
const state = reducer( undefined, {} );

expect( state ).toEqual( {
expect( typeof reducer ).toBe( 'function' );
expect( reducer( undefined, {} ) ).toEqual( {
past: [],
present: { count: 0 },
future: [],
} );
} );

it( 'should track changes in present', () => {
const reducer = withHistory( counter );

let state;
state = reducer( undefined, {} );
state = reducer( state, { type: 'INCREMENT' } );

expect( state ).toEqual( {
past: [ { count: 0 } ],
present: { count: 1 },
present: 0,
future: [],
} );
} );

it( 'should create undo level', () => {
it( 'should track history', () => {
const reducer = withHistory( counter );

let state;
state = reducer( undefined, {} );
state = reducer( state, { type: 'INCREMENT' } );
// state = reducer( state, { type: 'CREATE_UNDO_LEVEL' } );

expect( state ).toEqual( {
past: [ { count: 0 } ],
present: { count: 1 },
past: [ 0 ],
present: 1,
future: [],
} );

state = reducer( state, { type: 'INCREMENT' } );

expect( state ).toEqual( {
past: [ { count: 0 }, { count: 1 } ],
present: { count: 2 },
past: [ 0, 1 ],
present: 2,
future: [],
} );
} );
Expand All @@ -66,9 +51,14 @@ describe( 'withHistory', () => {

expect( state ).toEqual( {
past: [],
present: { count: 0 },
future: [ { count: 1 } ],
present: 0,
future: [ 1 ],
} );
} );

it( 'should not perform undo on empty past', () => {
const reducer = withHistory( counter );
const state = reducer( undefined, {} );

expect( state ).toBe( reducer( state, { type: 'UNDO' } ) );
} );
Expand All @@ -83,10 +73,15 @@ describe( 'withHistory', () => {
state = reducer( state, { type: 'REDO' } );

expect( state ).toEqual( {
past: [ { count: 0 } ],
present: { count: 1 },
past: [ 0 ],
present: 1,
future: [],
} );
} );

it( 'should not perform redo on empty future', () => {
const reducer = withHistory( counter );
const state = reducer( undefined, {} );

expect( state ).toBe( reducer( state, { type: 'REDO' } ) );
} );
Expand All @@ -101,11 +96,19 @@ describe( 'withHistory', () => {

expect( state ).toEqual( {
past: [],
present: { count: 1 },
present: 1,
future: [],
} );
} );

it( 'should return same reference if state has not changed', () => {
const reducer = withHistory( counter );
const original = reducer( undefined, {} );
const state = reducer( original, {} );

expect( state ).toBe( original );
} );

it( 'should overwrite present state with option.shouldOverwriteState', () => {
const reducer = withHistory( counter, {
shouldOverwriteState: ( { type } ) => type === 'INCREMENT',
Expand All @@ -116,16 +119,16 @@ describe( 'withHistory', () => {
state = reducer( state, { type: 'INCREMENT' } );

expect( state ).toEqual( {
past: [ { count: 0 } ],
present: { count: 1 },
past: [ 0 ],
present: 1,
future: [],
} );

state = reducer( state, { type: 'INCREMENT' } );

expect( state ).toEqual( {
past: [ { count: 0 } ],
present: { count: 2 },
past: [ 0 ],
present: 2,
future: [],
} );
} );
Expand All @@ -141,16 +144,16 @@ describe( 'withHistory', () => {
state = reducer( state, { type: 'CREATE_UNDO_LEVEL' } );

expect( state ).toEqual( {
past: [ { count: 0 } ],
present: { count: 1 },
past: [ 0 ],
present: 1,
future: [],
} );

state = reducer( state, { type: 'INCREMENT' } );

expect( state ).toEqual( {
past: [ { count: 0 }, { count: 1 } ],
present: { count: 2 },
past: [ 0, 1 ],
present: 2,
future: [],
} );
} );
Expand Down

0 comments on commit 2217c1d

Please sign in to comment.