|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import htmlQuizQuestions from "../src/data/html-quiz"; |
| 3 | +import { |
| 4 | + correctModalResponses, |
| 5 | + incorrectModalResponses |
| 6 | +} from "../src/data/modal-responses"; |
| 7 | + |
| 8 | +test.beforeEach(async ({ page }) => { |
| 9 | + await page.goto("/#/quizzes"); |
| 10 | +}); |
| 11 | + |
| 12 | +test("should show 'success' modal after selecting the correct option", async ({ |
| 13 | + page |
| 14 | +}) => { |
| 15 | + await page.getByRole("button", { name: "HTML" }).click(); |
| 16 | + |
| 17 | + await page.getByRole("button", { name: "10", exact: true }).click(); |
| 18 | + |
| 19 | + await expect(page.getByText("Points: 0")).toBeVisible(); |
| 20 | + |
| 21 | + const question = await page.locator("legend").textContent(); |
| 22 | + // Find the question inside questions of the category |
| 23 | + const questionData = htmlQuizQuestions.find(({ Question }) => |
| 24 | + question.includes(Question) |
| 25 | + ); |
| 26 | + |
| 27 | + if (!questionData) { |
| 28 | + console.log("Question not found."); |
| 29 | + } |
| 30 | + |
| 31 | + const answer = questionData.Answer; |
| 32 | + |
| 33 | + await page.getByRole("button", { name: answer }).click(); |
| 34 | + await page.getByRole("button", { name: "Submit", exact: true }).click(); |
| 35 | + |
| 36 | + const dialog = page.getByRole("dialog"); |
| 37 | + const message = await dialog.getByRole("heading", { level: 2 }).textContent(); |
| 38 | + const isMessageInExpectedSet = correctModalResponses.some(response => |
| 39 | + message.includes(response) |
| 40 | + ); |
| 41 | + |
| 42 | + await expect(dialog).toBeVisible(); |
| 43 | + expect(isMessageInExpectedSet).toEqual(true); |
| 44 | + await expect(dialog.getByText("Points: 1")).toBeVisible(); |
| 45 | +}); |
| 46 | + |
| 47 | +test("should show 'failure' modal after selecting the wrong option", async ({ |
| 48 | + page |
| 49 | +}) => { |
| 50 | + await page.getByRole("button", { name: "HTML" }).click(); |
| 51 | + |
| 52 | + await page.getByRole("button", { name: "10", exact: true }).click(); |
| 53 | + |
| 54 | + await expect(page.getByText("Points: 0")).toBeVisible(); |
| 55 | + |
| 56 | + const question = await page.locator("legend").textContent(); |
| 57 | + // Find the question inside questions of the category |
| 58 | + const questionData = htmlQuizQuestions.find(({ Question }) => |
| 59 | + question.includes(Question) |
| 60 | + ); |
| 61 | + |
| 62 | + if (!questionData) { |
| 63 | + console.log("Question not found."); |
| 64 | + } |
| 65 | + |
| 66 | + const distractor = questionData.Distractor1; |
| 67 | + |
| 68 | + await page.getByRole("button", { name: distractor }).click(); |
| 69 | + await page.getByRole("button", { name: "Submit", exact: true }).click(); |
| 70 | + |
| 71 | + const dialog = page.getByRole("dialog"); |
| 72 | + const message = await dialog.getByRole("heading", { level: 2 }).textContent(); |
| 73 | + const isMessageInExpectedSet = incorrectModalResponses.some(response => |
| 74 | + message.includes(response) |
| 75 | + ); |
| 76 | + |
| 77 | + await expect(dialog).toBeVisible(); |
| 78 | + expect(isMessageInExpectedSet).toEqual(true); |
| 79 | + await expect(dialog.getByText("Points: 0")).toBeVisible(); |
| 80 | +}); |
0 commit comments