Skip to content

Commit

Permalink
test: add test for checking wrong answer
Browse files Browse the repository at this point in the history
  • Loading branch information
shootermv committed Aug 12, 2024
1 parent 2364752 commit 9a7cfe3
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion e2e/score-and-results.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { test, expect } from "@playwright/test";
import htmlQuizQuestions from "../src/data/html-quiz";
import { correctModalResponses } from "../src/data/modal-responses";
import {
correctModalResponses,
incorrectModalResponses
} from "../src/data/modal-responses";

test.beforeEach(async ({ page }) => {
await page.goto("/#/quizzes");
Expand Down Expand Up @@ -49,3 +52,47 @@ test("should show 'success' modal after selecting the correct option", async ({

await expect(page.getByTestId("modal-points")).toHaveText("Points: 1");
});

test("should show 'failure' modal after selecting the WRONG option", async ({
page
}) => {
await page.getByRole("button", { name: "HTML" }).click();

await page.getByRole("button", { name: "10", exact: true }).click();

// get points num before answering the question
await expect(page.locator(".quiz-text")).toContainText("Points: 0");

// find which option is correct
// 1. get the question text
const legend = await page.locator("legend");
const questionText = await legend.textContent();
// 2. find the question inside questions of the category
const questionData = htmlQuizQuestions.find(({ Question }) =>
questionText.includes(Question)
);

if (!questionData) {
console.log("QUESTION NOT FOUND IN LIST!!!");
}
// 3. find the 'Distractor' option
const questionDistractor = questionData.Distractor1;
// 4. select correct option
await page.getByRole("button", { name: questionDistractor }).click();

// Click the submit button
await page.getByRole("button").nth(4).click();

await expect(page.getByRole("dialog")).toBeVisible();

const modalDialog = await page.getByRole("dialog");

// get the contents of modal-text
const failureLocator = await modalDialog.locator("h2");
let failureText = await failureLocator.textContent();
failureText = failureText.replace("😔", "").replace(" ", "");

expect(incorrectModalResponses).toContain(failureText);

await expect(page.getByTestId("modal-points")).toHaveText("Points: 0");
});

0 comments on commit 9a7cfe3

Please sign in to comment.