import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { TextInput, TextInputBase } from '../TextInput';
import { ValidatedOptions } from '../../../helpers/constants';
const props = {
onChange: jest.fn(),
value: 'test input'
};
describe('TextInput', () => {
test('input passes value and event to onChange handler', async () => {
const user = userEvent.setup();
render();
await user.type(screen.getByLabelText('test input'), 'a');
expect(props.onChange).toHaveBeenCalledWith(expect.any(Object), 'a');
});
test('simple text input', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('disabled text input', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('read only text input using isReadOnly', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('default read only text input', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('plain read only text input', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('invalid text input', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('validated text input success', () => {
render();
expect(screen.getByLabelText('validated text input').parentElement).toHaveClass('pf-m-success');
});
test('validated text input warning', () => {
render();
expect(screen.getByLabelText('validated text input').parentElement).toHaveClass('pf-m-warning');
});
test('validated text input error', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('should throw console error when no aria-label, id or aria-labelledby is given', () => {
const myMock = jest.fn();
global.console = { ...global.console, error: myMock };
render();
expect(myMock).toHaveBeenCalled();
});
test('should not throw console error when id is given but no aria-label or aria-labelledby', () => {
const myMock = jest.fn();
global.console = { ...global.console, error: myMock };
render();
expect(myMock).not.toHaveBeenCalled();
});
test('should not throw console error when aria-label is given but no id or aria-labelledby', () => {
const myMock = jest.fn();
global.console = { ...global.console, error: myMock };
render();
expect(myMock).not.toHaveBeenCalled();
});
test('should not throw console error when aria-labelledby is given but no id or aria-label', () => {
const myMock = jest.fn();
global.console = { ...global.console, error: myMock };
render();
expect(myMock).not.toHaveBeenCalled();
});
});