Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(chrome-ext): fix test setup to avoid race conditions (#4485)

Elijah Potterb8278ff3c8fb
diff --git a/packages/chrome-plugin/tests/editor_content.spec.ts b/packages/chrome-plugin/tests/editor_content.spec.ts
new file mode 100644
index 000000000..a41e0736a
--- /dev/null
+++ b/packages/chrome-plugin/tests/editor_content.spec.ts
@@ -0,0 +1,23 @@
+import { expect, test } from '@playwright/test';
+import { replaceEditorContent } from './testUtils';
+
+for (const tag of ['input', 'textarea']) {
+ test(`Ensure \`replaceEditorContent\` fills \`${tag}\` without allowing intermediate text to be linted.`, async ({
+ page,
+ }) => {
+ await page.setContent(`<${tag}></${tag}><output></output>`);
+ await page.evaluate(() => {
+ const values: string[] = [];
+ document.addEventListener('input', (event) => {
+ values.push((event.target as HTMLInputElement).value);
+ document.querySelector('output')!.textContent = JSON.stringify(values);
+ });
+ });
+
+ const text = 'This is a mistaek.';
+ await replaceEditorContent(page.locator(tag), text);
+ await expect(page.locator(tag)).toHaveValue(text);
+ // The prefix "This is a m" has a different lint from the completed sentence.
+ await expect(page.locator('output')).toHaveText(JSON.stringify([text]));
+ });
+}
diff --git a/packages/chrome-plugin/tests/review_banner.spec.ts b/packages/chrome-plugin/tests/review_banner.spec.ts
index 52e1b1e34..db838dd2f 100644
--- a/packages/chrome-plugin/tests/review_banner.spec.ts
+++ b/packages/chrome-plugin/tests/review_banner.spec.ts
@@ -23,13 +23,25 @@ test.describe('review banner', () => {
const extensionId = background.url().split('/')[2];
const popupUrl = `chrome-extension://${extensionId}/popup.html`;
- await page.goto(popupUrl);
+
+ // Let startup finish writing the installation date before replacing it.
+ await expect
+ .poll(
+ () =>
+ background.evaluate(async () => {
+ const { installedOn } = await chrome.storage.local.get('installedOn');
+ return typeof installedOn === 'string';
+ }),
+ { timeout: 30000 },
+ )
+ .toBe(true);
await background.evaluate(() =>
chrome.storage.local.set({
installedOn: new Date(Date.now() - 15 * 86400000).toISOString(),
}),
);
+ await page.goto(popupUrl);
await page.getByText("Let's start writing").click();
diff --git a/packages/chrome-plugin/tests/testUtils.ts b/packages/chrome-plugin/tests/testUtils.ts
index 00cd754ad..5493ad188 100644
--- a/packages/chrome-plugin/tests/testUtils.ts
+++ b/packages/chrome-plugin/tests/testUtils.ts
@@ -71,6 +71,13 @@ export function getDraftEditor(page: Page): Locator {
/** Replace the content of a text editor. */
export async function replaceEditorContent(editorEl: Locator, text: string, softBreaks = false) {
+ // Seed form controls in one update so popup tests cannot click lints for partial words.
+ // Rich editors still need their keyboard-driven editing behavior.
+ if (await isFormElement(editorEl)) {
+ await editorEl.fill(text);
+ return;
+ }
+
await editorEl.selectText();
await editorEl.press('Backspace');
@@ -292,8 +299,8 @@ export async function testCanIgnoreSuggestion(
const testText = 'This is a mistaek.';
await replaceEditorContent(editor, testText);
- // Ensure the test text produces only the spelling lint we intend to ignore.
- await expect(getHarperHighlights(page)).toHaveCount(1);
+ // test.slow() does not extend assertion timeouts during linter startup.
+ await expect(getHarperHighlights(page)).toHaveCount(1, { timeout: 30000 });
// Open the popup for the highlight and click Ignore.
const opened = await clickHarperHighlight(page);