jest
  .autoMockOff()

$ = require('jquery')
Data = require('../data')

element = $("""
  <div
    data-burger="Component"
    data-name="Gabe"
    data-height="6"
    data-food='{ "apple": "like" }'>
  </div>
""")

describe 'Data', ->
  instance = undefined

  beforeEach ->
    instance = new Data(element)

  describe '#get', ->

    it 'gets a value', ->
      data = instance.get()

      expect(data.name).toEqual('Gabe')
      expect(data.height).toEqual(6)
      expect(data.food).toEqual({ apple: 'like' })
      expect(data.burger).toEqual(undefined)
      expect(instance.get('name')).toEqual(data.name)
      expect(instance.get('height')).toEqual(data.height)

    it 'gets a nested value', ->
      data = element.data()
      expect(instance.get('food.apple')).toEqual(data.food.apple)

  describe '#set', ->
    it 'sets values', ->
      newName = 'Bob'
      instance.set('name', newName)

      expect(instance.get('name')).toEqual(newName)

    it 'allows you do a nested setting', ->
      taste = 'dislike'
      instance.set('food.apple', taste)

      expect(instance.get('food.apple')).toEqual(taste)

    it 'makes a copy when setting a new value', ->
      oldData = instance.get()
      newData = instance.set('height', 10)

      expect(oldData).not.toEqual(newData)

    it 'sets nested values if they were not defined', ->
      value = 5
      instance.set('one.two.three.four', value)
      _data = instance.get()

      expect(_data.one.two.three.four).toEqual(value)

  describe '#bind', ->
    it 'binds settings values to a callback', ->
      count = 0

      instance.bind 'name', ->
        instance.set('height', 12)

      instance.bind('height', -> count++)

      instance.set('name', 'stub')
      instance.set('height', 11)

      expect(count).toEqual(2)

  describe '#isEqual', ->
    other = undefined

    beforeEach ->
      other = new Data(element)

    it 'checks whether two data objects are equal', ->
      expect(instance.isEqual(other)).toEqual(true)

      other.set('something', 55)

      expect(instance.isEqual(other)).toEqual(false)

