|
1 |
| -import { stringify, StringifyExtension, type UserFlow } from "@puppeteer/replay" |
| 1 | +import { |
| 2 | + Selector, |
| 3 | + stringify, |
| 4 | + StringifyExtension, |
| 5 | + type LineWriter, |
| 6 | + type NavigateStep, |
| 7 | + type Step, |
| 8 | + type UserFlow, |
| 9 | +} from "@puppeteer/replay" |
2 | 10 |
|
3 |
| -export class Extension extends StringifyExtension {} |
| 11 | +export class Extension extends StringifyExtension { |
| 12 | + async beforeAllSteps(out: LineWriter, flow: UserFlow) { |
| 13 | + // Jest docblock |
| 14 | + out.appendLine("/**") |
| 15 | + const step = flow.steps.find((step) => step.type === "navigate") as |
| 16 | + | NavigateStep |
| 17 | + | undefined |
| 18 | + if (step) { |
| 19 | + out.appendLine(" * @jest-environment url") |
| 20 | + out.appendLine(` * @jest-environment-options { "url": "${step.url}" }`) |
| 21 | + } else { |
| 22 | + out.appendLine(" * @jest-environment jsdom") |
| 23 | + } |
| 24 | + out.appendLine(" */") |
| 25 | + |
| 26 | + // Imports |
| 27 | + out.appendLine( |
| 28 | + 'const { screen, waitFor } = require("@testing-library/dom")', |
| 29 | + ) |
| 30 | + out.appendLine('require("@testing-library/jest-dom")') |
| 31 | + |
| 32 | + out.appendLine("") |
| 33 | + |
| 34 | + // Test |
| 35 | + out.appendLine(`test(${JSON.stringify(flow.title)}, async () => {`) |
| 36 | + out.startBlock() |
| 37 | + } |
| 38 | + |
| 39 | + async afterAllSteps(out: LineWriter) { |
| 40 | + // Test |
| 41 | + out.endBlock() |
| 42 | + out.appendLine("})") |
| 43 | + } |
| 44 | + |
| 45 | + stringifySelector(selector: Selector) { |
| 46 | + const selectorString = Array.isArray(selector) ? selector[0] : selector |
| 47 | + |
| 48 | + if (selectorString.startsWith("aria/")) { |
| 49 | + return `screen.getByText("${selectorString.slice(5)}")` |
| 50 | + } else { |
| 51 | + return `document.querySelector("${selectorString}")` |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + async stringifyStep(out: LineWriter, step: Step, flow: UserFlow) { |
| 56 | + // Events |
| 57 | + switch (step.type) { |
| 58 | + case "click": |
| 59 | + out.appendLine( |
| 60 | + `userEvent.click(${this.stringifySelector(step.selectors[0])}")`, |
| 61 | + ) |
| 62 | + break |
| 63 | + case "waitForElement": |
| 64 | + out.appendLine( |
| 65 | + `await waitFor(() => ${this.stringifySelector(step.selectors[0])})`, |
| 66 | + ) |
| 67 | + break |
| 68 | + case "navigate": |
| 69 | + case "setViewport": |
| 70 | + break |
| 71 | + default: |
| 72 | + console.log( |
| 73 | + `Warning: Testing Library does not currently handle migrating steps of type: ${step.type}. Please check the output to see how this might affect your test.`, |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + // Assertions |
| 78 | + if (step.type === "navigate") { |
| 79 | + if (step === flow.steps.find((step) => step.type === "navigate")) { |
| 80 | + for (const { url, title } of step.assertedEvents ?? []) { |
| 81 | + if (url) { |
| 82 | + out.appendLine(`expect(location.href).toBe("${url}")`) |
| 83 | + } |
| 84 | + if (title) { |
| 85 | + out.appendLine(`expect(document.title).toBe("${title}")`) |
| 86 | + } |
| 87 | + } |
| 88 | + } else { |
| 89 | + console.log( |
| 90 | + "Warning: Testing Library does not currently handle more than one navigation step per test.", |
| 91 | + ) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
4 | 96 |
|
5 | 97 | if (process.env.NODE_ENV !== "test") {
|
6 | 98 | class RecorderPlugin {
|
|
0 commit comments