/*
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 { StartEnhancer, EndEnhancer, LoadingSpinner } from '../styled-components.js';
import Button from '../button.js';
describe('Button Component', function () {
  test('basic render', function () {
    var component = mount(React.createElement(Button, null));
    expect(component.find(StartEnhancer)).not.toExist();
    expect(component.find(EndEnhancer)).not.toExist();
    component.setProps({
      startEnhancer: 'start',
      endEnhancer: 'end'
    });
    expect(component.find(StartEnhancer)).toExist();
    expect(component.find(EndEnhancer)).toExist();
  });
  test('renders with components overrides', function () {
    var NewStartEnhancer = function NewStartEnhancer() {
      return React.createElement("div", null);
    };

    var props = {
      startEnhancer: function startEnhancer() {
        return null;
      },
      overrides: {
        StartEnhancer: NewStartEnhancer
      }
    };
    var component = mount(React.createElement(Button, props));
    expect(component.find(StartEnhancer)).not.toExist();
    expect(component.find(NewStartEnhancer)).toExist();
  });
  test('renders with loading spinner', function () {
    var component = mount(React.createElement(Button, null));
    expect(component.find(LoadingSpinner)).not.toExist();
    component.setProps({
      isLoading: true
    });
    expect(component.find(LoadingSpinner)).toExist();
  });
  test('onClick called with event', function () {
    var onClick = jest.fn();
    var component = mount(React.createElement(Button, {
      onClick: onClick
    }));
    component.simulate('click', {
      target: 'foo'
    });
    expect(onClick).toHaveBeenCalledTimes(1);
    expect(onClick.mock.calls[0][0]).toMatchObject({
      target: 'foo'
    });
  });
  test('internalOnClick prevents external onClick while loading', function () {
    var props = {
      onClick: jest.fn(),
      isLoading: true
    };
    var component = mount(React.createElement(Button, props));
    component.instance().internalOnClick();
    expect(props.onClick.mock.calls.length).toBe(0);
    component.setProps({
      isLoading: false
    });
    component.instance().internalOnClick();
    expect(props.onClick.mock.calls.length).toBe(1);
  });
});