forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublic-dynamic-fragments.e2e.ts
More file actions
141 lines (125 loc) · 4.31 KB
/
Copy pathpublic-dynamic-fragments.e2e.ts
File metadata and controls
141 lines (125 loc) · 4.31 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
import { expect, test, type Browser, type Page } from '@playwright/test'
import {
ANONYMOUS_STATE,
PUBLIC_BASE_URL,
canvasFrame,
createPage,
insertNotchModule,
login,
openSiteEditor,
publishDraft,
saveDraft,
setPropValue,
} from './helpers'
interface PublicResponseRecord {
url: string
status: number
contentType: string
}
/**
* PUBLIC-004 — route-query dynamic text should publish as a static shell and
* hydrate through the real browser hole runtime.
*/
test.describe('public dynamic fragments', () => {
test.use({ storageState: ANONYMOUS_STATE })
test('hydrates route-query text holes on desktop and mobile visitor pages (PUBLIC-004)', async ({
page,
browser,
}) => {
const suffix = Date.now().toString(36)
const pageName = `Dynamic Fragment ${suffix}`
const slug = `dynamic-fragment-${suffix}`
const authoredText = 'Dynamic result: {route.query.term}'
await login(page)
await openSiteEditor(page)
await createPage(page, pageName, slug)
await page.getByRole('treeitem', { name: `Open page ${pageName}` }).click()
await insertNotchModule(page, 'text')
await setPropValue(page, 'text', authoredText)
await expect(canvasFrame(page).getByText(/Dynamic result:/)).toBeVisible()
await saveDraft(page)
await publishDraft(page)
await expectDynamicFragmentVisitor(browser, {
path: `/${slug}?term=desktop-${suffix}`,
expectedText: `Dynamic result: desktop-${suffix}`,
})
await expectDynamicFragmentVisitor(browser, {
path: `/${slug}?term=mobile-${suffix}`,
expectedText: `Dynamic result: mobile-${suffix}`,
viewport: { width: 390, height: 844 },
})
})
})
async function expectDynamicFragmentVisitor(
browser: Browser,
options: {
path: string
expectedText: string
viewport?: { width: number; height: number }
},
): Promise<void> {
const context = await browser.newContext(
options.viewport ? { viewport: options.viewport } : {},
)
const visitor = await context.newPage()
const holeResponses: PublicResponseRecord[] = []
const runtimeResponses: PublicResponseRecord[] = []
visitor.on('response', (response) => {
const url = new URL(response.url())
if (url.pathname.startsWith('/_instatic/hole/')) {
holeResponses.push(responseRecord(response.url(), response.status(), response.headers()))
}
if (url.pathname === '/_instatic/hole-runtime.js') {
runtimeResponses.push(responseRecord(response.url(), response.status(), response.headers()))
}
})
try {
const response = await visitor.goto(`${PUBLIC_BASE_URL}${options.path}`)
expect(response, 'public page response').not.toBeNull()
expect(response!.status(), 'public page status').toBe(200)
const shellHtml = await response!.text()
expect(shellHtml).toContain('<instatic-hole')
expect(shellHtml).toContain('/_instatic/hole-runtime.js')
expect(shellHtml).not.toContain(options.expectedText)
await expect(visitor.getByText(options.expectedText, { exact: true })).toBeVisible({
timeout: 10_000,
})
await expect(visitor.locator('instatic-hole')).toHaveCount(0)
await expect(visitor.locator('[data-testid="canvas-root"]')).toHaveCount(0)
await expectNoHorizontalOverflow(visitor)
await expect.poll(() => runtimeResponses.length, {
message: 'hole runtime asset response observed',
}).toBeGreaterThan(0)
await expect.poll(() => holeResponses.length, {
message: 'hole fragment response observed',
}).toBeGreaterThan(0)
const runtime = runtimeResponses[0]!
expect(runtime.status).toBe(200)
expect(runtime.contentType).toContain('javascript')
const hole = holeResponses[0]!
expect(hole.status).toBe(200)
expect(hole.contentType).toContain('text/html')
const holeUrl = new URL(hole.url)
expect(holeUrl.searchParams.get('u')).toBe(options.path)
} finally {
await context.close()
}
}
function responseRecord(
url: string,
status: number,
headers: Record<string, string>,
): PublicResponseRecord {
return {
url,
status,
contentType: headers['content-type'] ?? '',
}
}
async function expectNoHorizontalOverflow(page: Page): Promise<void> {
await expect.poll(async () => {
return page.evaluate(() => (
document.documentElement.scrollWidth <= document.documentElement.clientWidth + 1
))
}).toBe(true)
}