import React from 'react';
import { configure, render, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

import Counter from './';

configure({ adapter: new Adapter() });

const mockFunction = jest.fn();

describe('DateCounter component', () => {
  it('renders "Launched on" title when countdown is finished', () => {
    const counter = mount(<Counter date="2019/06/25 16:26" onTimeout={mockFunction} t={key => key}/>);
    expect(counter.text().includes('dateCounter.launchedOn')).toBe(true);
    expect(counter.text().includes('/')).toBe(true);
    expect(counter.text().includes(':')).toBe(false);
    expect(counter.props().onTimeout).toHaveBeenCalled();
  });

  it('renders "Time to launch" title when countdown is unfinished', () => {
    const unfinishedDate = new Date();
    unfinishedDate.setSeconds(unfinishedDate.getSeconds() + 5000);
    const counter = render(<Counter date={unfinishedDate} t={key => key} />);
    expect(counter.text().includes('dateCounter.timeToLaunch')).toBe(true);
    expect(counter.text().includes(':')).toBe(true);
    expect(counter.text().includes('/')).toBe(false);
  });

  it('changes title when countdown is finished', () => {
    const unfinishedDate = new Date();
    unfinishedDate.setSeconds(unfinishedDate.getSeconds() + 1);
    const counter = render(<Counter date={unfinishedDate} t={key => key} />);
    expect(counter.text().includes('dateCounter.timeToLaunch')).toBe(true);
    setTimeout(() => {
      expect(counter.text().includes('dateCounter.launchedOn')).toBe(true);
    }, 1000);
  });

  it('renders text "week" when it is more than 1 week to finish', () => {
    const unfinishedDate = new Date();
    unfinishedDate.setMonth(unfinishedDate.getMonth() + 1);
    const counter = render(<Counter date={unfinishedDate} t={key => key} />);
    expect(counter.text().includes('week')).toBe(true);
    expect(counter.text().includes('minute')).toBe(false);
  });

  it('renders text "day" when it is more than 1 day to finish', () => {
    const unfinishedDate = new Date();
    unfinishedDate.setDate(unfinishedDate.getDay() + 2);
    const counter = render(<Counter date={unfinishedDate} t={key => key} />);
    expect(counter.text().includes('day')).toBe(true);
    expect(counter.text().includes('second')).toBe(false);
  });

  it('renders text "seconds" when it is less than 1 day to finish', () => {
    const unfinishedDate = new Date();
    unfinishedDate.setMinutes(unfinishedDate.getMinutes() + 60);
    const counter = render(<Counter date={unfinishedDate} t={key => key} />);
    expect(counter.text().includes('second')).toBe(true);
    expect(counter.text().includes('day')).toBe(false);
  });

  it('adds classes based on props', () => {
    const counter = render(<Counter date="2019/06/25 16:26" light large t={key => key} />);
    expect(counter.hasClass('DateCounter--light')).toEqual(true);
    expect(counter.hasClass('DateCounter--large')).toEqual(true);
  });

  it('renders custom title', () => {
    const counter = render(<Counter date="2019/06/25 16:26" title="Custom title" t={key => key}/>);
    expect(counter.find('.DateCounter__title').text()).toEqual('Custom title');
  });
});
