import React from 'react';
import { mount } from 'enzyme';

import TutorialTile from '.';

describe('TutorialTile', () => {
  let node;
  let props;

  beforeEach(() => {
    props = {
      openTutorial: jest.fn(),
      tutorial: {
        backgroundColor: 'fff',
        description: 'test tutorial desc',
        title: 'test tutorial title',
        emoji: '🦉',
        _id: '123',
      },
    };
    node = mount(<TutorialTile {...props} />);
  });

  it('should display the TutorialTile component', () => {
    expect(node.find('.TutorialTile').hasClass('TutorialTile')).toBe(true);
  });

  it('should handle button click event', () => {
    node.simulate('click');
    expect(props.openTutorial).toHaveBeenCalledWith({ tutorial: props.tutorial });
  });

  it('should handle keydown event', () => {
    node.simulate('keydown', { keyCode: 13, stopPropagation: jest.fn() });
    expect(props.openTutorial).toHaveBeenCalledWith({ tutorial: props.tutorial });
  });

  describe('callout', () => {
    let calloutNode;
    let calloutProps;

    beforeEach(() => {
      calloutProps = {
        tutorial: {
          backgroundColor: 'fff',
          description: 'test tutorial desc',
          title: 'test tutorial title',
          emoji: '🦉',
          _id: '123',
        },
        status: 'New',
      };
      calloutNode = mount(<TutorialTile {...calloutProps}>Only for admins!</TutorialTile>);
    });

    it('should display a callout of given children', () => {
      const n = !!calloutNode.find('.TutorialTile').find('.TutorialTile-Notification');
      expect(n).toBe(true);
    });

    it('should dismiss callout display onclick', () => {
      const button = calloutNode.find('.TutorialTile').find('button');
      button.simulate('click');

      expect(calloutNode.find('.TutorialTile').hasClass('TutorialTile-Notification')).toBe(false);
    });

    it('should not open tutorial on dismiss click or enter', () => {
      const div = calloutNode.find('.TutorialTile').find('.TutorialTile-Notification');
      const spy = jest.fn();
      div.simulate('keydown', { keyCode: 13, stopPropagation: spy });

      expect(spy).toHaveBeenCalledTimes(1);
    });
  });
});
