Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

script-duplication.spec.ts

138 lines4.8 KB

1import { expect, test } from '@playwright/test'
2​
3test.describe('Script Duplication Prevention', () => {
4 test('should not create duplicate scripts on SSR route', async ({ page }) => {
5 await page.goto('/scripts')
6​
7 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
8​
9 const scriptCount = await page.evaluate(() => {
10 return document.querySelectorAll('script[src="script.js"]').length
11 })
12​
13 expect(scriptCount).toBe(1)
14 expect(await page.evaluate('window.SCRIPT_1')).toBe(true)
15 })
16​
17 test('should not create duplicate scripts during client-side navigation', async ({
18 page,
19 }) => {
20 await page.goto('/')
21​
22 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
23 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
24​
25 const firstNavCount = await page.evaluate(() => {
26 return document.querySelectorAll('script[src="script.js"]').length
27 })
28 expect(firstNavCount).toBe(1)
29​
30 await page.getByRole('link', { name: 'Home' }).click()
31 await expect(page.getByRole('link', { name: 'Posts' })).toBeVisible()
32​
33 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
34 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
35​
36 const secondNavCount = await page.evaluate(() => {
37 return document.querySelectorAll('script[src="script.js"]').length
38 })
39 expect(secondNavCount).toBe(1)
40 expect(await page.evaluate('window.SCRIPT_1')).toBe(true)
41 })
42​
43 test('should not create duplicate scripts with multiple navigation cycles', async ({
44 page,
45 }) => {
46 await page.goto('/')
47​
48 for (let i = 0; i < 3; i++) {
49 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
50 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
51​
52 await page.getByRole('link', { name: 'Home' }).click()
53 await expect(page.getByRole('link', { name: 'Posts' })).toBeVisible()
54 }
55​
56 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
57 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
58​
59 const finalCount = await page.evaluate(() => {
60 return document.querySelectorAll('script[src="script.js"]').length
61 })
62 expect(finalCount).toBe(1)
63 expect(await page.evaluate('window.SCRIPT_1')).toBe(true)
64 })
65​
66 test('should not create duplicate inline scripts', async ({ page }) => {
67 await page.goto('/inline-scripts')
68​
69 await expect(
70 page.getByTestId('inline-scripts-test-heading'),
71 ).toBeInViewport()
72​
73 const script1Count = await page.evaluate(() => {
74 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
75 return scripts.filter(
76 (script) =>
77 script.textContent &&
78 script.textContent.includes('window.INLINE_SCRIPT_1 = true'),
79 ).length
80 })
81​
82 const script2Count = await page.evaluate(() => {
83 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
84 return scripts.filter(
85 (script) =>
86 script.textContent &&
87 script.textContent.includes('window.INLINE_SCRIPT_2 = "test"'),
88 ).length
89 })
90​
91 expect(script1Count).toBe(1)
92 expect(script2Count).toBe(1)
93 expect(await page.evaluate('window.INLINE_SCRIPT_1')).toBe(true)
94 expect(await page.evaluate('window.INLINE_SCRIPT_2')).toBe('test')
95 })
96​
97 test('should not create duplicate inline scripts during client-side navigation', async ({
98 page,
99 }) => {
100 await page.goto('/')
101​
102 await page.getByRole('link', { name: 'Inline Scripts' }).click()
103 await expect(
104 page.getByTestId('inline-scripts-test-heading'),
105 ).toBeInViewport()
106​
107 const firstNavScript1Count = await page.evaluate(() => {
108 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
109 return scripts.filter(
110 (script) =>
111 script.textContent &&
112 script.textContent.includes('window.INLINE_SCRIPT_1 = true'),
113 ).length
114 })
115 expect(firstNavScript1Count).toBe(1)
116​
117 await page.getByRole('link', { name: 'Home' }).click()
118 await expect(page.getByRole('link', { name: 'Posts' })).toBeVisible()
119​
120 await page.getByRole('link', { name: 'Inline Scripts' }).click()
121 await expect(
122 page.getByTestId('inline-scripts-test-heading'),
123 ).toBeInViewport()
124​
125 const secondNavScript1Count = await page.evaluate(() => {
126 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
127 return scripts.filter(
128 (script) =>
129 script.textContent &&
130 script.textContent.includes('window.INLINE_SCRIPT_1 = true'),
131 ).length
132 })
133 expect(secondNavScript1Count).toBe(1)
134​
135 expect(await page.evaluate('window.INLINE_SCRIPT_1')).toBe(true)
136 expect(await page.evaluate('window.INLINE_SCRIPT_2')).toBe('test')
137 })
138})