// @flow
import React from 'react';
import { mountWrappedComponent } from '../../../../utils/tests';

import type { TextInputProps } from './text-input.proptypes';
import {
  TextInput,
  ErrorLabelWithMargin,
  FullWidthFormInput,
} from './text-input.component';

const DEFAULT_PROPS: TextInputProps<*> = {
  label: 'test label',
  onChange: jest.fn(),
  value: '',
  iconType: 'error',
  iconPosition: 'left',
  placeholder: 'placeholder',
  onFocus: jest.fn(),
  onBlur: jest.fn(),
  large: false,
  disabled: false,
  error: false,
  focusOnMount: false,
};

describe('TextInput', () => {
  it('renders error text if given a error string', () => {
    const errorString = 'oh no there is an error';
    const textInput = mountWrappedComponent(
      <TextInput {...DEFAULT_PROPS} error={errorString} />,
    );

    const errorLabel = textInput.find(ErrorLabelWithMargin);
    expect(errorLabel.children().text()).toBe(errorString);
  });

  it('does not render error text if given a boolean', () => {
    const textInput = mountWrappedComponent(
      <TextInput {...DEFAULT_PROPS} error />,
    );

    const errorLabel = textInput.find(ErrorLabelWithMargin);
    expect(errorLabel.exists()).toBe(false);
  });

  it('renders error text if given a error array', () => {
    const error = ['oh no there is an error', 'oh no there is another error'];
    const errorString = 'oh no there is an error, oh no there is another error';

    const textInput = mountWrappedComponent(
      <TextInput {...DEFAULT_PROPS} error={error} />,
    );

    const errorLabel = textInput.find(ErrorLabelWithMargin);
    expect(errorLabel.children().text()).toBe(errorString);
  });

  it('renders an error icon if there is an error', () => {
    const textInput = mountWrappedComponent(
      <TextInput {...DEFAULT_PROPS} error />,
    );

    expect(textInput.find(FullWidthFormInput).props()).toEqual(
      expect.objectContaining({
        iconType: 'error',
        iconPosition: 'right',
      }),
    );
  });
});
