import React from 'react';
import '@testing-library/jest-dom';
import { fireEvent, render } from "@testing-library/react-native";
import { OrganismPromoCode } from './index';

describe('[React Native] PromoCode', () => {
  it('render component', () => {
    const mockHandler = jest.fn();
    const props = {
      headline: 'Title',
      text: 'This is a text',
      handlePress: mockHandler,
      errorMessageText: 'error'
    };
    const component = render(<OrganismPromoCode {...props} />);
    expect(component).toBeTruthy();
  });
  it('render custom icon', () => {
    const mockHandler = jest.fn();
    const props = {
      headline: 'Title',
      text: 'This is a text',
      iconName: 'beer',
      handlePress: mockHandler,
      errorMessageText: 'error'
    };
    const component = render(<OrganismPromoCode {...props} />);
    expect(component).toBeTruthy();
  });
  it('special characters and characters', () => {
    const mockHandler = jest.fn();
    const props = {
      headline: 'Title',
      text: 'This is a text',
      iconName: 'beer',
      handlePress: mockHandler,
      errorMessageText: 'error'
    };
    const component = render(<OrganismPromoCode {...props} />);
    const input = component.queryByTestId('input')
    fireEvent(input, 'focus');
    fireEvent.changeText(input, '.a!`lx!1.');
    fireEvent(input, 'blur');
    component.getByText(props.errorMessageText);
  })

  it('type only special characters', () => {
    const mockHandler = jest.fn();
    const props = {
      headline: 'Title',
      text: 'This is a text',
      iconName: 'beer',
      handlePress: mockHandler,
      errorMessageText: 'error'
    };
    const component = render(<OrganismPromoCode {...props} />);
    const input = component.queryByTestId('input')
    fireEvent(input, 'focus');
    fireEvent.changeText(input, '.!+`]./');
    component.getByText(props.errorMessageText);
  });

  it('press button', () => {
    const mockHandler = jest.fn();
    const props = {
      headline: 'Title',
      text: 'This is a text',
      iconName: 'beer',
      handlePress: mockHandler,
      errorMessageText: 'error',
      value: 'value-test',
    };

    const component = render(<OrganismPromoCode {...props} />);
    const input = component.queryByTestId('input')
    fireEvent(input, 'focus');
    fireEvent.changeText(input, 'P002EJ09Q2CYV43S');
    fireEvent(input, 'blur');
    const button = component.queryByTestId('df-btn');
    fireEvent.press(button);
    expect(mockHandler).toHaveBeenCalled();
  });

  it("When the code is deleted the button must be active and doesn't show any error message", () => {
    const mockHandler = jest.fn();
    const props = {
      headline: 'Title',
      text: 'This is a text',
      iconName: 'beer',
      handlePress: mockHandler,
      errorMessageText: 'error'
    } as const;

    const component = render(<OrganismPromoCode {...props} />);
    const input = component.queryByTestId('input')
    const button = component.queryByTestId('df-btn');
    fireEvent.changeText(input, 'aa');
    fireEvent.changeText(input, '');
    //button not disabled
    expect(button.props.accessibilityState.disabled).not.toBeTruthy();
    //not showing error message
    const errorMessage = component.queryByText(props.errorMessageText);
    expect(errorMessage===null || errorMessage.props.color==='transparent').toBeTruthy();
  });
});
