|
| 1 | +// @vitest-environment jsdom |
| 2 | +// Component test beside its component, like the other deployments tests: typing and folding have no route seam. |
| 3 | +import { EditorView } from "@codemirror/view"; |
| 4 | +import { createElement } from "react"; |
| 5 | +import { createRoot, type Root } from "react-dom/client"; |
| 6 | +import { act } from "react-dom/test-utils"; |
| 7 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 8 | +import { OperatingSystemContextProvider } from "../primitives/OperatingSystemProvider"; |
| 9 | +import { ShortcutsProvider } from "../primitives/ShortcutsProvider"; |
| 10 | +import { IgnoredPathsEditor } from "./IgnoredPathsEditor"; |
| 11 | + |
| 12 | +globalThis.ResizeObserver ??= class { |
| 13 | + observe() {} |
| 14 | + unobserve() {} |
| 15 | + disconnect() {} |
| 16 | +}; |
| 17 | + |
| 18 | +let container: HTMLDivElement; |
| 19 | +let root: Root; |
| 20 | + |
| 21 | +function mount(defaultPatterns: string[] = [], limits = { maxPaths: 100, maxLength: 256 }) { |
| 22 | + const onChange = vi.fn(); |
| 23 | + container = document.createElement("div"); |
| 24 | + document.body.append(container); |
| 25 | + root = createRoot(container); |
| 26 | + act(() => { |
| 27 | + root.render( |
| 28 | + createElement( |
| 29 | + OperatingSystemContextProvider, |
| 30 | + { platform: "mac" }, |
| 31 | + createElement( |
| 32 | + ShortcutsProvider, |
| 33 | + null, |
| 34 | + createElement(IgnoredPathsEditor, { |
| 35 | + name: "ignorePaths", |
| 36 | + defaultPatterns, |
| 37 | + limits, |
| 38 | + onChange, |
| 39 | + }) |
| 40 | + ) |
| 41 | + ) |
| 42 | + ); |
| 43 | + }); |
| 44 | + return onChange; |
| 45 | +} |
| 46 | + |
| 47 | +afterEach(() => { |
| 48 | + act(() => root.unmount()); |
| 49 | + container.remove(); |
| 50 | +}); |
| 51 | + |
| 52 | +function editor() { |
| 53 | + return EditorView.findFromDOM(container.querySelector<HTMLElement>(".cm-editor")!)!; |
| 54 | +} |
| 55 | + |
| 56 | +function edit(text: string) { |
| 57 | + const view = editor(); |
| 58 | + act(() => { |
| 59 | + view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: text } }); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +function submitted() { |
| 64 | + return container.querySelector<HTMLInputElement>('input[name="ignorePaths"]')!.value; |
| 65 | +} |
| 66 | + |
| 67 | +function button(label: string) { |
| 68 | + return container.querySelector<HTMLButtonElement>(`button[aria-label="${label}"]`); |
| 69 | +} |
| 70 | + |
| 71 | +function tester() { |
| 72 | + return container.querySelector<HTMLInputElement>('input[aria-label="Path to test"]'); |
| 73 | +} |
| 74 | + |
| 75 | +function type(value: string) { |
| 76 | + act(() => { |
| 77 | + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call( |
| 78 | + tester(), |
| 79 | + value |
| 80 | + ); |
| 81 | + tester()!.dispatchEvent(new Event("input", { bubbles: true })); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function press(key: string) { |
| 86 | + const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }); |
| 87 | + act(() => { |
| 88 | + tester()!.dispatchEvent(event); |
| 89 | + }); |
| 90 | + return event.defaultPrevented; |
| 91 | +} |
| 92 | + |
| 93 | +function verdict() { |
| 94 | + return [...container.querySelector('[role="status"]')!.children].map((part) => part.textContent); |
| 95 | +} |
| 96 | + |
| 97 | +function errors() { |
| 98 | + return [...container.querySelectorAll("li")].map((item) => item.textContent); |
| 99 | +} |
| 100 | + |
| 101 | +describe("IgnoredPathsEditor", () => { |
| 102 | + it("folds more than three saved paths and expands into the editor", () => { |
| 103 | + const paths = "docs/**\napps/website/**\n**/*.md\n.github/**\nexamples/**"; |
| 104 | + mount(paths.split("\n")); |
| 105 | + |
| 106 | + expect(button("Expand")).not.toBeNull(); |
| 107 | + expect(submitted()).toBe(paths); |
| 108 | + |
| 109 | + act(() => button("Expand")!.click()); |
| 110 | + expect(editor().hasFocus).toBe(true); |
| 111 | + expect(button("Collapse")).not.toBeNull(); |
| 112 | + |
| 113 | + act(() => button("Collapse")!.click()); |
| 114 | + expect(button("Expand")).not.toBeNull(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("starts with a note that every push deploys and reports typed paths", () => { |
| 118 | + const onChange = mount(); |
| 119 | + expect(container.textContent).toContain( |
| 120 | + "No paths yet, so every push to a tracked branch deploys." |
| 121 | + ); |
| 122 | + expect(button("Expand")).toBeNull(); |
| 123 | + |
| 124 | + edit(" docs/** \n\n*.md\n"); |
| 125 | + |
| 126 | + expect(submitted()).toBe(" docs/** \n\n*.md\n"); |
| 127 | + expect(onChange).toHaveBeenLastCalledWith(["docs/**", "*.md"], true); |
| 128 | + expect(container.textContent).not.toContain("No paths yet"); |
| 129 | + }); |
| 130 | + |
| 131 | + it("lists each invalid line and marks the editor invalid", () => { |
| 132 | + const onChange = mount(); |
| 133 | + edit("docs/**\n!src/**\nexamples/\n**/*.{md,mdx}"); |
| 134 | + |
| 135 | + expect(errors()).toEqual([ |
| 136 | + "Line 2: Negated patterns aren't supported. List only the paths to ignore.", |
| 137 | + "Line 3: Matches no files. Use examples/** for everything in the directory.", |
| 138 | + "Line 4: Braces aren't supported. Put each alternative on its own line.", |
| 139 | + ]); |
| 140 | + expect(container.textContent).toContain("3 errors"); |
| 141 | + expect(editor().contentDOM.getAttribute("aria-invalid")).toBe("true"); |
| 142 | + expect(onChange).toHaveBeenLastCalledWith( |
| 143 | + ["docs/**", "!src/**", "examples/", "**/*.{md,mdx}"], |
| 144 | + false |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it("applies the configured path count and length limits", () => { |
| 149 | + const onChange = mount(["docs/**", "*.md"], { maxPaths: 2, maxLength: 10 }); |
| 150 | + |
| 151 | + edit("docs/**\n*.md\nexamples/**/*.ts"); |
| 152 | + |
| 153 | + expect(errors()).toEqual(["Line 3: Use at most 10 characters.", "Add at most 2 paths."]); |
| 154 | + expect(onChange).toHaveBeenLastCalledWith(["docs/**", "*.md", "examples/**/*.ts"], false); |
| 155 | + }); |
| 156 | + |
| 157 | + it("tests a path against the saved paths while folded", () => { |
| 158 | + mount(["docs/**", "apps/website/**", "**/*.md", ".github/**"]); |
| 159 | + act(() => button("Test a path")!.click()); |
| 160 | + expect(document.activeElement).toBe(tester()); |
| 161 | + expect(button("Expand")).not.toBeNull(); |
| 162 | + |
| 163 | + type("apps/website/src/index.tsx"); |
| 164 | + expect(verdict()).toEqual(["Ignored", "by apps/website/**"]); |
| 165 | + |
| 166 | + type("src/index.ts"); |
| 167 | + expect(verdict()).toEqual(["Deploys", "no path matches"]); |
| 168 | + |
| 169 | + expect(press("Enter")).toBe(true); |
| 170 | + press("Escape"); |
| 171 | + expect(tester()).toBeNull(); |
| 172 | + expect(document.activeElement).toBe(button("Test a path")); |
| 173 | + |
| 174 | + act(() => button("Test a path")!.click()); |
| 175 | + expect(tester()!.value).toBe(""); |
| 176 | + }); |
| 177 | + |
| 178 | + it("updates the verdict when the paths change", () => { |
| 179 | + mount(["docs/**", "*.md"]); |
| 180 | + act(() => button("Test a path")!.click()); |
| 181 | + type("docs/guide.md"); |
| 182 | + expect(verdict()).toEqual(["Ignored", "by docs/**"]); |
| 183 | + |
| 184 | + edit("src/**\n**/*.md"); |
| 185 | + expect(verdict()).toEqual(["Ignored", "by **/*.md"]); |
| 186 | + |
| 187 | + edit("src/**\n!docs/**"); |
| 188 | + expect(verdict()).toEqual(["Deploys", "no path matches"]); |
| 189 | + }); |
| 190 | + |
| 191 | + it("offers the tester only once there is a valid path", () => { |
| 192 | + mount(); |
| 193 | + expect(button("Test a path")).toBeNull(); |
| 194 | + |
| 195 | + edit("!src/**"); |
| 196 | + expect(button("Test a path")).toBeNull(); |
| 197 | + |
| 198 | + edit("!src/**\ndocs/**"); |
| 199 | + expect(button("Test a path")).not.toBeNull(); |
| 200 | + |
| 201 | + act(() => button("Test a path")!.click()); |
| 202 | + edit("!src/**"); |
| 203 | + expect(button("Close the path tester")).not.toBeNull(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments