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

import TutorialCard from '.';
import { PermissionContext } from '@views/Tutorials';

describe('TutorialCard', () => {
  describe('not admin', () => {
    let node;

    beforeEach(() => {
      const props = {
        numTutorials: 3,
        deleteTutorial: jest.fn(),
        makeHero: jest.fn(),
        shiftOrder: jest.fn(),
        publish: jest.fn(),
        tutorial: {
          backgroundColor: '#fff',
          description: 'test tutorial desc',
          title: 'test tutorial title',
          hero: false,
          stepCount: 3,
          published: false,
          emoji: '🦉',
        },
      };
      node = mount(<TutorialCard {...props} />);
    });

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

    it('should not display button bar without admin privelages', () => {
      const buttonBar = node.find('.TutorialCard-Toolbar-Button');
      expect(buttonBar.find('button').exists()).toBe(false);
    });
  });

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

    beforeEach(() => {
      props = {
        numTutorials: 3,
        deleteTutorial: jest.fn(),
        makeHero: jest.fn(),
        shiftOrder: jest.fn(),
        publish: jest.fn(),
        tutorial: {
          _id: '123',
          backgroundColor: '#fff',
          description: 'test tutorial desc',
          title: 'test tutorial title',
          hero: false,
          stepCount: 3,
          published: true,
          emoji: '',
        },
      };
      node = mount(
        <PermissionContext.Provider value={true}>
          <TutorialCard {...props} />
        </PermissionContext.Provider>
      );
    });

    it('should display the button bar with admin', () => {
      const buttonBar = node.find('.TutorialCard-Toolbar');
      expect(buttonBar.find('button').exists()).toBe(true);
    });

    describe('button actions', () => {
      it('shiftLeft', () => {
        const buttonBar = node.find('.TutorialCard-Toolbar-Arrow');
        const button = buttonBar.find('button').first();

        button.simulate('click');
        expect(props.shiftOrder).toHaveBeenCalledWith({ _id: '123', direction: -1 });
      });

      it('shiftRight', () => {
        const buttonBar = node.find('.TutorialCard-Toolbar-Arrow');
        const button = buttonBar.find('button').last();

        button.simulate('click');
        expect(props.shiftOrder).toHaveBeenCalledWith({ _id: '123', direction: 1 });
      });

      it('togglePublish', () => {
        const buttonBar = node.find('.TutorialCard-Toolbar-Button');
        const button = buttonBar.find('button').at(1);

        button.simulate('click');
        expect(props.publish).toHaveBeenCalledWith({ _id: '123', published: false });
      });

      it('makeHero', () => {
        const buttonBar = node.find('.TutorialCard-Toolbar-Button');
        const button = buttonBar.find('button').at(2);

        button.simulate('click');
        expect(props.makeHero).toHaveBeenCalledWith({ _id: '123', published: true });
      });

      it('deleteTutorial', () => {
        const buttonBar = node.find('.TutorialCard-Toolbar-Button');
        const button = buttonBar.find('button').last();

        button.simulate('click');
        expect(props.deleteTutorial).toHaveBeenCalledWith({ _id: '123', title: 'test tutorial title' });
      });
    });

    describe('status', () => {
      it('should show a notification if admin + non-published status', () => {
        const p2 = {
          numTutorials: 3,
          deleteTutorial: jest.fn(),
          makeHero: jest.fn(),
          shiftOrder: jest.fn(),
          publish: jest.fn(),
          tutorial: {
            _id: '123',
            backgroundColor: '#fff',
            description: 'test tutorial desc',
            title: 'test tutorial title',
            hero: true,
            stepCount: 3,
            published: true,
            emoji: '',
          },
        };
        const n2 = mount(
          <PermissionContext.Provider value={true}>
            <TutorialCard {...p2} />
          </PermissionContext.Provider>
        );

        const status = n2.find('.TutorialCard-Status');
        expect(status.exists()).toBe(true);
      });
    });
  });
});
