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

100% Statements 44/44
100% Branches 4/4
100% Functions 5/5
100% Lines 39/39
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                                                                                   
import expect from 'expect'
import deleteWithPath from '../deleteWithPath'
 
describe('structure.plain.deleteWithPath', () => {
  it('should not return state if path not found', () => {
    const state = { foo: 'bar' }
    expect(deleteWithPath(state, /dog/)).toBe(state)
    expect(deleteWithPath(state, /cat\.rat/)).toBe(state)
  })
 
  it('should delete shallow keys without mutating state', () => {
    const state = { foo: 'bar', dog: 'fido' }
    expect(deleteWithPath(state, /foo/))
      .toNotBe(state)
      .toEqual({ dog: 'fido' })
    expect(deleteWithPath(state, /dog/))
      .toNotBe(state)
      .toEqual({ foo: 'bar' })
  })
 
  it('should delete deep keys without mutating state', () => {
    const state = {
      foo: {
        bar: [
          'baz',
          { dog: 42 }
        ]
      },
      cat: {
        bar: 43,
        dog: true
      }
    }
 
    const result1 = deleteWithPath(state, /foo\.bar\[0\]/)
    expect(result1)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            { dog: 42 }
          ]
        },
        cat: {
          bar: 43,
          dog: true
        }
      })
    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 ])
    expect(result1.cat).toBe(state.cat)
 
    const result2 = deleteWithPath(state, /.+bar\[1\]\.dog/)
    expect(result2)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            'baz',
            {}
          ]
        },
        cat: {
          bar: 43,
          dog: true
        }
      })
    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 ])
    expect(result2.cat).toBe(state.cat)
 
    const result3 = deleteWithPath(state, /.+\.dog$/)
    expect(result3)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            'baz',
            {}
          ]
        },
        cat: {
          bar: 43
        }
      })
    expect(result3.foo).toNotBe(state.foo)
    expect(result3.foo.bar).toNotBe(state.foo.bar)
    expect(result3.foo.bar[ 0 ]).toBe(state.foo.bar[ 0 ])
    expect(result3.foo.bar[ 1 ]).toNotBe(state.foo.bar[ 1 ])
    expect(result3.cat).toNotBe(state.cat)
    expect(result3.cat.bar).toBe(state.cat.bar)
 
    const result4 = deleteWithPath(state, /.+\.bar$/)
    expect(result4)
      .toNotBe(state)
      .toEqual({
        foo: {},
        cat: {
          dog: true
        }
      })
    expect(result4.foo).toNotBe(state.foo)
    expect(result4.cat).toNotBe(state.cat)
    expect(result4.cat.dog).toBe(state.cat.dog)
  })
})