import { addDays, getDay, startOfMonth, subDays } from 'date-fns'
import {
  daysInCalendar,
  getDates,
  getDayOfWeekLabel,
  getMonthLabel,
} from '../CalendarHelpers'

describe('getDates', () => {
  it('should always have a length of daysInCalendar days', () => {
    const dateArray = getDates(new Date('2019-01-17'))
    expect(dateArray.length).toBe(daysInCalendar)
  })

  it('has the right start date', () => {
    const date = new Date('2019-2-2')
    const dateArray = getDates(date)
    const startDate = subDays(startOfMonth(date), getDay(startOfMonth(date)))
    expect(dateArray[0]).toEqual(startDate)
  })

  it('has the right end date', () => {
    const date = new Date('2019-2-2')
    const dateArray = getDates(date)
    const startDate = subDays(startOfMonth(date), getDay(startOfMonth(date)))
    const endDate = addDays(startDate, daysInCalendar - 1)
    expect(dateArray[daysInCalendar - 1]).toEqual(endDate)
  })

  it('in january, previous month days come from december of previous year', () => {
    const date = new Date('2019-01-02')
    const dateArray = getDates(date)
    expect(dateArray[0].getMonth()).toBe(11)
    expect(date.getFullYear() - dateArray[0].getFullYear()).toBe(1)
  })

  it('in december, next month days come from january of next year', () => {
    const date = new Date('2019-12-25')
    const dateArray = getDates(date)
    const lastDay = dateArray[daysInCalendar - 1]
    expect(lastDay.getMonth()).toBe(0)
    expect(lastDay.getFullYear() - date.getFullYear()).toBe(1)
  })

  it('is Jan month label', () => {
    const label = getMonthLabel(0, 'en-US')
    expect(label).toBe('Jan')
  })

  it('is Jun month label', () => {
    const label = getMonthLabel(5, 'en-US')
    expect(label).toBe('Jun')
  })

  it('is janv month label (fr)', () => {
    const label = getMonthLabel(0, 'fr-FR')
    expect(label).toBe('janv.')
  })

  it('day of week (fr)', () => {
    const label = getDayOfWeekLabel(0, 'fr-FR')
    expect(label).toBe('di')
  })
})
