<commit_before>let { expect } = require('chai')
let { default: undoable, ActionCreators } = require('../src/index')

describe('Undoable', () => {
  let mockUndoableReducer
  let mockInitialState
  let incrementedState

  before('setup mock reducers and states', () => {
    let countInitialState = 0
    let countReducer = (state = countInitialState, action = {}) => {
      switch (action.type) {
        case 'INCREMENT':
          return state + 1
        case 'DECREMENT':
          return state - 1
        default:
          return state
      }
    }
    let undoConfig = {
      limit: 100,
      initTypes: 'RE-INITIALIZE',
      filter: function (action) {
        switch (action.type) {
          case 'DECREMENT':
            return false
          default:
            return true
        }
      }
    }
    mockUndoableReducer = undoable(countReducer, undoConfig)
    // { past: [], present: 0, future: [] }
    mockInitialState = mockUndoableReducer(void 0, {})
    // { past: [ 0 ], present: 1, future: [] }
    incrementedState = mockUndoableReducer(mockInitialState, { type: 'INCREMENT' })
  })

  it('should not record unwanted actions', () => {
    let decrementedState = mockUndoableReducer(mockInitialState, { type: 'DECREMENT' })

    expect(decrementedState.past).to.deep.equal([])
    expect(decrementedState.future).to.deep.equal([])
  })
  it('should reset upon init actions', () => {
    let doubleIncrementedState = mockUndoableReducer(incrementedState, { type: 'INCREMENT' })
    let reInitializedState = mockUndoableReducer(doubleIncrementedState, { type: 'RE-INITIALIZE' })

    expect(reInitializedState.past.length).to.equal(0)
    expect(reInitializedState.future.length).to.equal(0)
  })

  describe('Undo', () => {
    let undoState
    before('perform an undo action', () => {
      undoState = mockUndoableReducer(incrementedState, ActionCreators.undo())
    })
    it('should change present state back by one action', () => {
      expect(undoState.present).to.equal(mockInitialState.present)
    })
    it('should change present state to first element of \'past\'', () => {
      expect(undoState.present).to.equal(incrementedState.past[0])
    })
    it('should add a new element to \'future\' from last state', () => {
      expect(undoState.future[0]).to.equal(incrementedState.present)
    })
    it('should decrease length of \'past\' by one', () => {
      expect(undoState.past.length).to.equal(incrementedState.past.length - 1)
    })
    it('should increase length of \'future\' by one', () => {
      expect(undoState.future.length).to.equal(incrementedState.future.length + 1)
    })
    it('should do nothing if \'past\' is empty', () => {
      let undoInitialState = mockUndoableReducer(mockInitialState, ActionCreators.undo())
      expect(mockInitialState.past.length).to.equal(0)
      expect(undoInitialState.present).to.deep.equal(mockInitialState.present)
    })
  })
  describe('Redo', () => {
    let undoState
    let redoState
    before('perform an undo action then a redo action', () => {
      undoState = mockUndoableReducer(incrementedState, ActionCreators.undo())
      redoState = mockUndoableReducer(undoState, ActionCreators.redo())
    })
    it('should change present state to equal state before undo', () => {
      expect(redoState.present).to.equal(incrementedState.present)
    })
    it('should change present state to first element of \'future\'', () => {
      expect(redoState.present).to.equal(undoState.future[0])
    })
    it('should add a new element to \'past\' from last state', () => {
      expect(redoState.past[0]).to.equal(undoState.present)
    })
    it('should decrease length of \'future\' by one', () => {
      expect(redoState.future.length).to.equal(undoState.future.length - 1)
    })
    it('should increase length of \'past\' by one', () => {
      expect(redoState.past.length).to.equal(undoState.past.length + 1)
    })
    it('should do nothing if \'future\' is empty', () => {
      let secondRedoState = mockUndoableReducer(redoState, ActionCreators.redo())
      expect(redoState.future.length).to.equal(0)
      expect(secondRedoState.present).to.deep.equal(redoState.present)
    })
  })
})<commit_msg>Units Tests should respect `initialHistory`<commit_after>let { expect } = require('chai')
let { default: undoable, ActionCreators } = require('../src/index')

