|
1 | | -import React, { useState } from 'react'; |
2 | | -import { render, screen, waitFor } from '@testing-library/react'; |
| 1 | +import React from 'react'; |
| 2 | +import { getByText, render } from '@testing-library/react'; |
3 | 3 |
|
4 | 4 | import Typist from '..'; |
5 | | -import { nestedChildren } from './constant'; |
| 5 | +import { nestedChildren, textsArray } from './constant'; |
| 6 | +import { waitFor } from './utils'; |
6 | 7 |
|
7 | 8 | beforeEach(() => { |
8 | 9 | jest.useFakeTimers(); |
9 | 10 | }); |
10 | 11 |
|
11 | | -describe('Display children correctly.', () => { |
12 | | - // Since the way Typist generates its children is different when disabled is different, |
13 | | - // we need to test it separately. |
14 | | - describe('Disabled is true', () => { |
15 | | - test('Nested children', () => { |
16 | | - render(<Typist disabled>{nestedChildren}</Typist>); |
17 | | - screen.getByText('first'); |
18 | | - expect(screen.queryByText('second')).toBeNull(); |
19 | | - screen.getByText('third'); |
20 | | - }); |
| 12 | +test('render children correctly', async () => { |
| 13 | + const { container } = render(<Typist>{nestedChildren}</Typist>); |
21 | 14 |
|
22 | | - test('clear all children if the count of the last backspace is Infinity', () => { |
23 | | - const { container } = render( |
24 | | - <Typist disabled> |
25 | | - {nestedChildren} |
26 | | - <Typist.Backspace count={Infinity} /> |
27 | | - </Typist> |
28 | | - ); |
29 | | - expect(container.firstChild).toBeNull(); |
30 | | - }); |
31 | | - }); |
32 | | - |
33 | | - describe('Disabled is false', () => { |
34 | | - test('nested children', async () => { |
35 | | - render(<Typist>{nestedChildren}</Typist>); |
36 | | - await screen.findByText('first'); |
37 | | - await screen.findByText('second'); |
38 | | - await screen.findByText('third'); |
39 | | - expect(screen.queryByText('second')).toBeNull(); |
40 | | - }); |
41 | | - |
42 | | - test('clear all childthe count of the last backspace is Infinity', async () => { |
43 | | - const { container } = render( |
44 | | - <Typist> |
45 | | - {nestedChildren} |
46 | | - <Typist.Backspace count={Infinity} /> |
47 | | - </Typist> |
48 | | - ); |
| 15 | + for (const texts of textsArray) { |
| 16 | + for (const text of texts) { |
49 | 17 | await waitFor(() => { |
50 | | - expect(container.firstChild).toBeNull(); |
| 18 | + getByText(container, text); |
51 | 19 | }); |
52 | | - }); |
53 | | - }); |
54 | | -}); |
55 | | - |
56 | | -test('Dynamic children', async () => { |
57 | | - const arr = ['text 1', 'text 2']; |
58 | | - const App = () => { |
59 | | - const [index, setIndex] = useState(0); |
60 | | - return ( |
61 | | - <Typist |
62 | | - onTypingDone={() => { |
63 | | - setIndex(index === 0 ? 1 : 0); |
64 | | - }} |
65 | | - restartKey={index} |
66 | | - > |
67 | | - {arr[index]} |
68 | | - </Typist> |
69 | | - ); |
70 | | - }; |
71 | | - |
72 | | - render(<App />); |
73 | | - await waitFor(() => { |
74 | | - screen.getByText('text 1'); |
75 | | - expect(screen.queryByText('text 2')).toBeNull(); |
76 | | - }); |
77 | | - await waitFor(() => { |
78 | | - screen.getByText('text 2'); |
79 | | - expect(screen.queryByText('text 1')).toBeNull(); |
80 | | - }); |
| 20 | + } |
| 21 | + jest.runOnlyPendingTimers(); |
| 22 | + } |
81 | 23 | }); |
0 commit comments