|
| 1 | +import * as React from 'react'; |
| 2 | +import { render, screen, userEvent } from '@testing-library/react-native'; |
| 3 | +import App from '../App'; |
| 4 | + |
| 5 | +jest.useFakeTimers(); |
| 6 | + |
| 7 | +/** |
| 8 | + * A good place to start is having a tests that your component renders correctly. |
| 9 | + */ |
| 10 | +test('renders correctly', () => { |
| 11 | + // Idiom: no need to capture render output, as we will use `screen` for queries. |
| 12 | + render(<App />); |
| 13 | + |
| 14 | + // Idiom: `getBy*` queries are predicates by themselves, but we will use it with `expect().toBeOnTheScreen()` |
| 15 | + // to clarify our intent. |
| 16 | + expect(screen.getByRole('header', { name: 'Sign in to Example App' })).toBeOnTheScreen(); |
| 17 | +}); |
| 18 | + |
| 19 | +/** |
| 20 | + * Hint: It's best when your tests are similar to what a manual test scenarions would look like, |
| 21 | + * i.e. a series of actions taken by the user, followed by a series of assertions verified from |
| 22 | + * his point of view. |
| 23 | + */ |
| 24 | +test('User can sign in successully with correct credentials', async () => { |
| 25 | + // Setup User Event instance for realistic simulation of user interaction. |
| 26 | + const user = userEvent.setup(); |
| 27 | + |
| 28 | + // Idiom: no need to capture render output, as we will use `screen` for queries. |
| 29 | + render(<App />); |
| 30 | + |
| 31 | + // Idiom: `getBy*` queries are predicates by themselves, but we will use it with `expect().toBeOnTheScreen()` |
| 32 | + // to clarify our intent. |
| 33 | + expect(screen.getByRole('header', { name: 'Sign in to Example App' })).toBeOnTheScreen(); |
| 34 | + |
| 35 | + // Hint: we can use `getByLabelText` to find our text inputs using their labels. |
| 36 | + await user.type(screen.getByLabelText('Username'), 'admin'); |
| 37 | + await user.type(screen.getByLabelText('Password'), 'admin1'); |
| 38 | + |
| 39 | + // Hint: we can use `getByRole` to find our button with given text. |
| 40 | + await user.press(screen.getByRole('button', { name: 'Sign In' })); |
| 41 | + |
| 42 | + // Idiom: since pressing button triggers async operation we need to use `findBy*` query to wait |
| 43 | + // for the action to complete. |
| 44 | + // Hint: subsequent queries do not need to use `findBy*`, because they are used after the async action |
| 45 | + // already finished |
| 46 | + expect(await screen.findByRole('header', { name: 'Welcome admin!' })).toBeOnTheScreen(); |
| 47 | + |
| 48 | + // Idiom: use `queryBy*` with `expect().not.toBeOnTheScreen()` to assess that element is not present. |
| 49 | + expect(screen.queryByRole('header', { name: 'Sign in to Example App' })).not.toBeOnTheScreen(); |
| 50 | + expect(screen.queryByLabelText('Username')).not.toBeOnTheScreen(); |
| 51 | + expect(screen.queryByLabelText('Password')).not.toBeOnTheScreen(); |
| 52 | +}); |
| 53 | + |
| 54 | +/** |
| 55 | + * Another test case based on manual test scenario. |
| 56 | + * |
| 57 | + * Hint: Try to tests what a user would see and do, instead of assering internal component state |
| 58 | + * that is not directly reflected in the UI. |
| 59 | + * |
| 60 | + * For this reason prefer quries that correspond to things directly observable by the user like: |
| 61 | + * `getByRole`, `getByText`, `getByLabelText`, `getByPlaceholderText, `getByDisplayValue`, etc. |
| 62 | + * over `getByTestId` which is not directly observable by the user. |
| 63 | + * |
| 64 | + * Note: that some times you will have to resort to `getByTestId`, but treat it as a last resort. |
| 65 | + */ |
| 66 | +test('User will see errors for incorrect credentials', async () => { |
| 67 | + const user = userEvent.setup(); |
| 68 | + render(<App />); |
| 69 | + |
| 70 | + expect(screen.getByRole('header', { name: 'Sign in to Example App' })).toBeOnTheScreen(); |
| 71 | + |
| 72 | + await user.type(screen.getByLabelText('Username'), 'admin'); |
| 73 | + await user.type(screen.getByLabelText('Password'), 'qwerty123'); |
| 74 | + await user.press(screen.getByRole('button', { name: 'Sign In' })); |
| 75 | + |
| 76 | + // Hint: you can use custom Jest Native matcher to check text content. |
| 77 | + expect(await screen.findByRole('alert')).toHaveTextContent('Incorrect username or password'); |
| 78 | + |
| 79 | + expect(screen.getByRole('header', { name: 'Sign in to Example App' })).toBeOnTheScreen(); |
| 80 | + expect(screen.getByLabelText('Username')).toBeOnTheScreen(); |
| 81 | + expect(screen.getByLabelText('Password')).toBeOnTheScreen(); |
| 82 | +}); |
| 83 | + |
| 84 | +/** |
| 85 | + * Do not be afraid to write longer test scenarios, with repeating act and assert statements. |
| 86 | + */ |
| 87 | +test('User can sign in after incorrect attempt', async () => { |
| 88 | + const user = userEvent.setup(); |
| 89 | + render(<App />); |
| 90 | + |
| 91 | + expect(screen.getByRole('header', { name: 'Sign in to Example App' })).toBeOnTheScreen(); |
| 92 | + |
| 93 | + const usernameInput = screen.getByLabelText('Username'); |
| 94 | + const passwordInput = screen.getByLabelText('Password'); |
| 95 | + |
| 96 | + await user.type(usernameInput, 'admin'); |
| 97 | + await user.type(passwordInput, 'qwerty123'); |
| 98 | + await user.press(screen.getByRole('button', { name: 'Sign In' })); |
| 99 | + |
| 100 | + expect(await screen.findByRole('alert')).toHaveTextContent('Incorrect username or password'); |
| 101 | + |
| 102 | + // Clear password field |
| 103 | + await user.clear(passwordInput); |
| 104 | + |
| 105 | + await user.type(passwordInput, 'admin1'); |
| 106 | + await user.press(screen.getByRole('button', { name: 'Sign In' })); |
| 107 | + |
| 108 | + expect(await screen.findByText('Welcome admin!')).toBeOnTheScreen(); |
| 109 | + expect(screen.queryByRole('header', { name: 'Sign in to Example App' })).not.toBeOnTheScreen(); |
| 110 | + expect(screen.queryByLabelText('Username')).not.toBeOnTheScreen(); |
| 111 | + expect(screen.queryByLabelText('Password')).not.toBeOnTheScreen(); |
| 112 | +}); |
0 commit comments