Skip to content

Commit ecf5ca6

Browse files
committed
test: add test case for pause
1 parent 681ee5a commit ecf5ca6

4 files changed

Lines changed: 72 additions & 17 deletions

File tree

jest-setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import '@testing-library/jest-dom';
2-
import { configure } from '@testing-library/react';
2+
// import { configure } from '@testing-library/react';
33

4-
configure({ asyncUtilTimeout: 5000 });
4+
// configure({ asyncUtilTimeout: 1000 });

src/__test__/Typist.test.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
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';
38

49
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';
712

8-
beforeEach(() => {
9-
jest.useFakeTimers();
13+
afterEach(() => {
14+
jest.useRealTimers();
1015
});
1116

1217
test('render children correctly', async () => {
1318
const { container } = render(<Typist>{nestedChildren}</Typist>);
19+
jest.useFakeTimers();
1420

1521
for (const texts of textsArray) {
1622
for (const text of texts) {
17-
await waitFor(() => {
18-
getByText(container, text);
19-
});
23+
await findByText(container, text);
2024
}
2125
jest.runOnlyPendingTimers();
2226
}
2327
});
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+
});

src/__test__/constant.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import Typist from '..';
33

44
export const textsArray = [
@@ -14,11 +14,11 @@ export const textsArray = [
1414
];
1515

1616
export const nestedChildren = (
17-
<div className="ab">
17+
<div className='ab'>
1818
ab
19-
<div className="cd">cd</div>
19+
<div className='cd'>cd</div>
2020
<Typist.Backspace count={2} />
21-
<div className="ef">ef</div>
21+
<div className='ef'>ef</div>
2222
<Typist.Paste>
2323
<span>gh</span>
2424
</Typist.Paste>
@@ -27,3 +27,14 @@ export const nestedChildren = (
2727

2828
export const WRAPPER_ID = 'wrapper';
2929
export const BUTTON_ID = 'button';
30+
31+
export const PauseTest = () => {
32+
const [pause, setPause] = useState(false);
33+
34+
return (
35+
<>
36+
<Typist pause={pause}>ab</Typist>
37+
<button data-testid={BUTTON_ID} onClick={() => setPause(!pause)} />
38+
</>
39+
);
40+
};

src/__test__/utils.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
import type { waitForOptions } from '@testing-library/react';
2-
import { waitFor as originWaitFor } from '@testing-library/react';
1+
import type {
2+
waitForOptions,
3+
Matcher,
4+
SelectorMatcherOptions,
5+
} from '@testing-library/react';
6+
import {
7+
waitFor as originWaitFor,
8+
findByText as originFindByText,
9+
} from '@testing-library/react';
310

4-
export function waitFor<T>(callback: () => Promise<T> | T, options?: waitForOptions) {
11+
export function waitFor<T>(
12+
callback: () => Promise<T> | T,
13+
options?: waitForOptions
14+
) {
515
return originWaitFor(callback, { interval: 0, ...options });
616
}
17+
18+
export function findByText<T extends HTMLElement = HTMLElement>(
19+
container: HTMLElement,
20+
id: Matcher,
21+
options?: SelectorMatcherOptions,
22+
waitForElementOptions?: waitForOptions
23+
) {
24+
return originFindByText<T>(container, id, options, {
25+
interval: 0,
26+
...waitForElementOptions,
27+
});
28+
}

0 commit comments

Comments
 (0)