Skip to content

Commit 18c6a89

Browse files
committed
test: update Typist's test
1 parent d032ee6 commit 18c6a89

3 files changed

Lines changed: 18 additions & 114 deletions

File tree

src/__test__/Typist.test.tsx

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,23 @@
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';
33

44
import Typist from '..';
5-
import { nestedChildren } from './constant';
5+
import { nestedChildren, textsArray } from './constant';
6+
import { waitFor } from './utils';
67

78
beforeEach(() => {
89
jest.useFakeTimers();
910
});
1011

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>);
2114

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) {
4917
await waitFor(() => {
50-
expect(container.firstChild).toBeNull();
18+
getByText(container, text);
5119
});
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+
}
8123
});

src/__test__/getTypedChildrenArray.test.tsx

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/__test__/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { waitForOptions } from '@testing-library/react';
2+
import { waitFor as originWaitFor } from '@testing-library/react';
3+
4+
export function waitFor<T>(callback: () => Promise<T> | T, options?: waitForOptions) {
5+
return originWaitFor(callback, { interval: 0, ...options });
6+
}

0 commit comments

Comments
 (0)