-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtestUtils.test.js
More file actions
79 lines (72 loc) · 2.03 KB
/
testUtils.test.js
File metadata and controls
79 lines (72 loc) · 2.03 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
import { render } from '@testing-library/react';
import { Context } from './ContextDemo.tsx'
import {
assertByTestId,
assertProperty,
assertByTextContent,
renderWithContext,
assertMockFunctionArg
} from '../testUtils.tsx';
describe('assertByTestId', () => {
const renderCom = render(<h1 data-testid="hello">Hello</h1>);
test('testId exists', () => {
assertByTestId(renderCom, 'hello', true);
});
test('testId do not exists', () => {
assertByTestId(renderCom, 'Hello', false);
});
});
describe('assertProperty', () => {
test('test property', () => {
const componant = {
name: 'default'
};
assertProperty(componant, 'name', 'default');
});
});
describe('asserByTextContent', () => {
test('assert test content', () => {
const renderedComp = render(<h1>Hello default</h1>);
assertByTextContent(renderedComp, 'Hello default', true);
});
test('assert test content not present', () => {
const renderedComp = render(<h1>Hello default</h1>);
assertByTextContent(renderedComp, 'Not Found', false);
});
});
describe('renderWithContext', () => {
test('assert context state', () => {
const contextData = {
value: 'context'
};
const props = {};
const obj = {
Comp: ContextDemo,
ContextProviderRef: Context.Provider,
state: contextData,
props: props
};
const renderedComp = renderWithContext(obj);
assertByTextContent(renderedComp, 'context', true);
});
});
describe('assertMockFunctionsArgument', () => {
test('test arguments in mock function call', () => {
function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
}
const mockCallback = jest.fn((x) => 42 + x);
forEach([0, 1], mockCallback);
const obj1 = {
mockFunction: mockCallback,
funCallIndex: 1,
argIndex: 0,
argument: 1
};
assertMockFunctionArg(obj1);
const obj2 = { mockFunction: mockCallback, argument: 0 };
assertMockFunctionArg(obj2);
});
});