describe('Undoable', () => {
  let mockUndoableReducer
  let mockInitialState
  let incrementedState

  before('setup mock reducers and states', () => {
    let countInitialState = 0
    let countReducer = (state = countInitialState, action = {}) => {
      switch (action.type) {
        case 'INCREMENT':
          return state + 1
        case 'DECREMENT':
          return state - 1
        default:
          return state
      }
    }
    let undoConfig = {
      limit: 100,
      initTypes: 'RE-INITIALIZE',
      initialHistory: {
        past: [0, 1, 2, 3],
        present: 4,
        future: [5, 6, 7]
      },
      filter: function (action) {
        switch (action.type) {
          case 'DECREMENT':
            return false
          default:
            return true
        }
      }
    }
    mockUndoableReducer = undoable(countReducer, undoConfig)
    mockInitialState = mockUndoableReducer(void 0, {})
    incrementedState = mockUndoableReducer(mockInitialState, { type: 'INCREMENT' })
  })

  it('should not record unwanted actions', () => {
    let decrementedState = mockUndoableReducer(mockInitialState, { type: 'DECREMENT' })

    expect(decrementedState.past).to.deep.equal(mockInitialState.past)
    expect(decrementedState.future).to.deep.equal(mockInitialState.future)
  })
  it('should reset upon init actions', () => {
    let doubleIncrementedState = mockUndoableReducer(incrementedState, { type: 'INCREMENT' })
    let reInitializedState = mockUndoableReducer(doubleIncrementedState, { type: 'RE-INITIALIZE' })

    expect(reInitializedState.past.length).to.equal(0)
    expect(reInitializedState.future.length).to.equal(0)
  })

  describe('Undo', () => {
    let undoState
    before('perform an undo action', () => {
      undoState = mockUndoableReducer(incrementedState, ActionCreators.undo())
    })
    it('should change present state back by one action', () => {
      expect(undoState.present).to.equal(mockInitialState.present)
    })
    it('should change present state to last element of \'past\'', () => {
      expect(undoState.present).to.equal(incrementedState.past[incrementedState.past.length - 1])
    })
    it('should add a new element to \'future\' from last state', () => {
      expect(undoState.future[0]).to.equal(incrementedState.present)
    })
    it('should decrease length of \'past\' by one', () => {
      expect(undoState.past.length).to.equal(incrementedState.past.length - 1)
    })
    it('should increase length of \'future\' by one', () => {
      expect(undoState.future.length).to.equal(incrementedState.future.length + 1)
    })
    it('should do nothing if \'past\' is empty', () => {
      let undoInitialState = mockUndoableReducer(mockInitialState, ActionCreators.undo())
      if (!mockInitialState.past.length) {
        expect(undoInitialState.present).to.deep.equal(mockInitialState.present)
      }
    })
  })
  describe('Redo', () => {
    let undoState
    let redoState
    before('perform an undo action then a redo action', () => {
      undoState = mockUndoableReducer(incrementedState, ActionCreators.undo())
      redoState = mockUndoableReducer(undoState, ActionCreators.redo())
    })
    it('should change present state to equal state before undo', () => {
      expect(redoState.present).to.equal(incrementedState.present)
    })
    it('should change present state to first element of \'future\'', () => {
      expect(redoState.present).to.equal(undoState.future[0])
    })
    it('should add a new element to \'past\' from last state', () => {
      expect(redoState.past[redoState.past.length - 1]).to.equal(undoState.present)
    })
    it('should decrease length of \'future\' by one', () => {
      expect(redoState.future.length).to.equal(undoState.future.length - 1)
    })
    it('should increase length of \'past\' by one', () => {
      expect(redoState.past.length).to.equal(undoState.past.length + 1)
    })
    it('should do nothing if \'future\' is empty', () => {
      let secondRedoState = mockUndoableReducer(redoState, ActionCreators.redo())
      if (!redoState.future.length) {
        expect(secondRedoState.present).to.deep.equal(redoState.present)
      }
    })
  })
})
{'fraction_non_alphanumeric': 0.14232384507699486, 'fraction_numerical': 0.005482967802146524, 'mean_word_length': 3.135552339604438, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3555916', 'n_tokens_mistral': 2752, 'n_tokens_neox': 2581, 'n_words': 533}