Skip to content

Commit 45256b8

Browse files
committed
test: add test for getTypedChildrenArray
1 parent 0998f1b commit 45256b8

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

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__/constant.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
import React from 'react';
22
import Typist from '..';
33

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+
416
export const nestedChildren = (
5-
<div className="first">
6-
first
7-
<div className="second">second</div>
8-
<Typist.Backspace count={6} />
9-
<div className="third">third</div>
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>
1025
</div>
1126
);
27+
28+
export const WRAPPER_ID = 'wrapper';
29+
export const BUTTON_ID = 'button';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { fireEvent, getByText, render, screen } from '@testing-library/react';
2+
import type { ReactNode } from 'react';
3+
import React, { useState, useMemo } from 'react';
4+
5+
import type { Splitter } from '../types/TypistProps';
6+
import { defaultSplitter } from '../utils/defaultFuncs';
7+
import getTypedChildrenArray from '../utils/getTypedChildrenArray';
8+
9+
import { BUTTON_ID, nestedChildren, textsArray, WRAPPER_ID } from './constant';
10+
11+
type Props = {
12+
children: ReactNode;
13+
splitter?: Splitter;
14+
};
15+
16+
const App = ({ children, splitter = defaultSplitter }: Props) => {
17+
const typedChildrenArray = useMemo(
18+
() => getTypedChildrenArray(children, splitter),
19+
[children, splitter]
20+
);
21+
const [tick, setTick] = useState(0);
22+
23+
return (
24+
<>
25+
<div data-testid={WRAPPER_ID}>{typedChildrenArray[tick]}</div>
26+
<button data-testid={BUTTON_ID} onClick={() => setTick(tick + 1)}>
27+
increment
28+
</button>
29+
</>
30+
);
31+
};
32+
33+
test('display children correctly', () => {
34+
render(<App>{nestedChildren}</App>);
35+
const wrapper = screen.getByTestId(WRAPPER_ID);
36+
const button = screen.getByTestId(BUTTON_ID);
37+
38+
textsArray.forEach(texts => {
39+
texts.forEach(text => {
40+
getByText(wrapper, text);
41+
});
42+
fireEvent.click(button);
43+
});
44+
});

0 commit comments

Comments
 (0)