|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | + |
| 3 | +/** |
| 4 | + * Unit tests for detectUserContext() and TRAINER_FLAG_SELECTOR in |
| 5 | + * src/core/storage.js. |
| 6 | + * |
| 7 | + * The module touches `document`, so it is imported and exercised in the |
| 8 | + * browser via page.evaluate against a controlled DOM. A deliberately empty |
| 9 | + * fixture (blank-page.html) is used so no GramFrame instance interferes with |
| 10 | + * detection. |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * Set document.body.innerHTML to `html`, import the storage module, and return |
| 15 | + * the result of detectUserContext(). |
| 16 | + * @param {import('@playwright/test').Page} page |
| 17 | + * @param {string} html - markup to place in the body before detection |
| 18 | + * @returns {Promise<'trainer' | 'student'>} |
| 19 | + */ |
| 20 | +async function detectWith(page, html) { |
| 21 | + return page.evaluate(async (markup) => { |
| 22 | + document.body.innerHTML = markup |
| 23 | + const mod = await import('/src/core/storage.js') |
| 24 | + return mod.detectUserContext() |
| 25 | + }, html) |
| 26 | +} |
| 27 | + |
| 28 | +test.beforeEach(async ({ page }) => { |
| 29 | + await page.goto('/tests/fixtures/blank-page.html') |
| 30 | +}) |
| 31 | + |
| 32 | +test('returns "trainer" for the legacy id="gf-persistent" flag', async ({ page }) => { |
| 33 | + const context = await detectWith(page, '<span id="gf-persistent" hidden></span>') |
| 34 | + expect(context).toBe('trainer') |
| 35 | +}) |
| 36 | + |
| 37 | +test('returns "trainer" for the class="gf-persistent" flag (DITA outputclass)', async ({ page }) => { |
| 38 | + const context = await detectWith(page, '<span class="p edition-instructor gf-persistent"></span>') |
| 39 | + expect(context).toBe('trainer') |
| 40 | +}) |
| 41 | + |
| 42 | +test('returns "trainer" for the data-gf-persistent attribute flag', async ({ page }) => { |
| 43 | + const context = await detectWith(page, '<span data-gf-persistent></span>') |
| 44 | + expect(context).toBe('trainer') |
| 45 | +}) |
| 46 | + |
| 47 | +test('returns "trainer" for the legacy ANALYSIS anchor heuristic', async ({ page }) => { |
| 48 | + const context = await detectWith(page, '<a href="#">ANALYSIS</a>') |
| 49 | + expect(context).toBe('trainer') |
| 50 | +}) |
| 51 | + |
| 52 | +test('returns "student" when no flag is present', async ({ page }) => { |
| 53 | + const context = await detectWith(page, '<p>Ordinary student page content</p>') |
| 54 | + expect(context).toBe('student') |
| 55 | +}) |
| 56 | + |
| 57 | +test('TRAINER_FLAG_SELECTOR is exported and matches all three flag forms', async ({ page }) => { |
| 58 | + const result = await page.evaluate(async () => { |
| 59 | + const mod = await import('/src/core/storage.js') |
| 60 | + document.body.innerHTML = |
| 61 | + '<span id="gf-persistent"></span>' + |
| 62 | + '<span class="gf-persistent"></span>' + |
| 63 | + '<span data-gf-persistent></span>' |
| 64 | + return { |
| 65 | + selector: mod.TRAINER_FLAG_SELECTOR, |
| 66 | + matches: document.querySelectorAll(mod.TRAINER_FLAG_SELECTOR).length |
| 67 | + } |
| 68 | + }) |
| 69 | + expect(typeof result.selector).toBe('string') |
| 70 | + expect(result.selector).toContain('#gf-persistent') |
| 71 | + expect(result.selector).toContain('.gf-persistent') |
| 72 | + expect(result.selector).toContain('[data-gf-persistent]') |
| 73 | + expect(result.matches).toBe(3) |
| 74 | +}) |
0 commit comments