all files / src/__tests__/ flattenPaths.spec.js

100% Statements 19/19
100% Branches 4/4
100% Functions 6/6
100% Lines 14/14
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                                                                   
import expect from 'expect'
import flattenPaths from '../flattenPaths'
 
describe('flattenPaths', () => {
  it('should return an empty map if no fields are provided', () => {
    expect(flattenPaths(undefined)).toEqual({})
    expect(flattenPaths(null)).toEqual({})
    expect(flattenPaths(7)).toEqual({})
    expect(flattenPaths({})).toEqual({})
    expect(flattenPaths([])).toEqual({})
  })
 
  it('should flatten an already flat object', () => {
    expect(flattenPaths({ a: 1, b: 2, c: 3 })).toEqual({ a: 1, b: 2, c: 3 })
  })
 
  it('should flatten an array into a flat object', () => {
    expect(flattenPaths(['a', 'b', 'c'])).toEqual({ '0': 'a', '1': 'b', '2': 'c' })
  })
 
  it('should flatten an arbitrarily deep structure', () => {
    expect(flattenPaths({
      a: {
        b: {
          c: 'the',
          d: 'quick'
        },
        c: 'brown'
      },
      d: [
        'fox',
        'jumps',
        {
          e: 'over'
        }
      ]
    })).toEqual({
      'a.b.c': 'the',
      'a.b.d': 'quick',
      'a.c': 'brown',
      'd[0]': 'fox',
      'd[1]': 'jumps',
      'd[2].e': 'over'
    })
  })
})