Skip to content

Commit 6ec1090

Browse files
committed
Put the har stuff into a test fixture.
1 parent e9cb9ca commit 6ec1090

File tree

4 files changed

+174
-60
lines changed

4 files changed

+174
-60
lines changed

browser-extension/tests/lib/enhancers/github.test.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import fs from 'node:fs/promises'
2-
import path from 'node:path'
3-
import { fileURLToPath } from 'node:url'
4-
import { parseHTML } from 'linkedom'
5-
import { describe, expect, it, vi } from 'vitest'
1+
import { vi } from 'vitest'
62
import { EnhancerRegistry } from '../../../src/lib/registries'
7-
import { PAGES } from '../../har-index'
3+
import { describe, expect, it } from '../../test-fixtures'
84

95
vi.stubGlobal('defineContentScript', vi.fn())
106
vi.mock('../../../src/overtype/overtype', () => {
@@ -26,43 +22,22 @@ vi.mock('../../../src/overtype/overtype', () => {
2622
}
2723
})
2824

29-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
30-
async function loadHtmlFromHar(key: keyof typeof PAGES): Promise<string> {
31-
const url = PAGES[key]
32-
const harPath = path.join(__dirname, '../../har', `${key}.har`)
33-
const harContent = await fs.readFile(harPath, 'utf-8')
34-
const harData = JSON.parse(harContent)
35-
const mainEntry = harData.log.entries.find((entry: any) => entry.request.url === url)
36-
return mainEntry.response.content.text
37-
}
38-
3925
describe('github', () => {
40-
it('should identify gh_pr textarea and create proper spot object', async () => {
41-
const html = await loadHtmlFromHar('gh_pr')
42-
43-
// Parse HTML with linkedom
44-
const dom = parseHTML(html)
45-
46-
// Replace global document with parsed one
47-
Object.assign(globalThis, {
48-
document: dom.document,
49-
window: {
50-
...dom.window,
51-
location: new URL(PAGES.gh_pr),
52-
},
53-
})
26+
it('should identify gh_pr textarea and create proper spot object', async ({ harDOM }) => {
27+
// Setup DOM from HAR snapshot
28+
await harDOM('gh_pr')
5429

5530
const enhancers = new EnhancerRegistry()
5631
const textareas = document.querySelectorAll('textarea')
5732

58-
let enhanced: any = null
33+
let enhanced: ReturnType<EnhancerRegistry['tryToEnhance']> = null
5934
for (const textarea of textareas) {
6035
enhanced = enhancers.tryToEnhance(textarea as HTMLTextAreaElement)
6136
if (enhanced) break
6237
}
6338

6439
expect(enhanced).toBeTruthy()
65-
expect(enhanced.spot).toMatchInlineSnapshot(`
40+
expect(enhanced?.spot).toMatchInlineSnapshot(`
6641
{
6742
"domain": "github.com",
6843
"number": 517,

browser-extension/tests/setup.ts

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import '@testing-library/jest-dom/vitest'
2-
import { parseHTML } from 'linkedom'
2+
import './test-fixtures'
33

4-
// Set up linkedom globals for browser-like environment
5-
const dom = parseHTML(`
6-
<!DOCTYPE html>
7-
<html>
8-
<head>
9-
<meta charset="utf-8">
10-
<title>Test</title>
11-
</head>
12-
<body></body>
13-
</html>
14-
`)
15-
16-
// Mock global DOM objects
17-
Object.assign(globalThis, {
18-
Document: dom.Document,
19-
DocumentFragment: dom.DocumentFragment,
20-
document: dom.document,
21-
Element: dom.Element,
22-
HTMLDivElement: dom.HTMLDivElement,
23-
HTMLElement: dom.HTMLElement,
24-
HTMLMetaElement: dom.HTMLMetaElement,
25-
HTMLTextAreaElement: dom.HTMLTextAreaElement,
26-
location: dom.window.location,
27-
Node: dom.Node,
28-
Text: dom.Text,
29-
window: dom.window,
30-
})
4+
// Test fixtures are now available globally via test-fixtures.ts
5+
// Individual tests should use the harDOM fixture to load HAR snapshots
6+
// DOM cleanup is handled automatically by the fixture lifecycle
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe as baseDescribe, test as baseTest, expect } from 'vitest'
2+
import { PAGES } from './har-index'
3+
import {
4+
cleanupDOM,
5+
createDOMFromHar,
6+
loadHtmlFromHar,
7+
setupDOMFromHar,
8+
type TestDOMGlobals,
9+
} from './test-utils'
10+
11+
export interface TestFixtures {
12+
harDOM: (key: keyof typeof PAGES) => Promise<TestDOMGlobals>
13+
}
14+
15+
export const test = baseTest.extend<TestFixtures>({
16+
// biome-ignore lint/correctness/noEmptyPattern: Required by Vitest fixture API
17+
harDOM: async ({}, use) => {
18+
let currentDOM: TestDOMGlobals | null = null
19+
20+
const setupDOM = async (key: keyof typeof PAGES): Promise<TestDOMGlobals> => {
21+
// Clean up any existing DOM
22+
if (currentDOM) {
23+
cleanupDOM()
24+
}
25+
26+
// Load HTML from HAR file
27+
const html = await loadHtmlFromHar(key)
28+
const url = PAGES[key]
29+
30+
// Create and setup new DOM
31+
const domGlobals = createDOMFromHar(html, url)
32+
setupDOMFromHar(domGlobals)
33+
34+
currentDOM = domGlobals
35+
return domGlobals
36+
}
37+
38+
// Provide the setup function to the test
39+
await use(setupDOM)
40+
41+
// Cleanup after test completes
42+
if (currentDOM) {
43+
cleanupDOM()
44+
currentDOM = null
45+
}
46+
},
47+
})
48+
49+
export const describe = baseDescribe
50+
export const it = test
51+
52+
// Re-export expect from vitest
53+
export { expect }
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import fs from 'node:fs/promises'
2+
import path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import type { Har as HarFile } from 'har-format'
5+
import { parseHTML } from 'linkedom'
6+
import { PAGES } from './har-index'
7+
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
9+
10+
export interface TestDOMGlobals {
11+
Document: typeof Document
12+
DocumentFragment: typeof DocumentFragment
13+
document: Document
14+
Element: typeof Element
15+
HTMLDivElement: typeof HTMLDivElement
16+
HTMLElement: typeof HTMLElement
17+
HTMLMetaElement: typeof HTMLMetaElement
18+
HTMLTextAreaElement: typeof HTMLTextAreaElement
19+
location: Location
20+
Node: typeof Node
21+
Text: typeof Text
22+
window: Window
23+
}
24+
25+
export interface TestDOMContext {
26+
cleanup: () => void
27+
document: Document
28+
window: Window
29+
url: string
30+
}
31+
32+
let currentDOMInstance: any = null
33+
let originalGlobals: Partial<TestDOMGlobals> = {}
34+
35+
export async function loadHtmlFromHar(key: keyof typeof PAGES): Promise<string> {
36+
const url = PAGES[key]
37+
const harPath = path.join(__dirname, 'har', `${key}.har`)
38+
const harContent = await fs.readFile(harPath, 'utf-8')
39+
const harData: HarFile = JSON.parse(harContent)
40+
const mainEntry = harData.log.entries.find((entry) => entry.request.url === url)
41+
42+
if (!mainEntry) {
43+
throw new Error(`No entry found for URL: ${url} in HAR file: ${harPath}`)
44+
}
45+
46+
return mainEntry.response.content.text || ''
47+
}
48+
49+
export function createDOMFromHar(html: string, url: string): TestDOMGlobals {
50+
const dom = parseHTML(html)
51+
52+
return {
53+
Document: dom.Document,
54+
DocumentFragment: dom.DocumentFragment,
55+
document: dom.document,
56+
Element: dom.Element,
57+
HTMLDivElement: dom.HTMLDivElement,
58+
HTMLElement: dom.HTMLElement,
59+
HTMLMetaElement: dom.HTMLMetaElement,
60+
HTMLTextAreaElement: dom.HTMLTextAreaElement,
61+
location: new URL(url) as any,
62+
Node: dom.Node,
63+
Text: dom.Text,
64+
window: {
65+
...dom.window,
66+
location: new URL(url),
67+
} as any,
68+
}
69+
}
70+
71+
export function setupDOMFromHar(domGlobals: TestDOMGlobals): void {
72+
// Store original globals for cleanup
73+
originalGlobals = {
74+
Document: (globalThis as any).Document,
75+
DocumentFragment: (globalThis as any).DocumentFragment,
76+
document: (globalThis as any).document,
77+
Element: (globalThis as any).Element,
78+
HTMLDivElement: (globalThis as any).HTMLDivElement,
79+
HTMLElement: (globalThis as any).HTMLElement,
80+
HTMLMetaElement: (globalThis as any).HTMLMetaElement,
81+
HTMLTextAreaElement: (globalThis as any).HTMLTextAreaElement,
82+
location: (globalThis as any).location,
83+
Node: (globalThis as any).Node,
84+
Text: (globalThis as any).Text,
85+
window: (globalThis as any).window,
86+
}
87+
88+
// Set new globals
89+
Object.assign(globalThis, domGlobals)
90+
currentDOMInstance = domGlobals
91+
}
92+
93+
export function cleanupDOM(): void {
94+
if (currentDOMInstance) {
95+
// Reset globals to original values
96+
Object.assign(globalThis, originalGlobals)
97+
98+
// Clear references
99+
currentDOMInstance = null
100+
originalGlobals = {}
101+
}
102+
}
103+
104+
export async function setupHarDOM(key: keyof typeof PAGES): Promise<TestDOMGlobals> {
105+
const html = await loadHtmlFromHar(key)
106+
const url = PAGES[key]
107+
const domGlobals = createDOMFromHar(html, url)
108+
setupDOMFromHar(domGlobals)
109+
return domGlobals
110+
}

0 commit comments

Comments
 (0)