|
| 1 | +import { test, expect, type Locator } from "@playwright/test" |
| 2 | + |
| 3 | +test.describe("interactive examples", () => { |
| 4 | + test("adds appearsIn field to hero query and gets correct response", async ({ |
| 5 | + page, |
| 6 | + }) => { |
| 7 | + await page.goto("/learn") |
| 8 | + await page.waitForSelector(".CodeMirror", { timeout: 10000 }) |
| 9 | + |
| 10 | + const editors = page.locator(".miniGraphiQL") |
| 11 | + let heroEditor: Locator | null = null |
| 12 | + |
| 13 | + for (let i = 0; i < (await editors.count()); i++) { |
| 14 | + const editor = editors.nth(i) |
| 15 | + const content = await editor.textContent() |
| 16 | + if (content && content.includes("hero")) { |
| 17 | + heroEditor = editor |
| 18 | + break |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + if (!heroEditor) { |
| 23 | + throw new Error("Could not find hero GraphQL editor") |
| 24 | + } |
| 25 | + |
| 26 | + const codeMirrorEditor = heroEditor.locator(".CodeMirror").first() |
| 27 | + await expect(codeMirrorEditor).toBeVisible() |
| 28 | + |
| 29 | + await codeMirrorEditor.click() |
| 30 | + |
| 31 | + const codeLines = codeMirrorEditor.locator(".CodeMirror-line") |
| 32 | + |
| 33 | + // Find the line containing "name" and click after it |
| 34 | + for (let i = 0; i < (await codeLines.count()); i++) { |
| 35 | + const line = codeLines.nth(i) |
| 36 | + const lineText = await line.textContent() |
| 37 | + if (lineText && lineText.includes("name")) { |
| 38 | + await line.click() |
| 39 | + // Move to end of line |
| 40 | + await page.keyboard.press("End") |
| 41 | + // Add new line |
| 42 | + await page.keyboard.press("Enter") |
| 43 | + break |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + await page.keyboard.type("ap") |
| 48 | + await page.keyboard.press("Control+Space") |
| 49 | + |
| 50 | + const autoCompleteMenu = page.locator(".CodeMirror-hints") |
| 51 | + await expect(autoCompleteMenu).toBeVisible({ timeout: 5000 }) |
| 52 | + |
| 53 | + const appearsInSuggestion = page |
| 54 | + .locator(".CodeMirror-hints li") |
| 55 | + .filter({ hasText: "appearsIn" }) |
| 56 | + |
| 57 | + if (await appearsInSuggestion.isVisible()) { |
| 58 | + await appearsInSuggestion.click() |
| 59 | + } else { |
| 60 | + await page.keyboard.press("Enter") |
| 61 | + } |
| 62 | + |
| 63 | + const resultViewer = heroEditor.locator(".result-window") |
| 64 | + await expect(resultViewer).toBeVisible() |
| 65 | + |
| 66 | + await expect |
| 67 | + .poll(async () => { |
| 68 | + const resultContent = await resultViewer.textContent() |
| 69 | + const jsonMatch = resultContent?.match(/\{[\s\S]*\}/) |
| 70 | + if (jsonMatch) { |
| 71 | + const responseJson = JSON.parse(jsonMatch[0]) |
| 72 | + return responseJson |
| 73 | + } |
| 74 | + |
| 75 | + return {} |
| 76 | + }) |
| 77 | + .toStrictEqual({ |
| 78 | + data: { |
| 79 | + hero: { |
| 80 | + name: "R2-D2", |
| 81 | + appearsIn: ["NEWHOPE", "EMPIRE", "JEDI"], |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + test("edits variables and receives an expected mutation result", async ({ |
| 88 | + page, |
| 89 | + }) => { |
| 90 | + await page.goto("/learn/mutations") |
| 91 | + await page.waitForLoadState("networkidle") |
| 92 | + |
| 93 | + // Find the mutation example that has GraphiQL enabled |
| 94 | + const editors = page.locator(".miniGraphiQL") |
| 95 | + let mutationEditor: Locator | null = null |
| 96 | + |
| 97 | + for (let i = 0; i < (await editors.count()); i++) { |
| 98 | + const editor = editors.nth(i) |
| 99 | + const content = await editor.textContent() |
| 100 | + if (content && content.includes("CreateReviewForEpisode")) { |
| 101 | + mutationEditor = editor |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (!mutationEditor) { |
| 107 | + throw new Error("Could not find mutation GraphQL editor") |
| 108 | + } |
| 109 | + |
| 110 | + const variableEditor = mutationEditor.locator(".variable-editor").first() |
| 111 | + |
| 112 | + if (await variableEditor.isVisible()) { |
| 113 | + await variableEditor.click() |
| 114 | + |
| 115 | + await page.getByText('"This is a great movie!"').first().click() |
| 116 | + await page.keyboard.press("ControlOrMeta+ArrowRight") |
| 117 | + for (let i = 0; i < 4; i++) |
| 118 | + await page.keyboard.press("Alt+Shift+ArrowLeft") |
| 119 | + await page.keyboard.type('almost as good as Andor"') |
| 120 | + |
| 121 | + const resultViewer = mutationEditor.locator(".result-window") |
| 122 | + await expect(resultViewer).toBeVisible() |
| 123 | + |
| 124 | + await expect |
| 125 | + .poll(async () => { |
| 126 | + const resultContent = await resultViewer.textContent() |
| 127 | + const jsonMatch = resultContent?.match(/\{[\s\S]*\}/) |
| 128 | + if (jsonMatch) { |
| 129 | + const responseJson = JSON.parse(jsonMatch[0]) |
| 130 | + return responseJson |
| 131 | + } |
| 132 | + return {} |
| 133 | + }) |
| 134 | + .toStrictEqual({ |
| 135 | + data: { |
| 136 | + createReview: { |
| 137 | + stars: 5, |
| 138 | + commentary: "This is almost as good as Andor", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }) |
| 142 | + } |
| 143 | + }) |
| 144 | +}) |
0 commit comments