]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/table/__tests__/unit/LexicalTableCellNode.test.ts
9c56db63bf33c06b8b17409298593545219a08c6
[bookstack] / resources / js / wysiwyg / lexical / table / __tests__ / unit / LexicalTableCellNode.test.ts
1 /**
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  *
7  */
8
9 import {$createTableCellNode, TableCellHeaderStates} from '@lexical/table';
10 import {initializeUnitTest} from 'lexical/src/__tests__/utils';
11
12 const editorConfig = Object.freeze({
13   namespace: '',
14   theme: {
15     tableCell: 'test-table-cell-class',
16   },
17 });
18
19 describe('LexicalTableCellNode tests', () => {
20   initializeUnitTest((testEnv) => {
21     test('TableCellNode.constructor', async () => {
22       const {editor} = testEnv;
23
24       await editor.update(() => {
25         const cellNode = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
26
27         expect(cellNode).not.toBe(null);
28       });
29
30       expect(() =>
31         $createTableCellNode(TableCellHeaderStates.NO_STATUS),
32       ).toThrow();
33     });
34
35     test('TableCellNode.createDOM()', async () => {
36       const {editor} = testEnv;
37
38       await editor.update(() => {
39         const cellNode = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
40         expect(cellNode.createDOM(editorConfig).outerHTML).toBe(
41           `<td class="${editorConfig.theme.tableCell}"></td>`,
42         );
43
44         const headerCellNode = $createTableCellNode(TableCellHeaderStates.ROW);
45         expect(headerCellNode.createDOM(editorConfig).outerHTML).toBe(
46           `<th class="${editorConfig.theme.tableCell}"></th>`,
47         );
48
49         const colSpan = 2;
50         const cellWithRowSpanNode = $createTableCellNode(
51           TableCellHeaderStates.NO_STATUS,
52           colSpan,
53         );
54         expect(cellWithRowSpanNode.createDOM(editorConfig).outerHTML).toBe(
55           `<td colspan="${colSpan}" class="${editorConfig.theme.tableCell}"></td>`,
56         );
57
58         const cellWidth = 200;
59         const cellWithCustomWidthNode = $createTableCellNode(
60           TableCellHeaderStates.NO_STATUS,
61           undefined,
62           cellWidth,
63         );
64         expect(cellWithCustomWidthNode.createDOM(editorConfig).outerHTML).toBe(
65           `<td style="width: ${cellWidth}px;" class="${editorConfig.theme.tableCell}"></td>`,
66         );
67       });
68     });
69   });
70 });