Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed Nov 26, 2024
1 parent 75128c5 commit 4100839
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
61 changes: 61 additions & 0 deletions packages/lexical-playground/__tests__/e2e/RetainSelection.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {selectAll} from '../keyboardShortcuts/index.mjs';
import {
evaluate,
expect,
focusEditor,
initialize,
locate,
test,
} from '../utils/index.mjs';

/* eslint-disable sort-keys-fix/sort-keys-fix */
test.describe('RetainSelection', () => {
test.beforeEach(({isCollab, page}) =>
initialize({isCollab, page, retainSelection: true}),
);
test(`retain selection works`, async ({page, isPlainText, browserName}) => {
await focusEditor(page);
await page.keyboard.type('Lexical');
await selectAll(page);
await locate(page, 'body').click();
const {distance, widthDifference, heightDifference} = await evaluate(
page,
() => {
function compareNodeAlignment(node1, node2, tolerance = 0) {
const rect1 = node1.getBoundingClientRect();
const rect2 = node2.getBoundingClientRect();
const distance_ = Math.sqrt(
Math.pow(rect1.left - rect2.left, 2) +
Math.pow(rect1.top - rect2.top, 2),
);
const widthDifference_ = Math.abs(rect1.width - rect2.width);
const heightDifference_ = Math.abs(rect1.height - rect2.height);
return {
distance: distance_,
widthDifference: widthDifference_,
heightDifference: heightDifference_,
};
}
const editorSpan = document.querySelector(
'[contenteditable="true"] span',
);
const fakeSelection = document.querySelector(
'[style*="background: highlight"]',
);
return compareNodeAlignment(editorSpan, fakeSelection, 5);
},
);
await expect(distance).toBeLessThanOrEqual(5);
await expect(widthDifference).toBeLessThanOrEqual(5);
await expect(heightDifference).toBeLessThanOrEqual(5);
});
});
/* eslint-enable sort-keys-fix/sort-keys-fix */
3 changes: 3 additions & 0 deletions packages/lexical-playground/__tests__/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function initialize({
tableCellBackgroundColor,
shouldUseLexicalContextMenu,
tableHorizontalScroll,
retainSelection,
}) {
// Tests with legacy events often fail to register keypress, so
// slowing it down to reduce flakiness
Expand Down Expand Up @@ -115,6 +116,7 @@ export async function initialize({
appSettings.tableCellBackgroundColor = tableCellBackgroundColor;
}
appSettings.shouldUseLexicalContextMenu = !!shouldUseLexicalContextMenu;
appSettings.retainSelection = !!retainSelection;

const urlParams = appSettingsToURLParams(appSettings);
const url = `http://localhost:${E2E_PORT}/${
Expand Down Expand Up @@ -171,6 +173,7 @@ export const test = base.extend({
isPlainText: IS_PLAIN_TEXT,
isRichText: IS_RICH_TEXT,
legacyEvents: LEGACY_EVENTS,
retainSelection: false,
shouldUseLexicalContextMenu: false,
});

Expand Down

0 comments on commit 4100839

Please sign in to comment.