|
| 1 | +import { beforeAll, describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { get } from '@/tests/helpers/e2etest' |
| 4 | + |
| 5 | +const makeURL = (pathname: string): string => |
| 6 | + `/api/article/body?${new URLSearchParams({ pathname })}` |
| 7 | + |
| 8 | +describe('article body api', () => { |
| 9 | + beforeAll(() => { |
| 10 | + // If you didn't set the `ROOT` variable, the tests will fail rather |
| 11 | + // cryptically. So as a warning for engineers running these tests, |
| 12 | + // alert in case it was accidentally forgotten. |
| 13 | + if (!process.env.ROOT) { |
| 14 | + console.warn( |
| 15 | + 'WARNING: The article body tests require the ROOT environment variable to be set to the fixture root', |
| 16 | + ) |
| 17 | + } |
| 18 | + }) |
| 19 | + |
| 20 | + test('happy path', async () => { |
| 21 | + const res = await get(makeURL('/en/get-started/start-your-journey/hello-world')) |
| 22 | + expect(res.statusCode).toBe(200) |
| 23 | + expect(res.body).toContain('## Introduction') |
| 24 | + expect(res.body).toContain('This is just a test.') |
| 25 | + expect(res.headers['content-type']).toContain('text/markdown') |
| 26 | + }) |
| 27 | + |
| 28 | + test('octicons auto-generate aria-labels', async () => { |
| 29 | + const res = await get(makeURL('/en/get-started/start-your-journey/hello-world')) |
| 30 | + expect(res.statusCode).toBe(200) |
| 31 | + |
| 32 | + // Check that octicons without aria-label get auto-generated ones |
| 33 | + expect(res.body).toContain('aria-label="check icon"') |
| 34 | + expect(res.body).toContain('aria-label="git branch icon"') |
| 35 | + }) |
| 36 | + |
| 37 | + test('octicons with custom aria-labels use the custom value', async () => { |
| 38 | + const res = await get(makeURL('/en/get-started/start-your-journey/hello-world')) |
| 39 | + expect(res.statusCode).toBe(200) |
| 40 | + |
| 41 | + // Check that custom aria-labels are preserved |
| 42 | + expect(res.body).toContain('aria-label="Supported"') |
| 43 | + expect(res.body).toContain('aria-label="Not supported"') |
| 44 | + }) |
| 45 | + |
| 46 | + test('octicons with other attributes still get auto-generated aria-labels', async () => { |
| 47 | + const res = await get(makeURL('/en/get-started/start-your-journey/hello-world')) |
| 48 | + expect(res.statusCode).toBe(200) |
| 49 | + |
| 50 | + // Check that octicons with width attribute still get aria-labels |
| 51 | + expect(res.body).toContain('aria-label="rocket icon"') |
| 52 | + expect(res.body).toContain('width="32"') |
| 53 | + }) |
| 54 | + |
| 55 | + test('a pathname that does not exist', async () => { |
| 56 | + const res = await get(makeURL('/en/never/heard/of')) |
| 57 | + expect(res.statusCode).toBe(404) |
| 58 | + const { error } = JSON.parse(res.body) |
| 59 | + expect(error).toBe("No page found for '/en/never/heard/of'") |
| 60 | + }) |
| 61 | + |
| 62 | + test('non-article pages return error', async () => { |
| 63 | + // Index pages are not articles and should not be renderable |
| 64 | + const res = await get(makeURL('/en/get-started')) |
| 65 | + expect(res.statusCode).toBe(403) |
| 66 | + const { error } = JSON.parse(res.body) |
| 67 | + expect(error).toContain("isn't yet available in markdown") |
| 68 | + }) |
| 69 | +}) |
0 commit comments