-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorpus-fixture.ts
More file actions
105 lines (96 loc) · 3.13 KB
/
corpus-fixture.ts
File metadata and controls
105 lines (96 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { vi } from "vitest"
// Mock MutationObserver for tests
global.MutationObserver = vi.fn().mockImplementation(() => ({
disconnect: vi.fn(),
observe: vi.fn(),
takeRecords: vi.fn(() => []),
}))
// Mock the OverType editor component
vi.mock("overtype", () => {
const mockConstructor = vi.fn().mockImplementation(() => [
{
container: document.createElement("div"),
destroy: vi.fn(),
focus: vi.fn(),
getValue: vi.fn(() => ""),
preview: document.createElement("div"),
setValue: vi.fn(),
textarea: document.createElement("textarea"),
updatePreview: vi.fn(),
wrapper: document.createElement("div"),
},
])
;(mockConstructor as any).setCodeHighlighter = vi.fn()
;(mockConstructor as any).setTheme = vi.fn()
return {
default: mockConstructor,
}
})
import { describe as baseDescribe, test as baseTest, expect } from "vitest"
import type { StrippedLocation } from "@/lib/enhancer"
import { EnhancerRegistry } from "../src/lib/registries"
import type { CORPUS } from "./corpus/_corpus-index"
import { cleanupDOM, setupDOM } from "./corpus-utils"
export const describe = baseDescribe
// Re-export expect from vitest
export { expect }
// Fluent interface for any corpus type (HAR or HTML)
export function withCorpus(corpusKey: keyof typeof CORPUS) {
return {
it: (name: string, fn: () => void | Promise<void>) => {
return baseTest(`${String(corpusKey)}:${name}`, async () => {
// Setup DOM for any corpus type (delegates to HAR or HTML based on type)
await setupDOM(corpusKey)
try {
return await fn()
} finally {
// Cleanup after test
cleanupDOM()
}
})
},
}
}
// Helper function for detection tests
export function detectedSpots() {
const enhancers = new EnhancerRegistry()
const textareas = document.querySelectorAll("textarea")
const location: StrippedLocation = {
host: window.location.host,
pathname: window.location.pathname,
search: window.location.search,
}
const detectionResults = []
for (const textarea of textareas) {
const enhanced = enhancers.tryToEnhance(textarea, location)
const forValue = `id=${textarea.id} name=${textarea.name} className=${textarea.className}`
detectionResults.push({
for: forValue,
spot: enhanced ? enhanced.spot : "NO_SPOT",
})
}
return detectionResults
}
// Helper function for UI tests
export function tableUI() {
const enhancers = new EnhancerRegistry()
const textareas = document.querySelectorAll("textarea")
const location: StrippedLocation = {
host: window.location.host,
pathname: window.location.pathname,
search: window.location.search,
}
const uiResults = []
for (const textarea of textareas) {
const enhanced = enhancers.tryToEnhance(textarea, location)
const forValue = `id=${textarea.id} name=${textarea.name} className=${textarea.className}`
if (enhanced) {
uiResults.push({
for: forValue,
title: enhanced.enhancer.tableTitle(enhanced.spot),
upperDecoration: enhanced.enhancer.tableUpperDecoration(enhanced.spot),
})
}
}
return uiResults
}