123 lines
6.7 KiB
JavaScript
123 lines
6.7 KiB
JavaScript
// Writer find bar in the top editor panel: shortcuts, replacement, query sync, invalid regex, tooltip, outline filtering, focus return, and narrow layout.
|
|
import assert from "node:assert/strict";
|
|
import path from "node:path";
|
|
import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
|
|
|
|
await runSmoke("find/searchPanel", async ({ page }) => {
|
|
const output = await outputDir("find/searchPanel");
|
|
await mockTauri(page, {
|
|
commands: test => {
|
|
const workspace = test.workspace({
|
|
root: "C:\\smoke-workspace",
|
|
writeEvents: true,
|
|
files: {
|
|
"notes/welcome.md": "---\ntitle: Welcome\n---\n\n# Working notes\n\nFirst paragraph.\n\n- [ ] A task\n\n[[second]]\n",
|
|
"notes/second.md": "# Second note\n",
|
|
"src/hello.py": "print('hello')\n",
|
|
},
|
|
});
|
|
return {
|
|
...workspace,
|
|
workspace_write: args => {
|
|
if (args.expected !== undefined && workspace.files.get(args.rel) !== args.expected) throw new Error("Save blocked: file changed on disk");
|
|
return workspace.workspace_write(args);
|
|
},
|
|
workspace_search: () => [],
|
|
"plugin:dialog|message": () => "Cancel",
|
|
};
|
|
},
|
|
});
|
|
await page.goto(smokeUrl);
|
|
await page.getByText("smoke-workspace", { exact: true }).waitFor();
|
|
await page.keyboard.press("Control+p");
|
|
await page.getByRole("textbox", { name: "Find a file" }).fill("welcome.md");
|
|
await page.getByRole("dialog").getByRole("button").filter({ hasText: "welcome.md" }).first().click();
|
|
const editor = page.getByRole("textbox", { name: "Writing editor", exact: true });
|
|
await editor.waitFor();
|
|
await page.evaluate(async () => {
|
|
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
|
|
window.__CM__ = EditorView.findFromDOM(document.querySelector(".cm-content"));
|
|
window.__CM__.dispatch({ changes: { from: 0, to: window.__CM__.state.doc.length, insert: "# Alpha\n\nalpha Alpha alphabet\n\n## Beta\n\nBeta text\n" }, selection: { anchor: 0 } });
|
|
window.__CM__.focus();
|
|
});
|
|
await page.keyboard.press("Control+f");
|
|
const find = page.getByRole("textbox", { name: "Find in document", exact: true });
|
|
await find.waitFor();
|
|
assert.equal(await find.evaluate(el => document.activeElement === el), true);
|
|
assert.equal(await page.locator(".cm-panels-top .rs-editor-search").count(), 1);
|
|
const panelBounds = await page.locator(".rs-editor-search").boundingBox();
|
|
const scrollBounds = await page.locator(".cm-scroller").boundingBox();
|
|
assert.ok(panelBounds.y + panelBounds.height <= scrollBounds.y + 1);
|
|
await find.fill("Alpha");
|
|
await page.keyboard.press("Enter");
|
|
const first = await page.evaluate(() => window.__CM__.state.selection.main.from);
|
|
await page.keyboard.press("Enter");
|
|
const second = await page.evaluate(() => window.__CM__.state.selection.main.from);
|
|
assert.notEqual(first, second);
|
|
await page.keyboard.press("Shift+Enter");
|
|
assert.equal(await page.evaluate(() => window.__CM__.state.selection.main.from), first);
|
|
await page.getByRole("checkbox", { name: "Match case" }).check();
|
|
await page.getByRole("checkbox", { name: "Whole word" }).check();
|
|
await page.getByRole("textbox", { name: "Replace with" }).fill("Gamma");
|
|
await page.getByRole("button", { name: "Replace all", exact: true }).click();
|
|
assert.match(await page.evaluate(() => window.__CM__.state.sliceDoc()), /# Gamma\n\nalpha Gamma alphabet/);
|
|
await page.getByRole("checkbox", { name: "Regex" }).check();
|
|
await find.fill("[");
|
|
assert.equal(await find.getAttribute("aria-invalid"), "true");
|
|
assert.equal(await page.getByRole("button", { name: "Replace all", exact: true }).isDisabled(), true);
|
|
await page.getByText("Invalid expression", { exact: true }).waitFor();
|
|
await find.fill("Gamma");
|
|
await page.getByRole("checkbox", { name: "Regex" }).uncheck();
|
|
await page.evaluate(async () => {
|
|
const moduleUrl = performance.getEntriesByType("resource").find(entry => entry.name.includes("/@codemirror_search.js"))?.name;
|
|
if (!moduleUrl) throw new Error("Search module was not loaded");
|
|
const { SearchQuery, setSearchQuery } = await import(moduleUrl);
|
|
window.__CM__.dispatch({ effects: setSearchQuery.of(new SearchQuery({ search: "Beta" })) });
|
|
});
|
|
await page.waitForFunction(() => document.querySelector('input[name="search"]').value === "Beta");
|
|
await find.focus();
|
|
await page.keyboard.press("Escape");
|
|
assert.equal(await find.count(), 0);
|
|
assert.equal(await editor.evaluate(el => document.activeElement === el), true);
|
|
await page.keyboard.press("Control+f");
|
|
await find.waitFor();
|
|
await page.getByRole("button", { name: "Next match (Enter)" }).focus();
|
|
await page.getByRole("tooltip", { name: "Next match (Enter)" }).waitFor({ state: "visible" });
|
|
await find.focus();
|
|
|
|
const outline = page.getByRole("region", { name: "Outline", exact: true });
|
|
if (!await outline.count()) await page.getByRole("button", { name: "Outline", exact: true }).click();
|
|
await outline.focus();
|
|
await page.keyboard.press("Control+f");
|
|
const outlineFind = page.getByRole("textbox", { name: "Find in outline", exact: true });
|
|
await outlineFind.fill("beta");
|
|
assert.equal(await outline.locator("li").count(), 1);
|
|
assert.equal(await outline.locator("li button").textContent(), "Beta");
|
|
await outlineFind.fill("missing");
|
|
await outline.getByText("No matching headings or symbols.").waitFor();
|
|
await outlineFind.fill("Gamma");
|
|
await page.screenshot({ path: path.join(output, "find-bars.png") });
|
|
await page.keyboard.press("Escape");
|
|
assert.equal(await outlineFind.count(), 0);
|
|
assert.equal(await outline.locator("li").count(), 2);
|
|
assert.equal(await outline.evaluate(el => document.activeElement === el), true);
|
|
await page.keyboard.press("Control+f");
|
|
await outlineFind.fill("Beta");
|
|
await page.keyboard.press("Enter");
|
|
await page.waitForFunction(() => window.__CM__.state.doc.lineAt(window.__CM__.state.selection.main.head).text === "## Beta");
|
|
assert.equal(await editor.evaluate(el => document.activeElement === el), true);
|
|
|
|
await page.setViewportSize({ width: 880, height: 760 });
|
|
// The outline gives way below 1100 pixels. The test opens it again to check its search field.
|
|
await page.getByRole("button", { name: "Show outline", exact: true }).click();
|
|
await outline.focus();
|
|
await page.keyboard.press("Control+f");
|
|
await outlineFind.waitFor();
|
|
await find.focus();
|
|
await page.screenshot({ path: path.join(output, "find-bars-narrow.png") });
|
|
for (const selector of [".rs-editor-search", ".outline-search"]) {
|
|
assert.equal(await page.locator(selector).evaluate(el => el.scrollWidth <= el.clientWidth), true, selector);
|
|
}
|
|
console.log("PASS: Top search panel, shortcuts, replacement, query sync, invalid regex, focus tooltip, outline filtering, focus return and narrow layout.");
|
|
});
|