all files / src/structure/plain/__tests__/ deleteIn.spec.js

100% Statements 40/40
100% Branches 4/4
100% Functions 6/6
100% Lines 34/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                                                                                                                 
import expect from 'expect'
import deleteIn from '../deleteIn'
 
describe('structure.plain.deleteIn', () => {
  it('should not return state if path not found', () => {
    const state = { foo: 'bar' }
    expect(deleteIn(state, undefined)).toBe(state)
    expect(deleteIn(state, 'dog')).toBe(state)
    expect(deleteIn(state, 'cat.rat.pig')).toBe(state)
  })
 
  it('should delete shallow keys without mutating state', () => {
    const state = { foo: 'bar', dog: 'fido' }
    expect(deleteIn(state, 'foo'))
      .toNotBe(state)
      .toEqual({ dog: 'fido' })
    expect(deleteIn(state, 'dog'))
      .toNotBe(state)
      .toEqual({ foo: 'bar' })
  })
 
  it('should delete shallow array indexes without mutating state', () => {
    const state = [ 'the', 'quick', 'brown', 'fox' ]
    expect(deleteIn(state, 4)).toBe(state) // index not found
    expect(deleteIn(state, 0))
      .toNotBe(state)
      .toEqual([ 'quick', 'brown', 'fox' ])
    expect(deleteIn(state, 1))
      .toNotBe(state)
      .toEqual([ 'the', 'brown', 'fox' ])
    expect(deleteIn(state, 2))
      .toNotBe(state)
      .toEqual([ 'the', 'quick', 'fox' ])
    expect(deleteIn(state, 3))
      .toNotBe(state)
      .toEqual([ 'the', 'quick', 'brown' ])
  })
 
  it('should delete deep keys without mutating state', () => {
    const state = {
      foo: {
        bar: [
          'baz',
          { dog: 42 }
        ]
      }
    }
 
    const result1 = deleteIn(state, 'foo.bar[0]')
    expect(result1)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            { dog: 42 }
          ]
        }
      })
    expect(result1.foo).toNotBe(state.foo)
    expect(result1.foo.bar).toNotBe(state.foo.bar)
    expect(result1.foo.bar.length).toBe(1)
    expect(result1.foo.bar[ 0 ]).toBe(state.foo.bar[ 1 ])
 
    const result2 = deleteIn(state, 'foo.bar[1].dog')
    expect(result2)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            'baz',
            {}
          ]
        }
      })
    expect(result2.foo).toNotBe(state.foo)
    expect(result2.foo.bar).toNotBe(state.foo.bar)
    expect(result2.foo.bar[ 0 ]).toBe(state.foo.bar[ 0 ])
    expect(result2.foo.bar[ 1 ]).toNotBe(state.foo.bar[ 1 ])
 
    const result3 = deleteIn(state, 'foo.bar')
    expect(result3)
      .toNotBe(state)
      .toEqual({
        foo: {}
      })
    expect(result3.foo).toNotBe(state.foo)
  })
})