|
| 1 | +// @ts-check |
| 2 | +const { test, expect } = require("@playwright/test"); |
| 3 | +const { login } = require("../helpers/auth"); |
| 4 | +const { navigateToTraining } = require("../helpers/navigation"); |
| 5 | + |
| 6 | +test.describe("Training plugin - certificate / badge selection", () => { |
| 7 | + test.beforeEach(async ({ page }) => { |
| 8 | + await login(page); |
| 9 | + await navigateToTraining(page); |
| 10 | + }); |
| 11 | + |
| 12 | + test("should list available certificates from the API", async ({ page }) => { |
| 13 | + const select = page.locator("#select-certificate select").first(); |
| 14 | + await expect(select).toBeVisible(); |
| 15 | + // Wait for the certs API before checking options |
| 16 | + await page.waitForResponse((resp) => resp.url().includes('/plugin/training/certs') && resp.status() === 200, { timeout: 15_000 }); |
| 17 | + // There should be at least 1 real option beyond the placeholder |
| 18 | + const options = select.locator("option:not([disabled])"); |
| 19 | + const count = await options.count(); |
| 20 | + expect(count).toBeGreaterThanOrEqual(1); |
| 21 | + }); |
| 22 | + |
| 23 | + test("should show Red Certificate option", async ({ page }) => { |
| 24 | + const select = page.locator("#select-certificate select").first(); |
| 25 | + await expect(select).toBeVisible(); |
| 26 | + await page.waitForResponse((resp) => resp.url().includes('/plugin/training/certs') && resp.status() === 200, { timeout: 15_000 }); |
| 27 | + const redOption = select.locator('option:has-text("Red")'); |
| 28 | + const redCount = await redOption.count(); |
| 29 | + if (redCount === 0) { |
| 30 | + test.skip(true, "No Red Certificate option available on this server"); |
| 31 | + return; |
| 32 | + } |
| 33 | + expect(redCount).toBeGreaterThanOrEqual(1); |
| 34 | + }); |
| 35 | + |
| 36 | + test("should show Blue Certificate option", async ({ page }) => { |
| 37 | + const select = page.locator("#select-certificate select").first(); |
| 38 | + await expect(select).toBeVisible(); |
| 39 | + await page.waitForResponse((resp) => resp.url().includes('/plugin/training/certs') && resp.status() === 200, { timeout: 15_000 }); |
| 40 | + const blueOption = select.locator('option:has-text("Blue")'); |
| 41 | + const blueCount = await blueOption.count(); |
| 42 | + if (blueCount === 0) { |
| 43 | + test.skip(true, "No Blue Certificate option available on this server"); |
| 44 | + return; |
| 45 | + } |
| 46 | + expect(blueCount).toBeGreaterThanOrEqual(1); |
| 47 | + }); |
| 48 | + |
| 49 | + test("selecting a certificate should load badges", async ({ page }) => { |
| 50 | + const select = page.locator("#select-certificate select").first(); |
| 51 | + await expect(select).toBeVisible(); |
| 52 | + |
| 53 | + // Pick the first non-disabled option |
| 54 | + const firstOption = select.locator("option:not([disabled])").first(); |
| 55 | + const optionValue = await firstOption.getAttribute("value"); |
| 56 | + if (!optionValue) return; // guard |
| 57 | + |
| 58 | + await select.selectOption(optionValue); |
| 59 | + |
| 60 | + // Badges should appear - they render as .badge-container-button elements |
| 61 | + const badges = page.locator(".badge-container-button, .badge-text"); |
| 62 | + await expect(badges.first()).toBeVisible({ timeout: 15_000 }); |
| 63 | + }); |
| 64 | + |
| 65 | + test("clicking a badge should filter visible flags", async ({ page }) => { |
| 66 | + const select = page.locator("#select-certificate select").first(); |
| 67 | + await expect(select).toBeVisible(); |
| 68 | + |
| 69 | + const firstOption = select.locator("option:not([disabled])").first(); |
| 70 | + const optionValue = await firstOption.getAttribute("value"); |
| 71 | + if (!optionValue) return; |
| 72 | + await select.selectOption(optionValue); |
| 73 | + |
| 74 | + // Wait for badges |
| 75 | + const badges = page.locator(".badge-container-button"); |
| 76 | + await expect(badges.first()).toBeVisible({ timeout: 15_000 }); |
| 77 | + |
| 78 | + // Count initial flags |
| 79 | + const flagsBefore = page.locator(".flag-card"); |
| 80 | + await expect(flagsBefore.first()).toBeVisible({ timeout: 10_000 }); |
| 81 | + const totalFlags = await flagsBefore.count(); |
| 82 | + |
| 83 | + // Click first badge to filter |
| 84 | + await badges.first().click(); |
| 85 | + |
| 86 | + // After filtering, flag count should change (either same or fewer) |
| 87 | + const flagsAfter = page.locator(".flag-card"); |
| 88 | + const filteredCount = await flagsAfter.count(); |
| 89 | + expect(filteredCount).toBeLessThanOrEqual(totalFlags); |
| 90 | + expect(filteredCount).toBeGreaterThanOrEqual(1); |
| 91 | + }); |
| 92 | + |
| 93 | + test("clicking a selected badge again should deselect it and show all flags", async ({ page }) => { |
| 94 | + const select = page.locator("#select-certificate select").first(); |
| 95 | + await expect(select).toBeVisible(); |
| 96 | + |
| 97 | + const firstOption = select.locator("option:not([disabled])").first(); |
| 98 | + const optionValue = await firstOption.getAttribute("value"); |
| 99 | + if (!optionValue) return; |
| 100 | + await select.selectOption(optionValue); |
| 101 | + |
| 102 | + const badges = page.locator(".badge-container-button"); |
| 103 | + await expect(badges.first()).toBeVisible({ timeout: 15_000 }); |
| 104 | + const flagsBefore = page.locator(".flag-card"); |
| 105 | + await expect(flagsBefore.first()).toBeVisible({ timeout: 10_000 }); |
| 106 | + const totalFlags = await flagsBefore.count(); |
| 107 | + |
| 108 | + // Select then deselect |
| 109 | + await badges.first().click(); |
| 110 | + await badges.first().click(); |
| 111 | + |
| 112 | + // Should show all flags again |
| 113 | + const flagsAfter = page.locator(".flag-card"); |
| 114 | + const restoredCount = await flagsAfter.count(); |
| 115 | + expect(restoredCount).toBe(totalFlags); |
| 116 | + }); |
| 117 | + |
| 118 | + test("selected badge should have the selected-badge CSS class", async ({ page }) => { |
| 119 | + const select = page.locator("#select-certificate select").first(); |
| 120 | + await expect(select).toBeVisible(); |
| 121 | + |
| 122 | + const firstOption = select.locator("option:not([disabled])").first(); |
| 123 | + const optionValue = await firstOption.getAttribute("value"); |
| 124 | + if (!optionValue) return; |
| 125 | + await select.selectOption(optionValue); |
| 126 | + |
| 127 | + const badges = page.locator(".badge-container-button"); |
| 128 | + await expect(badges.first()).toBeVisible({ timeout: 15_000 }); |
| 129 | + |
| 130 | + await badges.first().click(); |
| 131 | + await expect(badges.first()).toHaveClass(/selected-badge/); |
| 132 | + }); |
| 133 | +}); |
0 commit comments