all files / src/structure/immutable/__tests__/ deleteWithPath.js

100% Statements 48/48
100% Branches 4/4
100% Functions 5/5
100% Lines 40/40
1 branch Ignored     
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 113 114 115 116                                                                                                                                                         
import deleteWithPath from '../deleteWithPath'
import { fromJS } from 'immutable'
import expectations from '../expectations'
import addExpectations from '../../../__tests__/addExpectations'
 
describe('structure.immutable.deleteWithPath', () => {
  const expect = addExpectations(expectations)
 
  it('should not return state if path not found', () => {
    const state = fromJS({ 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 = fromJS({ foo: 'bar', dog: 'fido' })
    expect(deleteWithPath(state, /foo/))
      .toNotBe(state)
      .toEqualMap({ dog: 'fido' })
    expect(deleteWithPath(state, /dog/))
      .toNotBe(state)
      .toEqualMap({ foo: 'bar' })
  })
 
  it('should delete deep keys without mutating state', () => {
    const state = fromJS({
      foo: {
        bar: [
          'baz',
          { dog: 42 }
        ]
      },
      cat: {
        bar: 43,
        dog: true
      }
    })
 
    const result1 = deleteWithPath(state, /foo\.bar\[0\]/)
    expect(result1)
      .toNotBe(state)
      .toEqualMap({
        foo: {
          bar: [
            { dog: 42 }
          ]
        },
        cat: {
          bar: 43,
          dog: true
        }
      })
    expect(result1.get('foo')).toNotBe(state.get('foo'))
    expect(result1.get('foo').get('bar')).toNotBe(state.get('foo').get('bar'))
    expect(result1.get('foo').get('bar').count()).toBe(1)
    expect(result1.get('foo').get('bar').get(0)).toBe(state.get('foo').get('bar').get(1))
    expect(result1.get('cat')).toBe(state.get('cat'))
 
    const result2 = deleteWithPath(state, /.+bar\[1\]\.dog/)
    expect(result2)
      .toNotBe(state)
      .toEqualMap({
        foo: {
          bar: [
            'baz',
            {}
          ]
        },
        cat: {
          bar: 43,
          dog: true
        }
      })
    expect(result2.get('foo')).toNotBe(state.get('foo'))
    expect(result2.get('foo').get('bar')).toNotBe(state.get('foo').get('bar'))
    expect(result2.get('foo').get('bar').get(0)).toBe(state.get('foo').get('bar').get(0))
    expect(result2.get('foo').get('bar').get(1)).toNotBe(state.get('foo').get('bar').get(1))
    expect(result2.get('cat')).toBe(state.get('cat'))
 
    const result3 = deleteWithPath(state, /.+\.dog$/)
    expect(result3)
      .toNotBe(state)
      .toEqualMap({
        foo: {
          bar: [
            'baz',
            {}
          ]
        },
        cat: {
          bar: 43
        }
      })
    expect(result3.get('foo')).toNotBe(state.get('foo'))
    expect(result3.get('foo').get('bar')).toNotBe(state.get('foo').get('bar'))
    expect(result3.get('foo').get('bar').get(0)).toBe(state.get('foo').get('bar').get(0))
    expect(result3.get('foo').get('bar').get(1)).toNotBe(state.get('foo').get('bar').get(1))
    expect(result3.get('cat')).toNotBe(state.get('cat'))
    expect(result3.get('cat').get('bar')).toBe(state.get('cat').get('bar'))
 
    const result4 = deleteWithPath(state, /.+\.bar$/)
    expect(result4)
      .toNotBe(state)
      .toEqualMap({
        foo: {},
        cat: {
          dog: true
        }
      })
    expect(result4.get('foo')).toNotBe(state.get('foo'))
    expect(result4.get('cat')).toNotBe(state.get('cat'))
    expect(result4.get('cat').get('dog')).toBe(state.get('cat').get('dog'))
  })
})