import { getValue, setValue } from '../nestedValues'

const testUser1 = {
  name: 'Test User 1',
  age: 27,
  interests: ['food']
}
const testUser2 = {
  name: 'Test User 2',
  age: 23,
  interests: ['party', 'facebook']
}
const newUser = {
  name: 'Test User 3',
  age: 31,
  interests: ['work', 'life']
}
const testMe = {
  users: [testUser1, testUser2]
}

describe('get item', () => {
  it('gets first level element', () => {
    const result = getValue(testMe, 'users')
    expect(result.length).toBe(2)
  })
  it('extracts elements from an array', () => {
    const result = getValue(testMe, 'users[1]')
    expect(result).toEqual(testUser2)
  })
  it('gets second order elements', () => {
    const result = getValue(testMe, 'users[1].interests[0]')
    expect(result).toEqual('party')
  })
})

describe('set item', () => {
  it('sets first level element', () => {
    const result = setValue(testMe, 'users', [newUser])
    expect(result).toEqual({ users: [newUser] })
  })
  it('sets array elements', () => {
    const result = setValue(testMe, 'users[1]', newUser)
    expect(result).toEqual({ users: [testUser1, newUser] })
  })
  it('sets object elements', () => {
    const result = setValue(testMe, 'users[0].age', 76)
    expect(result).toEqual({ users: [{ ...testUser1, age: 76 }, testUser2] })
  })
})

export default true
