|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import * as fc from 'fast-check' |
| 3 | +import { render, screen, cleanup } from '@testing-library/react' |
| 4 | +import { Header } from '../Header' |
| 5 | + |
| 6 | +// Arbitrary for valid URLs |
| 7 | +const urlArb = fc.webUrl() |
| 8 | + |
| 9 | +// Arbitrary for valid title strings (problem number + Chinese title) |
| 10 | +const titleArb = fc |
| 11 | + .tuple(fc.integer({ min: 1, max: 3000 }), fc.string({ minLength: 1, maxLength: 20 })) |
| 12 | + .map(([num, name]) => `${num}. ${name}`) |
| 13 | + |
| 14 | +/** |
| 15 | + * **Feature: page-title-navigation, Property 1: External links open in new tab** |
| 16 | + * **Validates: Requirements 1.3, 2.3, 3.1, 3.2** |
| 17 | + * |
| 18 | + * For any external link (LeetCode problem link or back link) rendered by the Header component, |
| 19 | + * the link element SHALL have `target="_blank"` attribute set. |
| 20 | + */ |
| 21 | +describe('Property 1: External links open in new tab', () => { |
| 22 | + it('all external links should have target="_blank"', () => { |
| 23 | + fc.assert( |
| 24 | + fc.property(titleArb, urlArb, urlArb, urlArb, (title, leetcodeUrl, githubUrl, backUrl) => { |
| 25 | + cleanup() |
| 26 | + const { container } = render( |
| 27 | + <Header |
| 28 | + title={title} |
| 29 | + leetcodeUrl={leetcodeUrl} |
| 30 | + githubUrl={githubUrl} |
| 31 | + backUrl={backUrl} |
| 32 | + /> |
| 33 | + ) |
| 34 | + |
| 35 | + // Get all anchor elements |
| 36 | + const links = container.querySelectorAll('a') |
| 37 | + |
| 38 | + // All links should have target="_blank" |
| 39 | + links.forEach((link) => { |
| 40 | + expect(link.getAttribute('target')).toBe('_blank') |
| 41 | + }) |
| 42 | + cleanup() |
| 43 | + }), |
| 44 | + { numRuns: 100 } |
| 45 | + ) |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +/** |
| 50 | + * **Feature: page-title-navigation, Property 2: External links have security attributes** |
| 51 | + * **Validates: Requirements 3.3** |
| 52 | + * |
| 53 | + * For any external link rendered by the Header component with `target="_blank"`, |
| 54 | + * the link element SHALL have `rel="noopener noreferrer"` attribute set. |
| 55 | + */ |
| 56 | +describe('Property 2: External links have security attributes', () => { |
| 57 | + it('all external links with target="_blank" should have rel="noopener noreferrer"', () => { |
| 58 | + fc.assert( |
| 59 | + fc.property(titleArb, urlArb, urlArb, urlArb, (title, leetcodeUrl, githubUrl, backUrl) => { |
| 60 | + cleanup() |
| 61 | + const { container } = render( |
| 62 | + <Header |
| 63 | + title={title} |
| 64 | + leetcodeUrl={leetcodeUrl} |
| 65 | + githubUrl={githubUrl} |
| 66 | + backUrl={backUrl} |
| 67 | + /> |
| 68 | + ) |
| 69 | + |
| 70 | + // Get all anchor elements with target="_blank" |
| 71 | + const links = container.querySelectorAll('a[target="_blank"]') |
| 72 | + |
| 73 | + // All such links should have rel="noopener noreferrer" |
| 74 | + links.forEach((link) => { |
| 75 | + expect(link.getAttribute('rel')).toBe('noopener noreferrer') |
| 76 | + }) |
| 77 | + cleanup() |
| 78 | + }), |
| 79 | + { numRuns: 100 } |
| 80 | + ) |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +/** |
| 85 | + * **Feature: page-title-navigation, Property 3: Title text preservation** |
| 86 | + * **Validates: Requirements 1.1** |
| 87 | + * |
| 88 | + * For any valid problem title string passed to the Header component, |
| 89 | + * the rendered title text SHALL contain the original title string unchanged. |
| 90 | + */ |
| 91 | +describe('Property 3: Title text preservation', () => { |
| 92 | + it('title text should be rendered unchanged', () => { |
| 93 | + fc.assert( |
| 94 | + fc.property(titleArb, urlArb, urlArb, (title, leetcodeUrl, githubUrl) => { |
| 95 | + cleanup() |
| 96 | + const { container } = render( |
| 97 | + <Header title={title} leetcodeUrl={leetcodeUrl} githubUrl={githubUrl} /> |
| 98 | + ) |
| 99 | + |
| 100 | + // Find the title link element and check its text content |
| 101 | + const titleLink = container.querySelector('a[class*="title"]') |
| 102 | + expect(titleLink).not.toBeNull() |
| 103 | + expect(titleLink?.textContent).toBe(title) |
| 104 | + cleanup() |
| 105 | + }), |
| 106 | + { numRuns: 100 } |
| 107 | + ) |
| 108 | + }) |
| 109 | +}) |
0 commit comments