Skip to content

Commit

Permalink
wip: fixed problem with jsdom in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasho Mihajlov authored and Sasho Mihajlov committed Oct 2, 2023
1 parent a04816b commit f08a2ee
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/html-to-lexical-parser/__tests__/html-articles.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import { parseToLexicalObject } from "~/index";
import { carArticle } from "./html-articles";

describe("Test html-to-lexical parser", () => {
it("should parse html string to lexical object", async () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/html-to-lexical-parser/__tests__/setupEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-nocheck
// noinspection JSConstantReassignment
const { TextEncoder, TextDecoder } = require("util");

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
6 changes: 5 additions & 1 deletion packages/html-to-lexical-parser/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const base = require("../../jest.config.base");

module.exports = {
...base({ path: __dirname })
...base({ path: __dirname }),
moduleNameMapper: {
"\\.(css|sass)$": "identity-obj-proxy"
},
setupFilesAfterEnv: [require.resolve("./__tests__/setupEnv.ts")]
};
3 changes: 2 additions & 1 deletion packages/html-to-lexical-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"scripts": {
"build": "yarn webiny run build",
"watch": "yarn webiny run watch"
}
},
"test": "jest --verbose --runInBand --detectOpenHandles --forceExit"
}
18 changes: 14 additions & 4 deletions packages/html-to-lexical-parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { generateInitialLexicalValue, getSupportedNodeList } from "@webiny/lexical-editor";
import {
generateInitialLexicalValue,
getSupportedNodeList,
getTheme
} from "@webiny/lexical-editor";
import { createHeadlessEditor } from "@lexical/headless";
import { JSDOM } from "jsdom";
import { $generateNodesFromDOM } from "@lexical/html";
import { TextEncoder } from "util";

const jsdom = require("jsdom");
const { JSDOM } = jsdom;

global.TextEncoder = TextEncoder;

/**
* Parse html string to lexical object.
Expand All @@ -17,12 +26,13 @@ export const parseToLexicalObject = (

const editor = createHeadlessEditor({
nodes: getSupportedNodeList(),
onError: onError
onError: onError,
theme: getTheme()
});

const dom = new JSDOM(htmlString);

// Convert to lexical node objects format that can be stored in db.
const nodesData = $generateNodesFromDOM(editor, dom).map(node => node.get());
const nodesData = $generateNodesFromDOM(editor, dom).map(node => node.exportJSON());
return nodesData;
};
1 change: 1 addition & 0 deletions packages/lexical-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export { generateInitialLexicalValue } from "~/utils/generateInitialLexicalValue
export { isValidLexicalData } from "~/utils/isValidLexicalData";
export { clearNodeFormatting } from "~/utils/nodes/clearNodeFormating";
export { getSupportedNodeList } from "~/utils/getSupportedNodeList";
export { getTheme } from "~/utils/getTheme";
// Commands
export { INSERT_IMAGE_COMMAND } from "~/commands/insertFiles";
// types
Expand Down
9 changes: 9 additions & 0 deletions packages/lexical-editor/src/utils/getTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { WebinyEditorTheme, webinyEditorTheme } from "~/themes/webinyLexicalTheme";
import { EditorThemeClasses } from "lexical";

/**
* Get webiny theme used for lexical editor
*/
export const getTheme = (): EditorThemeClasses | WebinyEditorTheme => {
return webinyEditorTheme;
};

0 comments on commit f08a2ee

Please sign in to comment.