Skip to content

Commit d76cea9

Browse files
authored
Merge pull request #8 from jason89521/test
2 parents 9262dbf + 18c6a89 commit d76cea9

9 files changed

Lines changed: 68 additions & 103 deletions

File tree

__test__/Typist.test.tsx

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

__test__/utils.tsx

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"scripts": {
3333
"commit": "cz",
3434
"dev": "vite",
35-
"test": "jest ./__test__/Typist.test.tsx",
35+
"test": "jest",
3636
"build:example": "tsc && vite build",
3737
"build:types": "rimraf types/* && tsc --project tsconfig.lib.json",
3838
"build:lib": "yarn build:types && vite build --mode lib && api-extractor run",

src/__test__/Typist.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
import { getByText, render } from '@testing-library/react';
3+
4+
import Typist from '..';
5+
import { nestedChildren, textsArray } from './constant';
6+
import { waitFor } from './utils';
7+
8+
beforeEach(() => {
9+
jest.useFakeTimers();
10+
});
11+
12+
test('render children correctly', async () => {
13+
const { container } = render(<Typist>{nestedChildren}</Typist>);
14+
15+
for (const texts of textsArray) {
16+
for (const text of texts) {
17+
await waitFor(() => {
18+
getByText(container, text);
19+
});
20+
}
21+
jest.runOnlyPendingTimers();
22+
}
23+
});

src/__test__/constant.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import Typist from '..';
3+
4+
export const textsArray = [
5+
['a'],
6+
['ab'],
7+
['ab', 'c'],
8+
['ab', 'cd'],
9+
['ab', 'c'],
10+
['ab'],
11+
['ab', 'e'],
12+
['ab', 'ef'],
13+
['ab', 'ef', 'gh'],
14+
];
15+
16+
export const nestedChildren = (
17+
<div className="ab">
18+
ab
19+
<div className="cd">cd</div>
20+
<Typist.Backspace count={2} />
21+
<div className="ef">ef</div>
22+
<Typist.Paste>
23+
<span>gh</span>
24+
</Typist.Paste>
25+
</div>
26+
);
27+
28+
export const WRAPPER_ID = 'wrapper';
29+
export const BUTTON_ID = 'button';

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+
}

src/components/Main.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,27 @@ const Main = ({
8686
do {
8787
setCurrentIndex(-1);
8888
const actions = getActions(children, splitter);
89-
await timeoutPromise(startDelay);
89+
if (startDelay > 0) await timeoutPromise(startDelay);
9090
for (const { type, payload } of actions) {
91+
if (pause) await pausePromise();
9192
if (type === 'TYPE_TOKEN') {
92-
await pausePromise();
9393
setCurrentIndex(prev => prev + 1);
9494
await timeoutPromise(typingDelay);
9595
} else if (type === 'BACKSPACE') {
9696
let amount = payload;
9797
while (amount--) {
98-
await pausePromise();
9998
setCurrentIndex(prev => prev + 1);
10099
await timeoutPromise(backspaceDelay);
101100
}
102101
} else if (type === 'PASTE') {
103102
setCurrentIndex(prev => prev + 1);
104-
} else if (type === 'PAUSE') {
103+
} else if (type === 'DELAY') {
105104
await timeoutPromise(payload);
106105
}
107106
}
108107
onTypingDone?.();
109-
await timeoutPromise(finishDelay);
110-
await loopPromise();
108+
if (finishDelay > 0) await timeoutPromise(finishDelay);
109+
if (!loopRef.current) await loopPromise();
111110
} while (loopRef.current);
112111
} catch (error) {
113112
// do nothing

src/types/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export type TypeTokenAction = {
55
payload: string | ReactElement;
66
};
77
export type BackspaceAction = { type: 'BACKSPACE'; payload: number };
8-
export type PauseAction = { type: 'PAUSE'; payload: number };
8+
export type DelayAction = { type: 'DELAY'; payload: number };
99
export type PasteAction = { type: 'PASTE'; payload: string | ReactElement };
10-
export type Action = TypeTokenAction | BackspaceAction | PauseAction | PasteAction;
10+
export type Action = TypeTokenAction | BackspaceAction | DelayAction | PasteAction;

src/utils/getActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Children, isValidElement } from 'react';
44
import type {
55
Action,
66
BackspaceAction,
7-
PauseAction,
7+
DelayAction,
88
PasteAction,
99
TypeTokenAction,
1010
} from '../types/actions';
@@ -25,7 +25,7 @@ const backspace = (count: number): BackspaceAction => ({
2525
payload: count,
2626
});
2727

28-
const pause = (ms: number): PauseAction => ({ type: 'PAUSE', payload: ms });
28+
const pause = (ms: number): DelayAction => ({ type: 'DELAY', payload: ms });
2929

3030
const paste = (str: string): PasteAction => ({ type: 'PASTE', payload: str });
3131

0 commit comments

Comments
 (0)