-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtestUtils.tsx
53 lines (48 loc) · 1.24 KB
/
testUtils.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
46
47
48
49
50
51
52
53
import { render, RenderResult } from '@testing-library/react';
import {
AssertMockFunctionParams,
ContextRenderParams
} from './types';
export const assertByTestId = (
renderedComp: RenderResult,
testId: string,
isTruthy: boolean
) => {
if (!isTruthy) {
return expect(renderedComp.queryAllByTestId(testId).length).toBe(0);
}
return expect(renderedComp.getByTestId(testId)).toBeTruthy();
};
export const assertByTextContent = (
renderedComp: RenderResult,
textContent: string,
isTruthy: boolean
) => {
if (!isTruthy) {
return expect(renderedComp.queryByText(textContent)).toBeNull();
}
return expect(renderedComp.getByText(textContent)).toBeInTheDocument();
};
export const assertProperty = (obj: Object, key: string, val: any) => {
return expect(obj).toHaveProperty(key, val);
};
export const renderWithContext = ({
Comp,
ContextProviderRef,
state,
props
}: ContextRenderParams) => {
return render(
<ContextProviderRef value={state}>
<Comp {...props} />
</ContextProviderRef>
);
};
export const assertMockFunctionArg = ({
mockFunction,
funCallIndex = 0,
argIndex = 0,
argument
}: AssertMockFunctionParams) => {
return expect(mockFunction.mock.calls[funCallIndex][argIndex]).toBe(argument);
};