import { shallow } from 'enzyme';
import { useIntl } from 'react-intl';

import TableLink from '.';

jest.mock('react-intl');

jest.mock('../DateLookup.messages', () => ({
  selected: {
    id: 'neptune.DateLookup.selected',
    defaultMessage: 'selected',
  },
  day: {
    id: 'neptune.DateLookup.day',
    defaultMessage: 'day',
  },
  month: {
    id: 'neptune.DateLookup.month',
    defaultMessage: 'month',
  },
  year: {
    id: 'neptune.DateLookup.year',
    defaultMessage: 'year',
  },
}));

describe('TableLink', () => {
  let component;
  let props;

  beforeEach(() => {
    useIntl.mockReturnValue({
      formatMessage: (message) => message.defaultMessage,
    });
    props = {
      item: 12,
      type: 'day',
      active: false,
      disabled: false,
      today: false,
      onClick: jest.fn(),
      title: '12',
      longTitle: '12/12/1212',
    };
    component = shallow(<TableLink {...props} />);
  });

  it('shows item value', () => {
    expect(button().text()).toBe('12');
  });

  it('shows title when provided (instead of item value)', () => {
    component.setProps({ title: 'five' });
    expect(button().text()).toBe('five');
  });

  it('disables the link', () => {
    component.setProps({ disabled: true });
    expect(button().prop('disabled')).toBe(true);
    expect(props.onClick).not.toHaveBeenCalled();
    button().simulate('click', { preventDefault: () => {} });
    expect(props.onClick).not.toHaveBeenCalled();
  });

  it('highlights active link', () => {
    expect(button().hasClass('active')).toBe(false);
    component.setProps({ active: true });
    expect(button().hasClass('active')).toBe(true);
  });

  it('highlights today', () => {
    expect(button().hasClass('today')).toBe(false);
    component.setProps({ today: true });
    expect(button().hasClass('today')).toBe(true);
  });

  it('calls click handler on click', () => {
    button().simulate('click', { preventDefault: () => {} });
    expect(props.onClick).toHaveBeenCalledWith(12);
  });

  it('adds aria label to selected day', () => {
    expect(button().hasClass('active')).toBe(false);
    component.setProps({ active: true });
    expect(button().prop('aria-label')).toBe('12/12/1212, selected day');
  });

  it('adds aria pressed to selected day', () => {
    expect(button().hasClass('active')).toBe(false);
    component.setProps({ active: true });
    expect(button().prop('aria-pressed')).toBe(true);
  });

  it('adds aria label to selected month', () => {
    component.setProps({ type: 'month' });
    expect(button().hasClass('active')).toBe(false);
    component.setProps({ active: true });
    expect(button().prop('aria-label')).toBe('12/12/1212, selected month');
  });

  it('adds aria label to selected year', () => {
    component.setProps({ type: 'year' });
    expect(button().hasClass('active')).toBe(false);
    component.setProps({ active: true });
    expect(button().prop('aria-label')).toBe('12/12/1212, selected year');
  });

  const button = () => component.find('button');
});
