forked from vercel/next.js
/
index.test.ts
226 lines (195 loc) · 6.03 KB
/
index.test.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import webdriver from 'next-webdriver'
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { BrowserInterface } from 'test/lib/browsers/base'
import { waitFor } from 'next-test-utils'
describe('experimental.nextScriptWorkers: false with no Partytown dependency', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"
strategy="worker"
/>
</>
)
}
`,
},
// TODO: @housseindjirdeh: verify React 18 functionality
dependencies: {
react: '17.0.2',
'react-dom': '17.0.2',
},
})
})
afterAll(() => next.destroy())
it('Partytown snippet is not injected to head if not enabled in configuration', async () => {
let browser: BrowserInterface
try {
browser = await webdriver(next.url, '/')
const snippetScript = await browser.eval(
`document.querySelector('script[data-partytown]')`
)
expect(snippetScript).toEqual(null)
} finally {
if (browser) await browser.close()
}
})
})
describe('experimental.nextScriptWorkers: true with required Partytown dependency', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
nextConfig: {
experimental: {
nextScriptWorkers: true,
},
dependencies: {
react: '17',
'react-dom': '17',
},
},
files: {
'pages/index.js': `
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"
strategy="worker"
/>
</>
)
}
`,
},
dependencies: {
'@builder.io/partytown': '0.4.2',
},
})
})
afterAll(() => next.destroy())
it('Partytown snippets are injected to head if enabled in configuration', async () => {
let browser: BrowserInterface
try {
browser = await webdriver(next.url, '/')
const snippetScript = await browser.eval(
`document.querySelector('script[data-partytown]').innerHTML`
)
const configScript = await browser.eval(
`document.querySelector('script[data-partytown-config]').innerHTML`
)
expect(snippetScript).not.toEqual(null)
// A default config is included that points to the correct folder that hosts partytown's static files
expect(configScript).not.toEqual(null)
expect(configScript.replace(/(?: *[\n\r])+ */g, '')).toEqual(
'partytown = {lib: "/_next/static/~partytown/"};'
)
} finally {
if (browser) await browser.close()
}
})
it('Worker scripts are modified by Partytown to execute on a worker thread', async () => {
let browser: BrowserInterface
try {
browser = await webdriver(next.url, '/')
const predefinedWorkerScripts = await browser.eval(
`document.querySelectorAll('script[type="text/partytown"]').length`
)
expect(predefinedWorkerScripts).toBeGreaterThan(0)
await waitFor(1000)
// Partytown modifes type to "text/partytown-x" after it has been executed in the web worker
const processedWorkerScripts = await browser.eval(
`document.querySelectorAll('script[type="text/partytown-x"]').length`
)
expect(processedWorkerScripts).toBeGreaterThan(0)
} finally {
if (browser) await browser.close()
}
})
})
describe('experimental.nextScriptWorkers: true with config override', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
nextConfig: {
experimental: {
nextScriptWorkers: true,
},
},
files: {
'pages/_document.js': `
import Document, { Html, Head, Main, NextScript } from "next/document";
class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<script
data-partytown-config
dangerouslySetInnerHTML={{
__html: \`
partytown = {
lib: "/_next/static/~partytown/",
debug: true
};
\`,
}}
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;
`,
'pages/index.js': `
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"
strategy="worker"
/>
</>
)
}
`,
},
dependencies: {
'@builder.io/partytown': '0.4.2',
react: '17',
'react-dom': '17',
},
})
})
afterAll(() => next.destroy())
it('Partytown config script is overwritten', async () => {
let browser: BrowserInterface
try {
browser = await webdriver(next.url, '/')
const configScript = await browser.eval(
`document.querySelector('script[data-partytown-config]').innerHTML`
)
expect(configScript).not.toEqual(null)
expect(configScript.replace(/(?: *[\n\r])+ */g, '')).toEqual(
'partytown = {lib: "/_next/static/~partytown/",debug: true};'
)
} finally {
if (browser) await browser.close()
}
})
})