Skip to content

Commit 96e7a1c

Browse files
committed
test: Add test case.
1 parent adad5e6 commit 96e7a1c

File tree

7 files changed

+98
-21
lines changed

7 files changed

+98
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
lib
22
build
3+
coverage
34
node_modules
45
npm-debug.log*
56
package-lock.json

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@kkt/raw-modules": "6.10.4",
5252
"@kkt/scope-plugin-options": "6.10.4",
5353
"@mapbox/rehype-prism": "0.6.0",
54+
"@testing-library/react": "12.0.0",
5455
"@types/react": "17.0.11",
5556
"@types/react-dom": "17.0.8",
5657
"@types/react-test-renderer": "17.0.1",

src/__test__/index.test.tsx

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,101 @@
11
/* eslint-disable jest/no-conditional-expect */
2+
import React, { useRef } from 'react';
23
import TestRenderer from 'react-test-renderer';
4+
import { fireEvent, render } from '@testing-library/react';
35
import TextareaCodeEditor from '../';
46

57
it('Should output a TextareaCodeEditor', async () => {
68
const component = TestRenderer.create(<TextareaCodeEditor />);
79
let tree = component.toJSON();
810
if (tree && !Array.isArray(tree)) {
9-
expect(tree.type).toEqual('button');
11+
expect(tree.type).toEqual('div');
1012
expect(tree.props.disabled).toBeFalsy();
11-
expect(tree.props.className).toEqual('w-btn w-btn-default w-btn-primary');
13+
expect(tree.props.className).toEqual('w-tc-editor ');
14+
expect(tree.props.style).toEqual({
15+
position: 'relative',
16+
textAlign: 'left',
17+
boxSizing: 'border-box',
18+
padding: 0,
19+
overflow: 'hidden',
20+
});
21+
expect(tree.children!.length).toEqual(2);
22+
tree.children!.forEach((child) => {
23+
if (typeof child === 'object') {
24+
expect(/^(div|textarea)$/.test(child.type || '')).toBeTruthy();
25+
if (child.type === 'textarea') {
26+
expect(child.props.autoComplete).toEqual('off');
27+
expect(child.props.autoCorrect).toEqual('off');
28+
expect(child.props.spellCheck).toEqual('false');
29+
expect(child.props.autoCapitalize).toEqual('off');
30+
expect(child.props.className).toEqual('w-tc-editor-text');
31+
}
32+
}
33+
});
34+
}
35+
});
36+
37+
it('TextareaCodeEditor language="html"', async () => {
38+
const component = TestRenderer.create(
39+
<TextareaCodeEditor language="html" onChange={() => {}} value="<div>Hello World!</div>" />,
40+
);
41+
let tree = component.toJSON();
42+
if (tree && !Array.isArray(tree)) {
43+
expect(tree.type).toEqual('div');
44+
expect(tree.props.disabled).toBeFalsy();
45+
expect(tree.props.className).toEqual('w-tc-editor ');
46+
expect(tree.props.style).toEqual({
47+
position: 'relative',
48+
textAlign: 'left',
49+
boxSizing: 'border-box',
50+
padding: 0,
51+
overflow: 'hidden',
52+
});
53+
expect(tree.children!.length).toEqual(2);
54+
tree.children!.forEach((child) => {
55+
if (typeof child === 'object') {
56+
expect(/^(div|textarea)$/.test(child.type || '')).toBeTruthy();
57+
if (child.type === 'textarea') {
58+
expect(child.props.autoComplete).toEqual('off');
59+
expect(child.props.autoCorrect).toEqual('off');
60+
expect(child.props.spellCheck).toEqual('false');
61+
expect(child.props.autoCapitalize).toEqual('off');
62+
expect(child.props.className).toEqual('w-tc-editor-text');
63+
}
64+
if (child.type === 'div') {
65+
expect(child.props.style).toEqual({
66+
paddingTop: 10,
67+
paddingRight: 10,
68+
paddingBottom: 10,
69+
paddingLeft: 10,
70+
});
71+
expect(child.props.className).toEqual('w-tc-editor-preview language-html');
72+
expect(typeof child.props.dangerouslySetInnerHTML).toEqual('object');
73+
}
74+
}
75+
});
76+
}
77+
});
78+
79+
it('TextareaCodeEditor onChange', async () => {
80+
const MyComponent = () => {
81+
const txtRef = useRef<HTMLTextAreaElement>(null);
82+
const [code] = React.useState(`function add(a, b) {\n return a + b;\n}`);
83+
return (
84+
<TextareaCodeEditor
85+
language="js"
86+
value={code}
87+
ref={txtRef}
88+
onChange={(evn) => {
89+
expect(evn.target.value).toEqual('a');
90+
}}
91+
/>
92+
);
93+
};
94+
95+
const {
96+
container: { firstChild },
97+
} = render(<MyComponent />);
98+
if (firstChild && firstChild.firstChild) {
99+
fireEvent.input(firstChild.firstChild, { target: { value: 'a' } });
12100
}
13101
});

src/index.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,8 @@ export interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HT
1616
}
1717

1818
export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((props, ref) => {
19-
const {
20-
prefixCls = 'w-tc-editor',
21-
value: _,
22-
padding = 10,
23-
language,
24-
className,
25-
style,
26-
onChange = () => null,
27-
...other
28-
} = props;
19+
const { prefixCls = 'w-tc-editor', value: _, padding = 10, language, className, style, onChange, ...other } = props;
20+
2921
const [value, setValue] = useState(props.value || '');
3022
useEffect(() => setValue(props.value || ''), [props.value]);
3123

@@ -35,6 +27,7 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
3527
paddingBottom: padding,
3628
paddingLeft: padding,
3729
};
30+
3831
const htmlStr = useMemo(
3932
() =>
4033
processHtml(
@@ -74,7 +67,7 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
7467
ref={ref}
7568
onChange={(evn) => {
7669
setValue(evn.target.value);
77-
onChange(evn);
70+
onChange && onChange(evn);
7871
}}
7972
className={`${prefixCls}-text`}
8073
value={value}

src/style/index.less

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
font-size: 12px;
66
&-text,
77
&-preview {
8-
// padding: 10px;
98
min-height: 16px;
109
}
1110
&-preview {
@@ -82,7 +81,7 @@
8281

8382
.token.bold,
8483
.token.important {
85-
font-weight: 700;
84+
color: #333;
8685
}
8786
.token.tag {
8887
color: #22863a;

src/utils.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,3 @@ export function htmlEncode(sHtml: string) {
2424
(c: string) => (({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' } as Record<string, string>)[c]),
2525
);
2626
}
27-
28-
export const resultString = (text: string) =>
29-
text.replace(/<code\sclass="language-(html|css|js)">/, '').replace(/<\/code>/, '');

website/App.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ const App: React.FC = () => {
3030
if (language) {
3131
import(`code-example/txt/sample.${language}.txt`)
3232
.then((code) => {
33-
console.log('code:', code.default);
34-
setValue(code.default);
33+
setValue(code.default || '');
3534
})
3635
.catch((err) => {
37-
console.log('err:', err);
3836
setValue('');
3937
});
4038
}

0 commit comments

Comments
 (0)