-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathApp.test.tsx
110 lines (91 loc) · 4.59 KB
/
App.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as React from 'react';
import { render, screen, fireEvent } from '@testing-library/react-native';
import App from '../App';
/**
* A good place to start is having a tests that your component renders correctly.
*/
test('renders correctly', () => {
// Idiom: no need to capture render output, as we will use `screen` for queries.
render(<App />);
// Idiom: `getByXxx` is a predicate by itself, but we will use it with `expect().toBeTruthy()`
// to clarify our intent.
expect(screen.getByText('Sign in to Example App')).toBeTruthy();
});
/**
* Hint: It's best when your tests are similar to what a manual test scenarions would look like,
* i.e. a series of actions taken by the user, followed by a series of assertions verified from
* his point of view.
*/
test('User can sign in successully with correct credentials', async () => {
// Idiom: no need to capture render output, as we will use `screen` for queries.
render(<App />);
// Idiom: `getByXxx` is a predicate by itself, but we will use it with `expect().toBeTruthy()` to
// clarify our intent.
// Note: `.toBeTruthy()` is the preferred matcher for checking that elements are present.
expect(screen.getByText('Sign in to Example App')).toBeTruthy();
expect(screen.getByText('Username')).toBeTruthy();
expect(screen.getByText('Password')).toBeTruthy();
// Hint: we can use `getByLabelText` to find our text inputs in accessibility-friendly way.
fireEvent.changeText(screen.getByLabelText('Username'), 'admin');
fireEvent.changeText(screen.getByLabelText('Password'), 'admin1');
// Hint: we can use `getByText` to find our button by its text.
fireEvent.press(screen.getByText('Sign In'));
// Idiom: since pressing button triggers async operation we need to use `findBy` query to wait
// for the action to complete.
// Hint: subsequent queries do not need to use `findBy`, because they are used after the async action
// already finished
expect(await screen.findByText('Welcome admin!')).toBeTruthy();
// Idiom: use `queryByXxx` with `expect().toBeFalsy()` to assess that element is not present.
expect(screen.queryByText('Sign in to Example App')).toBeFalsy();
expect(screen.queryByText('Username')).toBeFalsy();
expect(screen.queryByText('Password')).toBeFalsy();
});
/**
* Another test case based on manual test scenario.
*
* Hint: Try to tests what a user would see and do, instead of assering internal component state
* that is not directly reflected in the UI.
*
* For this reason prefer quries that correspond to things directly observable by the user like:
* `getByText`, `getByLabelText`, `getByPlaceholderText, `getByDisplayValue`, `getByRole`, etc.
* over `getByTestId` which is not directly observable by the user.
*
* Note: that some times you will have to resort to `getByTestId`, but treat it as a last resort.
*/
test('User will see errors for incorrect credentials', async () => {
render(<App />);
expect(screen.getByText('Sign in to Example App')).toBeTruthy();
expect(screen.getByText('Username')).toBeTruthy();
expect(screen.getByText('Password')).toBeTruthy();
fireEvent.changeText(screen.getByLabelText('Username'), 'admin');
fireEvent.changeText(screen.getByLabelText('Password'), 'qwerty123');
fireEvent.press(screen.getByText('Sign In'));
// Hint: you can use custom Jest Native matcher to check text content.
expect(await screen.findByLabelText('Error')).toHaveTextContent(
'Incorrect username or password'
);
expect(screen.getByText('Sign in to Example App')).toBeTruthy();
expect(screen.getByText('Username')).toBeTruthy();
expect(screen.getByText('Password')).toBeTruthy();
});
/**
* Do not be afraid to write longer test scenarios, with repeating act and assert statements.
*/
test('User can sign in after incorrect attempt', async () => {
render(<App />);
expect(screen.getByText('Sign in to Example App')).toBeTruthy();
expect(screen.getByText('Username')).toBeTruthy();
expect(screen.getByText('Password')).toBeTruthy();
fireEvent.changeText(screen.getByLabelText('Username'), 'admin');
fireEvent.changeText(screen.getByLabelText('Password'), 'qwerty123');
fireEvent.press(screen.getByText('Sign In'));
expect(await screen.findByLabelText('Error')).toHaveTextContent(
'Incorrect username or password'
);
fireEvent.changeText(screen.getByLabelText('Password'), 'admin1');
fireEvent.press(screen.getByText('Sign In'));
expect(await screen.findByText('Welcome admin!')).toBeTruthy();
expect(screen.queryByText('Sign in to Example App')).toBeFalsy();
expect(screen.queryByText('Username')).toBeFalsy();
expect(screen.queryByText('Password')).toBeFalsy();
});