@@ -4,147 +4,167 @@ test.describe('Script Duplication Prevention', () => {
44 test ( 'should not create duplicate scripts on SSR route' , async ( { page } ) => {
55 // Navigate directly to scripts route (SSR scenario)
66 await page . goto ( '/scripts' )
7-
7+
88 await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
9-
9+
1010 // Count script tags with src="script.js"
1111 const scriptCount = await page . evaluate ( ( ) => {
1212 return document . querySelectorAll ( 'script[src="script.js"]' ) . length
1313 } )
14-
14+
1515 // Should have exactly one script tag
1616 expect ( scriptCount ) . toBe ( 1 )
17-
17+
1818 // Verify the script executed correctly
1919 expect ( await page . evaluate ( 'window.SCRIPT_1' ) ) . toBe ( true )
2020 } )
2121
22- test ( 'should not create duplicate scripts during client-side navigation' , async ( { page } ) => {
22+ test ( 'should not create duplicate scripts during client-side navigation' , async ( {
23+ page,
24+ } ) => {
2325 // Start from home page
2426 await page . goto ( '/' )
25-
27+
2628 // Navigate to scripts route (client-side navigation)
2729 await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
2830 await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
29-
31+
3032 // Count script tags after first navigation
3133 const firstNavCount = await page . evaluate ( ( ) => {
3234 return document . querySelectorAll ( 'script[src="script.js"]' ) . length
3335 } )
3436 expect ( firstNavCount ) . toBe ( 1 )
35-
37+
3638 // Navigate away from scripts route
3739 await page . getByRole ( 'link' , { name : 'Home' } ) . click ( )
3840 await expect ( page . getByRole ( 'link' , { name : 'Posts' } ) ) . toBeVisible ( )
39-
41+
4042 // Navigate back to scripts route
4143 await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
4244 await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
43-
45+
4446 // Count script tags after second navigation - should still be 1
4547 const secondNavCount = await page . evaluate ( ( ) => {
4648 return document . querySelectorAll ( 'script[src="script.js"]' ) . length
4749 } )
4850 expect ( secondNavCount ) . toBe ( 1 )
49-
51+
5052 // Verify the script is still working
5153 expect ( await page . evaluate ( 'window.SCRIPT_1' ) ) . toBe ( true )
5254 } )
5355
54- test ( 'should not create duplicate scripts with multiple navigation cycles' , async ( { page } ) => {
56+ test ( 'should not create duplicate scripts with multiple navigation cycles' , async ( {
57+ page,
58+ } ) => {
5559 // Start from home page
5660 await page . goto ( '/' )
57-
61+
5862 // Navigate to scripts route multiple times
5963 for ( let i = 0 ; i < 3 ; i ++ ) {
6064 // Go to scripts
6165 await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
6266 await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
63-
67+
6468 // Go back to home
6569 await page . getByRole ( 'link' , { name : 'Home' } ) . click ( )
6670 await expect ( page . getByRole ( 'link' , { name : 'Posts' } ) ) . toBeVisible ( )
6771 }
68-
72+
6973 // Final navigation to scripts
7074 await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
7175 await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
72-
76+
7377 // Count script tags - should still be exactly 1
7478 const finalCount = await page . evaluate ( ( ) => {
7579 return document . querySelectorAll ( 'script[src="script.js"]' ) . length
7680 } )
7781 expect ( finalCount ) . toBe ( 1 )
78-
82+
7983 // Verify the script is still working
8084 expect ( await page . evaluate ( 'window.SCRIPT_1' ) ) . toBe ( true )
8185 } )
8286
8387 test ( 'should not create duplicate inline scripts' , async ( { page } ) => {
8488 // Navigate directly to inline scripts route (SSR scenario)
8589 await page . goto ( '/inline-scripts' )
86-
87- await expect ( page . getByTestId ( 'inline-scripts-test-heading' ) ) . toBeInViewport ( )
88-
90+
91+ await expect (
92+ page . getByTestId ( 'inline-scripts-test-heading' ) ,
93+ ) . toBeInViewport ( )
94+
8995 // Count specific inline scripts
9096 const script1Count = await page . evaluate ( ( ) => {
9197 const scripts = Array . from ( document . querySelectorAll ( 'script:not([src])' ) )
92- return scripts . filter ( script =>
93- script . textContent && script . textContent . includes ( 'window.INLINE_SCRIPT_1 = true' )
98+ return scripts . filter (
99+ ( script ) =>
100+ script . textContent &&
101+ script . textContent . includes ( 'window.INLINE_SCRIPT_1 = true' ) ,
94102 ) . length
95103 } )
96-
104+
97105 const script2Count = await page . evaluate ( ( ) => {
98106 const scripts = Array . from ( document . querySelectorAll ( 'script:not([src])' ) )
99- return scripts . filter ( script =>
100- script . textContent && script . textContent . includes ( 'window.INLINE_SCRIPT_2 = "test"' )
107+ return scripts . filter (
108+ ( script ) =>
109+ script . textContent &&
110+ script . textContent . includes ( 'window.INLINE_SCRIPT_2 = "test"' ) ,
101111 ) . length
102112 } )
103-
113+
104114 // Should have exactly one of each inline script
105115 expect ( script1Count ) . toBe ( 1 )
106116 expect ( script2Count ) . toBe ( 1 )
107-
117+
108118 // Verify the scripts executed correctly
109119 expect ( await page . evaluate ( 'window.INLINE_SCRIPT_1' ) ) . toBe ( true )
110120 expect ( await page . evaluate ( 'window.INLINE_SCRIPT_2' ) ) . toBe ( 'test' )
111121 } )
112122
113- test ( 'should not create duplicate inline scripts during client-side navigation' , async ( { page } ) => {
123+ test ( 'should not create duplicate inline scripts during client-side navigation' , async ( {
124+ page,
125+ } ) => {
114126 // Start from home page
115127 await page . goto ( '/' )
116-
128+
117129 // Navigate to inline scripts route (client-side navigation)
118130 await page . getByRole ( 'link' , { name : 'Inline Scripts' } ) . click ( )
119- await expect ( page . getByTestId ( 'inline-scripts-test-heading' ) ) . toBeInViewport ( )
120-
131+ await expect (
132+ page . getByTestId ( 'inline-scripts-test-heading' ) ,
133+ ) . toBeInViewport ( )
134+
121135 // Count inline scripts after first navigation
122136 const firstNavScript1Count = await page . evaluate ( ( ) => {
123137 const scripts = Array . from ( document . querySelectorAll ( 'script:not([src])' ) )
124- return scripts . filter ( script =>
125- script . textContent && script . textContent . includes ( 'window.INLINE_SCRIPT_1 = true' )
138+ return scripts . filter (
139+ ( script ) =>
140+ script . textContent &&
141+ script . textContent . includes ( 'window.INLINE_SCRIPT_1 = true' ) ,
126142 ) . length
127143 } )
128144 expect ( firstNavScript1Count ) . toBe ( 1 )
129-
145+
130146 // Navigate away and back
131147 await page . getByRole ( 'link' , { name : 'Home' } ) . click ( )
132148 await expect ( page . getByRole ( 'link' , { name : 'Posts' } ) ) . toBeVisible ( )
133-
149+
134150 await page . getByRole ( 'link' , { name : 'Inline Scripts' } ) . click ( )
135- await expect ( page . getByTestId ( 'inline-scripts-test-heading' ) ) . toBeInViewport ( )
136-
151+ await expect (
152+ page . getByTestId ( 'inline-scripts-test-heading' ) ,
153+ ) . toBeInViewport ( )
154+
137155 // Count inline scripts after second navigation - should still be 1
138156 const secondNavScript1Count = await page . evaluate ( ( ) => {
139157 const scripts = Array . from ( document . querySelectorAll ( 'script:not([src])' ) )
140- return scripts . filter ( script =>
141- script . textContent && script . textContent . includes ( 'window.INLINE_SCRIPT_1 = true' )
158+ return scripts . filter (
159+ ( script ) =>
160+ script . textContent &&
161+ script . textContent . includes ( 'window.INLINE_SCRIPT_1 = true' ) ,
142162 ) . length
143163 } )
144164 expect ( secondNavScript1Count ) . toBe ( 1 )
145-
165+
146166 // Verify the scripts are still working
147167 expect ( await page . evaluate ( 'window.INLINE_SCRIPT_1' ) ) . toBe ( true )
148168 expect ( await page . evaluate ( 'window.INLINE_SCRIPT_2' ) ) . toBe ( 'test' )
149169 } )
150- } )
170+ } )
0 commit comments