/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import { mount } from 'enzyme';
import { Toast, StyledBody, StyledCloseIcon, KIND } from '../index.js';
jest.useFakeTimers();
describe('Toast', function () {
  test('basic toast functionality', function () {
    var wrapper = mount(React.createElement(Toast, null, "Notification"));
    expect(wrapper.instance().state.isRendered).toBe(true);
    expect(wrapper.instance().state.isVisible).toBe(false);
    expect(wrapper.find(StyledBody).first()).toExist();
    expect(wrapper.find(StyledCloseIcon).first()).toExist();
  });
  test('Body component props', function () {
    var renderedRoot;
    var wrapper = mount(React.createElement(Toast, null, "Notification"));
    renderedRoot = wrapper.find(StyledBody).first();
    expect(renderedRoot).toExist();
    expect(renderedRoot.props().$kind).toBe(KIND.info);
    expect(renderedRoot.props().$closeable).toBe(true);
    expect(renderedRoot.props().$isRendered).toBe(true);
    expect(renderedRoot.props().$isVisible).toBe(false); // pass new kind value set to KIND.positive
    // and closeable set to false

    wrapper.setProps({
      kind: KIND.positive,
      closeable: false
    });
    renderedRoot = wrapper.find(StyledBody).first();
    expect(renderedRoot.props().$kind).toBe(KIND.positive);
    expect(renderedRoot.props().$closeable).toBe(false); // pass new kind value set to KIND.warning

    wrapper.setProps({
      kind: KIND.warning
    });
    renderedRoot = wrapper.find(StyledBody).first();
    expect(renderedRoot.props().$kind).toBe(KIND.warning); // pass new kind value set to KIND.negative

    wrapper.setProps({
      kind: KIND.negative
    });
    renderedRoot = wrapper.find(StyledBody).first();
    expect(renderedRoot.props().$kind).toBe(KIND.negative);
  });
  test('close icon rendering', function () {
    var renderedCloseButton;
    var wrapper = mount(React.createElement(Toast, null, "Notification"));
    renderedCloseButton = wrapper.find(StyledCloseIcon).first();
    expect(renderedCloseButton).toExist(); // pass new closeable value set to false

    wrapper.setProps({
      closeable: false
    });
    renderedCloseButton = wrapper.find(StyledCloseIcon).first();
    expect(renderedCloseButton).not.toExist();
  });
  test('close icon props', function () {
    var renderedCloseButton;
    var wrapper = mount(React.createElement(Toast, null, "Notification"));
    renderedCloseButton = wrapper.find(StyledCloseIcon).first();
    expect(renderedCloseButton.props().onClick).toBe(wrapper.instance().dismiss);
    expect(renderedCloseButton.props().$kind).toBe(KIND.info); // pass new kind value set to KIND.positive

    wrapper.setProps({
      kind: KIND.positive
    });
    renderedCloseButton = wrapper.find(StyledCloseIcon).first();
    expect(renderedCloseButton.props().$kind).toBe(KIND.positive); // pass new kind value set to KIND.warning

    wrapper.setProps({
      kind: KIND.warning
    });
    renderedCloseButton = wrapper.find(StyledCloseIcon).first();
    expect(renderedCloseButton.props().$kind).toBe(KIND.warning); // pass new kind value set to KIND.negative

    wrapper.setProps({
      kind: KIND.negative
    });
    renderedCloseButton = wrapper.find(StyledCloseIcon).first();
    expect(renderedCloseButton.props().$kind).toBe(KIND.negative);
  });
  test('onClose handler', function () {
    var props = {
      onClose: jest.fn()
    };
    var wrapper = mount(React.createElement(Toast, props, "Notification"));
    var closeButton = wrapper.find(StyledCloseIcon).first();
    expect(closeButton).toExist();
    closeButton.simulate('click');
    expect(wrapper.instance().state.isRendered).toBe(true);
    expect(wrapper.instance().state.isVisible).toBe(false);
    expect(props.onClose).not.toHaveBeenCalled();
    jest.runAllTimers();
    expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 600);
    expect(wrapper.instance().state.isRendered).toBe(false);
    expect(props.onClose).toHaveBeenCalled();
  });
  test('onMouseEnter handler', function () {
    var props = {
      onMouseEnter: jest.fn()
    };
    var wrapper = mount(React.createElement(Toast, props, "Notification"));
    var renderedRoot = wrapper.find(StyledBody).first();
    var toastOnMouseEnterHandler = wrapper.instance().onMouseEnter;
    expect(renderedRoot.props().onMouseEnter).toBe(toastOnMouseEnterHandler);
    expect(renderedRoot.props().onMouseEnter).not.toBe(props.onMouseEnter);
    toastOnMouseEnterHandler({});
    expect(props.onMouseEnter).toHaveBeenCalled();
  });
  test('onMouseLeave handler', function () {
    var props = {
      onMouseLeave: jest.fn()
    };
    var wrapper = mount(React.createElement(Toast, props, "Notification"));
    var renderedRoot = wrapper.find(StyledBody).first();
    var toastOnMouseLeaveHandler = wrapper.instance().onMouseLeave;
    expect(renderedRoot.props().onMouseLeave).not.toBe(props.onMouseLeave);
    expect(renderedRoot.props().onMouseLeave).toBe(toastOnMouseLeaveHandler);
    toastOnMouseLeaveHandler({});
    expect(props.onMouseLeave).toHaveBeenCalled();
  });
  test('onFocus handler', function () {
    var props = {
      onFocus: jest.fn()
    };
    var wrapper = mount(React.createElement(Toast, props, "Notification"));
    var renderedRoot = wrapper.find(StyledBody).first();
    var toastOnFocusHandler = wrapper.instance().onFocus;
    expect(renderedRoot.props().onFocus).not.toBe(props.onFocus);
    expect(renderedRoot.props().onFocus).toBe(toastOnFocusHandler);
    toastOnFocusHandler({});
    expect(props.onFocus).toHaveBeenCalled();
  });
  test('onBlur handler', function () {
    var props = {
      onBlur: jest.fn()
    };
    var wrapper = mount(React.createElement(Toast, props, "Notification"));
    var renderedRoot = wrapper.find(StyledBody).first();
    var toastOnBlurHandler = wrapper.instance().onBlur;
    expect(renderedRoot.props().onBlur).not.toBe(props.onBlur);
    expect(renderedRoot.props().onBlur).toBe(toastOnBlurHandler);
    toastOnBlurHandler({});
    expect(props.onBlur).toHaveBeenCalled();
  });
  test('component overrides', function () {
    var Override = function Override(_ref) {
      var children = _ref.children;
      return React.createElement("span", null, children);
    };

    var overrides = {
      Body: jest.fn().mockImplementation(Override),
      CloseIcon: {
        props: {
          size: '54px'
        },
        style: {
          color: 'red'
        },
        component: Override
      }
    }; // $FlowFixMe

    var wrapper = mount(React.createElement(Toast, {
      overrides: overrides
    }, "Notification"));
    var bodyOverride = wrapper.find(overrides.Body);
    expect(bodyOverride).toHaveLength(1);
    var closeIconOverride = wrapper.find(overrides.CloseIcon.component);
    expect(closeIconOverride).toHaveLength(1);
    expect(closeIconOverride.props().$size).toBe('54px');
    expect(closeIconOverride.props().$style).toBe(overrides.CloseIcon.style);
  });
});