-
Notifications
You must be signed in to change notification settings - Fork 629
Expand file tree
/
Copy pathssr-fixture.ts
More file actions
160 lines (137 loc) · 5.23 KB
/
Copy pathssr-fixture.ts
File metadata and controls
160 lines (137 loc) · 5.23 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { Page } from "@playwright/test";
import { CSRFixture, type TemplateOrOptions } from "./csr-fixture.js";
export class SSRFixture extends CSRFixture {
/**
* Styles buffered before {@link setTemplate} is called.
*/
private pendingStyles: Parameters<Page["addStyleTag"]>[0][] = [];
/**
* Whether the template has been rendered.
*/
private templateRendered = false;
/**
* Creates an instance of the SSRFixture.
*
* @param page - The Playwright page object.
* @param tagName - The tag name of the custom element.
* @param innerHTML - The inner HTML of the custom element.
* @param waitFor - Additional custom elements to wait for.
* @param testId - The test ID for the SSR fixture.
* @param testTitle - The test title for the SSR fixture.
*/
constructor(
page: Page,
tagName: string,
innerHTML: string = "",
waitFor: string[] = [],
private readonly testId?: string,
private readonly testTitle?: string,
) {
super(page, tagName, innerHTML, waitFor);
}
/**
* Buffers style tags added before {@link setTemplate} so they can be
* included in the generated SSR page. After the template has been
* rendered, calls pass through to the page directly.
*
* @param options - The options for the style tag.
* @see {@link Page.addStyleTag}
*/
override async addStyleTag(
options: Parameters<Page["addStyleTag"]>[0],
): Promise<void> {
if (this.templateRendered) {
await this.page.addStyleTag(options);
return;
}
this.pendingStyles.push(options);
}
/**
* Sets up the test fixture by posting the template or configuration
* to the SSR generation endpoint.
*
* This method constructs a request body based on the provided
* `templateOrOptions` and the current test context (like `testId`
* and `testTitle`). It sends this data to the `/generate-fixture`
* endpoint to create a server-side rendered fixture, navigates the
* page to the resulting URL, and waits for the page to stabilize.
*
* @param templateOrOptions - The template configuration.
*/
override async setTemplate(templateOrOptions?: TemplateOrOptions): Promise<void> {
const body: Record<string, string> = {};
if (this.testId) {
body.testId = this.testId;
body.testTitle = this.testTitle || this.formatTestTitle(this.testId);
}
if (typeof templateOrOptions === "string") {
body.html = templateOrOptions.trim();
}
if (typeof templateOrOptions === "object") {
if (typeof templateOrOptions.innerHTML === "string") {
body.innerHTML = templateOrOptions.innerHTML;
}
if (templateOrOptions.attributes) {
const cleanedAttributes: Record<string, string | true> = {};
Object.entries(templateOrOptions.attributes).forEach(([key, value]) => {
cleanedAttributes[key.trim()] =
typeof value === "string" ? value.trim() : value;
});
body.attributes = JSON.stringify(cleanedAttributes);
}
}
if (!body.html && typeof templateOrOptions !== "string") {
body.tagName = this.tagName;
if (!body.innerHTML && typeof templateOrOptions?.innerHTML !== "string") {
body.innerHTML = this.innerHTML;
}
}
Object.entries(body).forEach(([key, value]) => {
if (typeof value === "string") {
body[key] = value.replace(/\s+/g, " ").trim();
}
});
if (this.pendingStyles.length) {
body.styles = JSON.stringify(
this.pendingStyles.map(s => s?.content).filter((c): c is string => !!c),
);
}
const response = await this.page.request.post("/generate-fixture", {
data: body,
});
if (!response.ok()) {
throw new Error(
`Failed to generate fixture: ${response.status()} ${response.statusText()}`,
);
}
const result = await response.json();
if (!result.url) {
throw new Error(`Invalid response from server: ${JSON.stringify(result)}`);
}
await this.page.goto(result.url);
await this.waitForStability();
this.templateRendered = true;
this.pendingStyles.length = 0;
}
/**
* Formats the test title based on the provided test ID.
*/
private formatTestTitle(testId: string): string {
const match = testId.match(/^(.*?\.(ts|js))-(.+)$/);
if (!match) {
return testId
.split("_")
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
}
const filePath = match[1];
const testParts = match[3];
const sections = testParts.split("-").map(section =>
section
.split("_")
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(" "),
);
return `${sections.join(" › ")} (${filePath})`;
}
}