Added render tests for common components

no issue

- Adds tests for ActionButton and InputField components
This commit is contained in:
Rish 2020-04-27 15:37:48 +05:30
parent eb4ed70303
commit 1a29d0926e
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,34 @@
import React from 'react';
import {render, fireEvent} from '@testing-library/react';
import ActionButton from './ActionButton';
const setup = (overrides) => {
const mockOnClickFn = jest.fn();
const props = {
label: 'Test Action Button', onClick: mockOnClickFn, disabled: false
};
const utils = render(
<ActionButton {...props} />
);
const buttonEl = utils.queryByRole('button', {name: props.label});
return {
buttonEl,
mockOnClickFn,
...utils
};
};
describe('ActionButton', () => {
test('renders', () => {
const {buttonEl} = setup();
expect(buttonEl).toBeInTheDocument();
});
test('fires onClick', () => {
const {buttonEl, mockOnClickFn} = setup();
fireEvent.click(buttonEl);
expect(mockOnClickFn).toHaveBeenCalled();
});
});

View File

@ -0,0 +1,38 @@
import React from 'react';
import {render, fireEvent} from '@testing-library/react';
import InputField from './InputField';
const setup = (overrides = {}) => {
const mockOnChangeFn = jest.fn();
const props = {
name: 'test-input',
label: 'Test Input',
value: '',
placeholder: 'Test placeholder',
onChange: mockOnChangeFn
};
const utils = render(
<InputField {...props} />
);
const inputEl = utils.getByLabelText(props.label);
return {
inputEl,
mockOnChangeFn,
...utils
};
};
describe('InputField', () => {
test('renders', () => {
const {inputEl} = setup();
expect(inputEl).toBeInTheDocument();
});
test('calls onChange on value', () => {
const {inputEl, mockOnChangeFn} = setup();
fireEvent.change(inputEl, {target: {value: 'Test'}});
expect(mockOnChangeFn).toHaveBeenCalled();
});
});