โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ ๐ shadcn/directory/udecode/plate/(guides)/unit-testing.cn โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
ๆฌๆๅๆฆ่ฟฐไบไฝฟ็จ @platejs/test-utils ๅฏน Plate ๆไปถๅ็ปไปถ่ฟ่กๅๅ
ๆต่ฏ็ๆไฝณๅฎ่ทตใ
npm install @platejs/test-utils
ๅจๆต่ฏๆไปถ้กถ้จๆทปๅ JSX ็ผ่ฏๆ็คบ๏ผ
/** @jsx jsx */
import { jsx } from '@platejs/test-utils';
jsx; // ้ฟๅ
ESLint ๆฅ้
่ฟๅ ่ฎธไฝ ไฝฟ็จ JSX ่ฏญๆณๆฅๅๅปบ็ผ่พๅจๅผใ
ไฝฟ็จ JSX ่กจ็คบ็ผ่พๅจ็ถๆ๏ผ
const input = (
<editor>
<hp>
Hello<cursor /> world
</hp>
</editor>
) as any as PlateEditor;
่็นๅ
็ด ๅฆ <hp />ใ<hul />ใ<hli /> ่กจ็คบไธๅ็ฑปๅ็่็นใ
็นๆฎๅ
็ด ๅฆ <cursor />ใ<anchor /> ๅ <focus /> ่กจ็คบ้ๆฉ็ถๆใ
createPlateEditor ่ฎพ็ฝฎ็ผ่พๅจๆต่ฏๅ ็ฒๆ ผๅผๅ็็คบไพ๏ผ
it('ๅบๅบ็จๅ ็ฒๆ ผๅผๅ', () => {
const input = (
<editor>
<hp>
Hello <anchor />
world
<focus />
</hp>
</editor>
) as any as PlateEditor;
const output = (
<editor>
<hp>
Hello <htext bold>world</htext>
</hp>
</editor>
) as any as PlateEditor;
const editor = createPlateEditor({
plugins: [BoldPlugin],
value: input.children,
selection: input.selection,
});
// ็ดๆฅๅบ็จ่ฝฌๆข
editor.tf.toggleMark('bold');
expect(editor.children).toEqual(output.children);
});
ๆต่ฏๆไฝๅฆไฝๅฝฑๅ็ผ่พๅจ็้ๆฉ๏ผ
it('ๅบๅจ้ๆ ผๆถๆๅ ้ๆฉ', () => {
const input = (
<editor>
<hp>
He<anchor />llo wor<focus />ld
</hp>
</editor>
) as any as PlateEditor;
const output = (
<editor>
<hp>
He<cursor />ld
</hp>
</editor>
) as any as PlateEditor;
const editor = createPlateEditor({
value: input.children,
selection: input.selection,
});
editor.tf.deleteBackward();
expect(editor.children).toEqual(output.children);
expect(editor.selection).toEqual(output.selection);
});
ๅฝ้่ฆ็ดๆฅๆต่ฏ้ฎ็ๅค็็จๅบๆถ๏ผ
it('ๅบ่ฐ็จ onKeyDown ๅค็็จๅบ', () => {
const input = (
<editor>
<hp>
Hello <anchor />world<focus />
</hp>
</editor>
) as any as PlateEditor;
// ๅๅปบๆจกๆๅค็็จๅบไปฅ้ช่ฏ่ฐ็จ
const onKeyDownMock = jest.fn();
const editor = createPlateEditor({
value: input.children,
selection: input.selection,
plugins: [
{
key: 'test',
handlers: {
onKeyDown: onKeyDownMock,
},
},
],
});
// ๅๅปบ้ฎ็ไบไปถ
const event = new KeyboardEvent('keydown', {
key: 'Enter',
}) as any;
// ็ดๆฅ่ฐ็จๅค็็จๅบ
editor.plugins.test.handlers.onKeyDown({
...getEditorPlugin(editor, { key: 'test' }),
event,
});
// ้ช่ฏๅค็็จๅบ่ขซ่ฐ็จ
expect(onKeyDownMock).toHaveBeenCalled();
});
ๅฏนไบ่กจๆ ผ็ญๅคๆๆไปถ๏ผ้่ฟ็ดๆฅๅบ็จ่ฝฌๆขๆฅๆต่ฏๅ็งๅบๆฏ๏ผ
describe('่กจๆ ผๆไปถ', () => {
it('ๅบๆๅ
ฅ่กจๆ ผ', () => {
const input = (
<editor>
<hp>
Test<cursor />
</hp>
</editor>
) as any as PlateEditor;
const output = (
<editor>
<hp>Test</hp>
<htable>
<htr>
<htd>
<hp>
<cursor />
</hp>
</htd>
<htd>
<hp></hp>
</htd>
</htr>
<htr>
<htd>
<hp></hp>
</htd>
<htd>
<hp></hp>
</htd>
</htr>
</htable>
</editor>
) as any as PlateEditor;
const editor = createPlateEditor({
value: input.children,
selection: input.selection,
plugins: [TablePlugin],
});
// ็ดๆฅ่ฐ็จ่ฝฌๆข
editor.tf.insertTable({ rows: 2, columns: 2 });
expect(editor.children).toEqual(output.children);
expect(editor.selection).toEqual(output.selection);
});
});
ๆต่ฏไธๅๆไปถ้้กนๅฆไฝๅฝฑๅ่กไธบ๏ผ
describe('ๅฝๅฏ็จๆค้ๆถ', () => {
it('ๅบๅจๅ ้คๆถๆค้ๆๆฌๆ ผๅผ', () => {
const input = (
<fragment>
<hp>
1/<cursor />
</hp>
</fragment>
) as any;
const output = (
<fragment>
<hp>
1/4<cursor />
</hp>
</fragment>
) as any;
const editor = createPlateEditor({
plugins: [
AutoformatPlugin.configure({
options: {
enableUndoOnDelete: true,
rules: [
{
format: 'ยผ',
match: '1/4',
mode: 'text',
},
],
},
}),
],
value: input,
});
// ่งฆๅ่ชๅจๆ ผๅผๅ
editor.tf.insertText('4');
// ๆจกๆ้ๆ ผ้ฎ
const event = new KeyboardEvent('keydown', {
key: 'backspace',
}) as any;
// ่ฐ็จๅค็็จๅบ
editor.getPlugin({key: KEYS.autoformat}).handlers.onKeyDown({
...getEditorPlugin(editor, AutoformatPlugin),
event,
});
// ๅฝ enableUndoOnDelete: true ๆถ๏ผๆ้ๆ ผ้ฎๅบๆขๅคๅๅงๆๆฌ
expect(input.children).toEqual(output.children);
});
});
่ฝ็ถๆจกๆๅฏ็จไบ้็ฆป็นๅฎ่กไธบ๏ผไฝ Plate ๆต่ฏ้ๅธธไผๅจ่ฝฌๆขๅ่ฏไผฐๅฎ้ ็็ผ่พๅจๅญ่็นๅ้ๆฉใ่ฟ็งๆนๆณ็กฎไฟๆไปถ่ฝไธๆดไธช็ผ่พๅจ็ถๆๆญฃ็กฎๅๅๅทฅไฝใ
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