-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcommon.tsx
More file actions
97 lines (77 loc) · 2.53 KB
/
common.tsx
File metadata and controls
97 lines (77 loc) · 2.53 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React, {ElementType} from 'react';
import {QAProps} from '../../src/models/common';
import {ERROR_INPUT_DATA_MESSAGE} from '../constants';
type CommonTestArgs<T> = {
component: ElementType;
props: T & QAProps;
options?: {
qaId?: string;
customClassNameProp?: string;
role?: string;
handlerName?: string;
};
};
const validateInputProps = <T,>(props: CommonTestArgs<T>['props']) => {
if (!props.qa) {
throw new Error(ERROR_INPUT_DATA_MESSAGE);
}
};
const getComponent = <T,>({props, options}: Omit<CommonTestArgs<T>, 'component'>) => {
if (!props.qa) {
throw new Error(ERROR_INPUT_DATA_MESSAGE);
}
return options?.role
? screen.getByRole(options.role)
: screen.getByTestId(options?.qaId || props.qa);
};
export const testCustomClassName = <T,>({
component: Component,
props,
options,
}: CommonTestArgs<T>) => {
validateInputProps(props);
const className = 'custom-class-name';
const classNameProps = {
[options?.customClassNameProp || 'className']: className,
};
render(<Component {...props} {...classNameProps} />);
const component = getComponent({props, options});
expect(component).toHaveClass(className);
};
export const testCustomStyle = <T,>({component: Component, props, options}: CommonTestArgs<T>) => {
validateInputProps(props);
const style = {color: 'red'};
render(<Component {...props} style={style} />);
const component = getComponent({props, options});
expect(component).toHaveStyle(style);
};
export const testAnimated = async <T,>({
component: Component,
props,
options,
}: CommonTestArgs<T>) => {
validateInputProps(props);
const user = userEvent.setup();
render(<Component animated {...props} />);
const component = getComponent({props, options});
await user.hover(component);
expect(component).toHaveClass('animate');
};
export const testOnClick = async <T,>({
component: Component,
props,
options,
}: CommonTestArgs<T>) => {
validateInputProps(props);
const user = userEvent.setup();
const handleOnClick = jest.fn();
const handlerObject = {
[options?.handlerName || 'onClick']: handleOnClick,
};
render(<Component {...props} {...handlerObject} />);
const component = getComponent({props, options});
await user.click(component);
expect(handleOnClick).toHaveBeenCalledTimes(1);
};