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

100% Statements 37/37
100% Branches 4/4
100% Functions 6/6
100% Lines 32/32
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                                                                                                                                         
import expect from 'expect'
import setIn from '../setIn'
 
describe('structure.plain.setIn', () => {
  it('should create a map if state is undefined and key is string', () => {
    expect(setIn(undefined, 'dog', 'fido'))
      .toBeA('object')
      .toEqual({ dog: 'fido' })
  })
 
  it('should create an array if state is undefined and key is string', () => {
    expect(setIn(undefined, '[0]', 'fido'))
      .toBeA(Array)
      .toEqual([ 'fido' ])
    const result = setIn(undefined, '[1]', 'second')
    expect(result)
      .toBeA(Array)
    expect(result.length)
      .toBe(2)
    expect(result[ 0 ])
      .toBe(undefined)
    expect(result[ 1 ])
      .toBe('second')
  })
 
  it('should set and shallow keys without mutating state', () => {
    const state = { foo: 'bar' }
    expect(setIn(state, 'foo', 'baz'))
      .toNotBe(state)
      .toEqual({ foo: 'baz' })
    expect(setIn(state, 'cat', 'fluffy'))
      .toNotBe(state)
      .toEqual({ foo: 'bar', cat: 'fluffy' })
    expect(setIn(state, 'age', 42))
      .toNotBe(state)
      .toEqual({ foo: 'bar', age: 42 })
  })
 
 
  it('should set and deep keys without mutating state', () => {
    const state = {
      foo: {
        bar: [
          'baz',
          { dog: 42 }
        ]
      }
    }
    const result1 = setIn(state, 'tv.best.canines[0]', 'scooby')
    expect(result1)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            'baz',
            { dog: 42 }
          ]
        },
        tv: {
          best: {
            canines: [ 'scooby' ]
          }
        }
      })
    expect(result1.foo).toBe(state.foo)
 
    const result2 = setIn(state, 'foo.bar[0]', 'cat')
    expect(result2)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            'cat',
            { dog: 42 }
          ]
        }
      })
    expect(result2.foo).toNotBe(state.foo)
    expect(result2.foo.bar).toNotBe(state.foo.bar)
    expect(result2.foo.bar[ 1 ]).toBe(state.foo.bar[ 1 ])
 
    const result3 = setIn(state, 'foo.bar[1].dog', 7)
    expect(result3)
      .toNotBe(state)
      .toEqual({
        foo: {
          bar: [
            'baz',
            { dog: 7 }
          ]
        }
      })
    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 ])
  })
})