<commit_before>/* global describe, it, expect, beforeEach */

import Vue from 'vue'
import Datepicker from '../../src/Datepicker.vue'

let vm

function dpc (state = {}) {
  return {
    template: '<div><datepicker :value="value" format="yyyy-MM-dd" name="dp1" v-ref:component></datepicker></div>',
    components: { Datepicker },
    data: function () {
      return state
    }
  }
}

describe('Datepicker unmounted', () => {
  it('has a ready hook', () => {
    expect(typeof Datepicker.ready).to.equal('function')
  })

  it('sets the correct default data', () => {
    expect(typeof Datepicker.data).to.equal('function')
    const defaultData = Datepicker.data()
    expect(defaultData.selectedDate).to.equal(null)
    expect(defaultData.showDayView).to.equal(false)
    expect(defaultData.showMonthView).to.equal(false)
    expect(defaultData.showYearView).to.equal(false)
    expect(defaultData.formattedValue).to.equal(null)
    expect(defaultData.calendarHeight).to.equal(0)
  })

  it('correctly sets the value when created', () => {
    const date = new Date(2016, 1, 15)
    const vm = new Vue(dpc({value: date})).$mount()
    expect(vm.value).to.equal(date)
  })
})

describe('Datepicker: mounted component', () => {
  it('correctly sets the value from method', () => {
    const date = new Date(2016, 1, 15)
    const vm = new Vue(dpc({
      value: date
    })).$mount()
    const newDate = new Date(2016, 9, 15)
    expect(typeof vm.$refs.component.setValue).to.equal('function')
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.selectedDate).to.equal(newDate)
    expect(vm.$refs.component.formattedValue).to.equal('2016-10-15')
    const now = new Date()
    vm.$refs.component.setValue()
    expect(vm.$refs.component.selectedDate).to.equal(null)
    const currDate = new Date(vm.$refs.component.currDate)
    expect(currDate.getYear()).to.equal(now.getYear())
    expect(currDate.getMonth()).to.equal(now.getMonth())
    expect(currDate.getDate()).to.equal(1)
  })

  it('knows the selected year', () => {
    const newDate = new Date(2016, 9, 15)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.isSelectedYear(newDate)).to.equal(true)
    expect(vm.$refs.component.isSelectedYear(new Date(2017, 1, 15))).to.equal(false)
  })

  it('knows the selected month', () => {
    const newDate = new Date(2016, 9, 15)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.isSelectedMonth(newDate)).to.equal(true)
    expect(vm.$refs.component.isSelectedMonth(new Date(2017, 1, 15))).to.equal(false)
  })

  it('knows the selected date', () => {
    const newDate = new Date(2016, 9, 15)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.isSelectedDate(newDate)).to.equal(true)
    expect(vm.$refs.component.isSelectedDate(new Date(2017, 1, 1))).to.equal(false)
  })

  it('sets the date', () => {
    const date = new Date(2016, 9, 9)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setDate(date.getTime())
    expect(vm.$refs.component.value.getTime()).to.equal(date.getTime())
    expect(vm.$refs.component.formattedValue).to.equal('2016-10-09')
  })
})

describe('Datepicker.vue', () => {
  beforeEach(() => {
    document.body.appendChild(document.createElement('div'))
    vm = new Vue({
      el: 'div',
      template: '<div><datepicker :value="value" name="dp2" format="yyyy-MM-d" v-ref:component></datepicker></div>',
      components: { Datepicker },
      data: function () {
        return {
          value: new Date(2016, 1, 15)
        }
      }
    })
  })

  it('should close when clicked outside', () => {
    vm.$refs.component.showDayCalendar()<commit_msg>fix failing test<commit_after>/* global describe, it, expect, beforeEach */

import Vue from 'vue'
import Datepicker from '../../src/Datepicker.vue'

let vm

function dpc (state = {}) {
  return {
    template: '<div><datepicker :value="value" format="yyyy-MM-dd" name="dp1" v-ref:component></datepicker></div>',
    components: { Datepicker },
    data: function () {
      return state
    }
  }
}

describe('Datepicker unmounted', () => {
  it('has a ready hook', () => {
    expect(typeof Datepicker.ready).to.equal('function')
  })

  it('sets the correct default data', () => {
    expect(typeof Datepicker.data).to.equal('function')
    const defaultData = Datepicker.data()
    expect(defaultData.selectedDate).to.equal(null)
    expect(defaultData.showDayView).to.equal(false)
    expect(defaultData.showMonthView).to.equal(false)
    expect(defaultData.showYearView).to.equal(false)
    expect(defaultData.formattedValue).to.equal(null)
    expect(defaultData.calendarHeight).to.equal(0)
  })

  it('correctly sets the value when created', () => {
    const date = new Date(2016, 1, 15)
    const vm = new Vue(dpc({value: date})).$mount()
    expect(vm.value).to.equal(date)
  })
})

describe('Datepicker: mounted component', () => {
  it('correctly sets the value from method', () => {
    const date = new Date(2016, 1, 15)
    const vm = new Vue(dpc({
      value: date
    })).$mount()
    const newDate = new Date(2016, 9, 15)
    expect(typeof vm.$refs.component.setValue).to.equal('function')
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.selectedDate).to.equal(newDate)
    expect(vm.$refs.component.formattedValue).to.equal('2016-10-15')
    const now = new Date()
    vm.$refs.component.setValue()
    expect(vm.$refs.component.selectedDate).to.equal(null)
    const currDate = new Date(vm.$refs.component.currDate)
    expect(currDate.getYear()).to.equal(now.getYear())
    expect(currDate.getMonth()).to.equal(now.getMonth())
    expect(currDate.getDate()).to.equal(1)
  })

  it('knows the selected year', () => {
    const newDate = new Date(2016, 9, 15)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.isSelectedYear(newDate)).to.equal(true)
    expect(vm.$refs.component.isSelectedYear(new Date(2017, 1, 15))).to.equal(false)
  })

  it('knows the selected month', () => {
    const newDate = new Date(2016, 9, 15)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.isSelectedMonth(newDate)).to.equal(true)
    expect(vm.$refs.component.isSelectedMonth(new Date(2017, 1, 15))).to.equal(false)
  })

  it('knows the selected date', () => {
    const newDate = new Date(2016, 9, 15)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setValue(newDate)
    expect(vm.$refs.component.isSelectedDate(newDate)).to.equal(true)
    expect(vm.$refs.component.isSelectedDate(new Date(2017, 1, 1))).to.equal(false)
  })

  it('sets the date', () => {
    const date = new Date(2016, 9, 9)
    const vm = new Vue(dpc()).$mount()
    vm.$refs.component.setDate(date.getTime())
    expect(vm.$refs.component.selectedDate.getTime()).to.equal(date.getTime())
    expect(vm.$refs.component.formattedValue).to.equal('2016-10-09')
  })
})

describe('Datepicker.vue', () => {
  beforeEach(() => {
    document.body.appendChild(document.createElement('div'))
    vm = new Vue({
      el: 'div',
      template: '<div><datepicker :value="value" name="dp2" format="yyyy-MM-d" v-ref:component></datepicker></div>',
      components: { Datepicker },
      data: function () {
        return {
          value: new Date(2016, 1, 15)
        }
      }
    })
  })

  it('should close when clicked outside', () => {
    vm.$refs.component.showDayCalendar()
{'fraction_non_alphanumeric': 0.1791907514450867, 'fraction_numerical': 0.024960588544403573, 'mean_word_length': 3.667688534641324, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1438902', 'n_tokens_mistral': 2712, 'n_tokens_neox': 2501, 'n_words': 453}