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

script-duplication.spec.ts

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