From 22ed98e3a531d2fb4eddab4fb55b488e666b3353 Mon Sep 17 00:00:00 2001 From: Brent Farese Date: Thu, 20 Jul 2023 22:45:02 -0400 Subject: [PATCH] Write failing test case. --- packages/core/src/utils/convert.ts | 3 ++- packages/core/test/index.test.ts | 10 ++++++++- packages/core/test/unit/yTextToSlate.tsx | 27 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 packages/core/test/unit/yTextToSlate.tsx diff --git a/packages/core/src/utils/convert.ts b/packages/core/src/utils/convert.ts index 044d9b39..cdd8be55 100644 --- a/packages/core/src/utils/convert.ts +++ b/packages/core/src/utils/convert.ts @@ -10,8 +10,9 @@ export function yTextToSlateElement(yText: Y.XmlText): Element { const children = // eslint-disable-next-line @typescript-eslint/no-use-before-define delta.length > 0 ? delta.map(deltaInsertToSlateNode) : [{ text: '' }]; + const attributes = yText.getAttributes(); - return { ...yText.getAttributes(), children }; + return { ...attributes, children }; } export function deltaInsertToSlateNode(insert: DeltaInsert): Node { diff --git a/packages/core/test/index.test.ts b/packages/core/test/index.test.ts index d914e558..11b0af44 100644 --- a/packages/core/test/index.test.ts +++ b/packages/core/test/index.test.ts @@ -10,7 +10,7 @@ export type FixtureModule = { module: { input: Editor; expected: Editor; - run: (e: Editor) => void; + run: (e: Editor) => any; }; }; @@ -60,6 +60,14 @@ async function runCollaborationTest({ module }: FixtureModule) { expect(editor.children).toEqual(expectedEditor.children); } +async function runUnitTest({ module }: FixtureModule) { + const { input, run, expected } = module; + const editor = await withTestingElements(input); + const runOutput = run(editor); + expect(runOutput).toEqual(expected); +} + describe('adapter', () => { + fixtures(__dirname, 'unit', runUnitTest); fixtures(__dirname, 'collaboration', runCollaborationTest); }); diff --git a/packages/core/test/unit/yTextToSlate.tsx b/packages/core/test/unit/yTextToSlate.tsx new file mode 100644 index 00000000..c0762b98 --- /dev/null +++ b/packages/core/test/unit/yTextToSlate.tsx @@ -0,0 +1,27 @@ +/** @jsx jsx */ +import { Editor } from 'slate'; +import { jsx } from '../../../../support/jsx'; +import { YjsEditor, yTextToSlateElement } from '../../src'; + +export const input = ( + + + + + +); + +export const expected = { + children: [ + + + , + ], +}; + +export function run(editor: Editor) { + const isYJSEditor = YjsEditor.isYjsEditor(editor); + if (!isYJSEditor) return; + + return yTextToSlateElement(editor.sharedRoot); +}