-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy patheditor.test.tsx
137 lines (131 loc) · 3.84 KB
/
editor.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @jest-environment jsdom
*/
/* eslint-disable jest/no-conditional-expect */
import React from 'react';
import TestRenderer from 'react-test-renderer';
import { render, fireEvent, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import MDEditor from '../core/src';
// In your test setup file
// @ts-ignore
// globalThis.IS_REACT_ACT_ENVIRONMENT = true;
it('MDEditor', () => {
const component = TestRenderer.create(<MDEditor value="**Hello world!!!**" />);
let tree = component.toJSON();
if (tree && !Array.isArray(tree)) {
expect(tree.type).toEqual('div');
expect(tree.props.className).toEqual('wmde-markdown-var w-md-editor w-md-editor-show-live');
expect(tree.props.style).toMatchObject({
height: 200,
});
}
});
it('MDEditor onChange', async () => {
const MyComponent = () => {
const [value, setValue] = React.useState('**Hello world!!!**');
return (
<MDEditor
value={value}
textareaProps={{
title: 'test',
}}
onChange={(value) => {
expect(value).toEqual('# title');
setValue(value || '');
}}
/>
);
};
render(<MyComponent />);
const inputNode = screen.getByTitle('test');
inputNode.focus();
fireEvent.change(inputNode, { target: { value: '# title' } });
inputNode.blur();
});
it('MDEditor KeyboardEvent onChange', async () => {
const handleChange = jest.fn((value, evn, state) => value);
render(
<MDEditor
value=""
textareaProps={{
title: 'test',
}}
onChange={handleChange}
/>,
);
const input = screen.getByTitle('test');
userEvent.type(input, 'Hello,{enter}World!');
expect(handleChange).toHaveLength(3);
// expect(handleChange).lastReturnedWith('!');
// expect(handleChange).nthCalledWith(7, '\n');
});
it('MDEditor KeyboardEvent onHeightChange', async () => {
const handleChange = jest.fn((value, propsValue, state) => {
return propsValue;
});
const MyComponent = () => {
const [height, setHeight] = React.useState(300);
React.useEffect(() => {
setHeight(500);
}, []);
return (
<MDEditor
value=""
height={height}
textareaProps={{
title: 'test',
}}
onHeightChange={handleChange}
/>
);
};
render(<MyComponent />);
expect(handleChange).lastReturnedWith(500);
expect(handleChange).toHaveLength(3);
});
it('MDEditor supports asynchronous plugins', async () => {
const MyComponent = () => {
const [value, setValue] = React.useState('**Hello world!!!**');
return (
<MDEditor
value={value}
textareaProps={{
title: 'test',
}}
previewOptions={{
rehypePlugins: [
async () => {
return (tree) => {
// Example async plugin that adds a class to the first node
return new Promise((resolve) => {
setTimeout(() => {
if (tree.children && tree.children.length > 0) {
tree.children[0].properties = {
...tree.children[0].properties,
className: 'async-plugin',
};
}
resolve(tree);
}, 100);
});
};
},
],
}}
onChange={(value) => {
setValue(value || '');
}}
/>
);
};
render(<MyComponent />);
const inputNode = screen.getByTitle('test');
inputNode.focus();
fireEvent.change(inputNode, { target: { value: '# title' } });
inputNode.blur();
await new Promise((resolve) => setTimeout(resolve, 200)); // Wait for async plugin to complete
const previewNode = screen.getByText('title');
expect(previewNode).toHaveClass('async-plugin');
});