|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | + |
| 3 | +test.describe("Ambient Search in Home page", () => { |
| 4 | + test.beforeEach(async ({ page }) => { |
| 5 | + await page.goto("./"); |
| 6 | + }); |
| 7 | + |
| 8 | + test("typing anywhere auto-focuses the search bar and captures the key", async ({ page }) => { |
| 9 | + const searchBar = page.getByTestId("main-search-bar"); |
| 10 | + await expect(searchBar).toBeVisible(); |
| 11 | + |
| 12 | + await page.locator("body").click(); |
| 13 | + |
| 14 | + await page.keyboard.press("n"); |
| 15 | + |
| 16 | + await expect(searchBar).toBeFocused(); |
| 17 | + await expect(searchBar).toHaveValue("n"); |
| 18 | + }); |
| 19 | + |
| 20 | + test("a full search can be done via ambient key presses", async ({ page }) => { |
| 21 | + const searchBar = page.getByTestId("main-search-bar"); |
| 22 | + await expect(searchBar).toBeVisible(); |
| 23 | + |
| 24 | + await page.locator("body").click(); |
| 25 | + |
| 26 | + // page.keyboard.type is too fast for Elm update so add a delay |
| 27 | + // delay 30, 100 was not enough |
| 28 | + await page.keyboard.type("python-web", { delay: 200 }); |
| 29 | + |
| 30 | + await expect(searchBar).toBeFocused(); |
| 31 | + await expect(searchBar).toHaveValue("python-web"); |
| 32 | + |
| 33 | + const results = page.getByTestId("app-result"); |
| 34 | + await expect(results).toHaveCount(1); |
| 35 | + }); |
| 36 | + |
| 37 | + test("esc key should clear the search field", async ({ page }) => { |
| 38 | + const searchBar = page.getByTestId("main-search-bar"); |
| 39 | + await expect(searchBar).toBeVisible(); |
| 40 | + |
| 41 | + await page.locator("body").click(); |
| 42 | + |
| 43 | + await page.keyboard.type("non-existent-app-for-test", { delay: 200 }); |
| 44 | + |
| 45 | + await expect(searchBar).toBeFocused(); |
| 46 | + await expect(searchBar).toHaveValue("non-existent-app-for-test"); |
| 47 | + |
| 48 | + let results = page.getByTestId("app-result"); |
| 49 | + await expect(results).toHaveCount(0); |
| 50 | + |
| 51 | + await page.keyboard.press("Escape"); |
| 52 | + await expect(searchBar).toHaveValue(""); |
| 53 | + |
| 54 | + results = page.getByTestId("app-result"); |
| 55 | + await expect(await results.count()).toBeGreaterThan(0); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +test.describe("Ambient Search in App page", () => { |
| 60 | + test.beforeEach(async ({ page }) => { |
| 61 | + await page.goto("./"); |
| 62 | + |
| 63 | + const firstApp = page.getByTestId("app-result"); |
| 64 | + await expect(firstApp).toBeVisible(); |
| 65 | + await firstApp.click(); |
| 66 | + }); |
| 67 | + |
| 68 | + test("ambient search works in app page", async ({ page }) => { |
| 69 | + const searchBar = page.getByTestId("main-search-bar"); |
| 70 | + await expect(searchBar).toBeVisible(); |
| 71 | + |
| 72 | + await page.locator("body").click(); |
| 73 | + |
| 74 | + await page.keyboard.press("n"); |
| 75 | + |
| 76 | + await expect(searchBar).toBeFocused(); |
| 77 | + await expect(searchBar).toHaveValue("n"); |
| 78 | + }); |
| 79 | + |
| 80 | + test("ambient search is disabled when a modal is open", async ({ page }) => { |
| 81 | + const runBtn = page.getByTestId("app-run-button"); |
| 82 | + await expect(runBtn).toBeVisible(); |
| 83 | + await runBtn.click(); |
| 84 | + |
| 85 | + const modal = page.getByTestId("run-modal-container"); |
| 86 | + await expect(modal).toBeVisible(); |
| 87 | + |
| 88 | + await page.keyboard.press("n"); |
| 89 | + |
| 90 | + const searchBar = page.getByTestId("main-search-bar"); |
| 91 | + await expect(searchBar).not.toBeFocused(); |
| 92 | + }); |
| 93 | + |
| 94 | + test("esc key will bring it back to app page for single keypress", async ({ page }) => { |
| 95 | + const currentAppPage = page.url(); |
| 96 | + const searchBar = page.getByTestId("main-search-bar"); |
| 97 | + await expect(searchBar).toBeVisible(); |
| 98 | + |
| 99 | + await page.locator("body").click(); |
| 100 | + |
| 101 | + await page.keyboard.press("n"); |
| 102 | + |
| 103 | + await expect(searchBar).toBeFocused(); |
| 104 | + await expect(searchBar).toHaveValue("n"); |
| 105 | + |
| 106 | + await page.keyboard.press("Escape"); |
| 107 | + await expect(searchBar).toHaveValue(""); |
| 108 | + |
| 109 | + await expect(page).toHaveURL(currentAppPage); |
| 110 | + }); |
| 111 | + |
| 112 | + test("esc key will bring it to apps list view after typing a word", async ({ page }) => { |
| 113 | + const searchBar = page.getByTestId("main-search-bar"); |
| 114 | + await expect(searchBar).toBeVisible(); |
| 115 | + |
| 116 | + await page.locator("body").click(); |
| 117 | + |
| 118 | + await page.keyboard.type("something", { delay: 200 }); |
| 119 | + |
| 120 | + await expect(searchBar).toBeFocused(); |
| 121 | + await expect(searchBar).toHaveValue("something"); |
| 122 | + |
| 123 | + await page.keyboard.press("Escape"); |
| 124 | + await expect(searchBar).toHaveValue(""); |
| 125 | + |
| 126 | + const results = page.getByTestId("app-result"); |
| 127 | + await expect(await results.count()).toBeGreaterThan(0); |
| 128 | + }); |
| 129 | +}); |
0 commit comments