-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathStudioIconTextfield.test.tsx
45 lines (39 loc) · 1.62 KB
/
StudioIconTextfield.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react';
import { render, screen } from '@testing-library/react';
import { StudioIconTextfield } from './StudioIconTextfield';
import type { StudioIconTextfieldProps } from './StudioIconTextfield';
import { KeyVerticalIcon } from '@studio/icons';
import userEvent from '@testing-library/user-event';
import { testCustomAttributes } from '../../test-utils/testCustomAttributes';
describe('StudioIconTextfield', () => {
it('render the icon', async () => {
renderStudioIconTextfield();
expect(screen.getByRole('img', { hidden: true })).toBeInTheDocument();
});
it('should render label', () => {
renderStudioIconTextfield();
expect(screen.getByLabelText(label)).toBeInTheDocument();
});
it('should execute onChange callback when input value changes', async () => {
const user = userEvent.setup();
renderStudioIconTextfield();
const input = screen.getByRole('textbox', { name: label });
const inputValue = 'my id is 123';
await user.type(input, inputValue);
expect(onChange).toHaveBeenCalledTimes(inputValue.length);
});
it('should forward the rest of the props to the input', () => {
const getTextbox = (): HTMLInputElement => screen.getByRole('textbox') as HTMLInputElement;
testCustomAttributes<HTMLInputElement>(renderStudioIconTextfield, getTextbox);
});
});
const label = 'label';
const onChange = jest.fn();
const defaultProps: StudioIconTextfieldProps = {
Icon: KeyVerticalIcon,
label,
onChange,
};
const renderStudioIconTextfield = (props: Partial<StudioIconTextfieldProps> = {}) => {
return render(<StudioIconTextfield {...defaultProps} {...props} />);
};