|
1 | 1 | import React from 'react'; |
2 | | -import { getByText, render } from '@testing-library/react'; |
| 2 | +import { |
| 3 | + fireEvent, |
| 4 | + getByTestId, |
| 5 | + getByText, |
| 6 | + render, |
| 7 | +} from '@testing-library/react'; |
3 | 8 |
|
4 | 9 | import Typist from '..'; |
5 | | -import { nestedChildren, textsArray } from './constant'; |
6 | | -import { waitFor } from './utils'; |
| 10 | +import { nestedChildren, textsArray, PauseTest, BUTTON_ID } from './constant'; |
| 11 | +import { findByText, waitFor } from './utils'; |
7 | 12 |
|
8 | | -beforeEach(() => { |
9 | | - jest.useFakeTimers(); |
| 13 | +afterEach(() => { |
| 14 | + jest.useRealTimers(); |
10 | 15 | }); |
11 | 16 |
|
12 | 17 | test('render children correctly', async () => { |
13 | 18 | const { container } = render(<Typist>{nestedChildren}</Typist>); |
| 19 | + jest.useFakeTimers(); |
14 | 20 |
|
15 | 21 | for (const texts of textsArray) { |
16 | 22 | for (const text of texts) { |
17 | | - await waitFor(() => { |
18 | | - getByText(container, text); |
19 | | - }); |
| 23 | + await findByText(container, text); |
20 | 24 | } |
21 | 25 | jest.runOnlyPendingTimers(); |
22 | 26 | } |
23 | 27 | }); |
| 28 | + |
| 29 | +test('pause the typing animation', async () => { |
| 30 | + const { container } = render(<PauseTest />); |
| 31 | + const button = getByTestId(container, BUTTON_ID); |
| 32 | + |
| 33 | + await findByText(container, 'a'); |
| 34 | + fireEvent.click(button); |
| 35 | + let isPauseWork = true; |
| 36 | + try { |
| 37 | + await findByText(container, 'ab'); |
| 38 | + isPauseWork = false; |
| 39 | + } catch (error) { |
| 40 | + // do nothing |
| 41 | + } |
| 42 | + if (!isPauseWork) throw new Error('Pause does not work!'); |
| 43 | + fireEvent.click(button); |
| 44 | + await findByText(container, 'ab'); |
| 45 | +}); |
0 commit comments