@@ -2,97 +2,74 @@ import { expect, test } from '@playwright/test'
2
2
3
3
test . describe ( 'Script Duplication Prevention' , ( ) => {
4
4
test ( 'should not create duplicate scripts on SSR route' , async ( { page } ) => {
5
- // Navigate directly to scripts route (SSR scenario)
6
5
await page . goto ( '/scripts' )
7
6
8
7
await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
9
8
10
- // Count script tags with src="script.js"
11
9
const scriptCount = await page . evaluate ( ( ) => {
12
10
return document . querySelectorAll ( 'script[src="script.js"]' ) . length
13
11
} )
14
12
15
- // Should have exactly one script tag
16
13
expect ( scriptCount ) . toBe ( 1 )
17
-
18
- // Verify the script executed correctly
19
14
expect ( await page . evaluate ( 'window.SCRIPT_1' ) ) . toBe ( true )
20
15
} )
21
16
22
17
test ( 'should not create duplicate scripts during client-side navigation' , async ( {
23
18
page,
24
19
} ) => {
25
- // Start from home page
26
20
await page . goto ( '/' )
27
21
28
- // Navigate to scripts route (client-side navigation)
29
22
await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
30
23
await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
31
24
32
- // Count script tags after first navigation
33
25
const firstNavCount = await page . evaluate ( ( ) => {
34
26
return document . querySelectorAll ( 'script[src="script.js"]' ) . length
35
27
} )
36
28
expect ( firstNavCount ) . toBe ( 1 )
37
29
38
- // Navigate away from scripts route
39
30
await page . getByRole ( 'link' , { name : 'Home' } ) . click ( )
40
31
await expect ( page . getByRole ( 'link' , { name : 'Posts' } ) ) . toBeVisible ( )
41
32
42
- // Navigate back to scripts route
43
33
await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
44
34
await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
45
35
46
- // Count script tags after second navigation - should still be 1
47
36
const secondNavCount = await page . evaluate ( ( ) => {
48
37
return document . querySelectorAll ( 'script[src="script.js"]' ) . length
49
38
} )
50
39
expect ( secondNavCount ) . toBe ( 1 )
51
-
52
- // Verify the script is still working
53
40
expect ( await page . evaluate ( 'window.SCRIPT_1' ) ) . toBe ( true )
54
41
} )
55
42
56
43
test ( 'should not create duplicate scripts with multiple navigation cycles' , async ( {
57
44
page,
58
45
} ) => {
59
- // Start from home page
60
46
await page . goto ( '/' )
61
47
62
- // Navigate to scripts route multiple times
63
48
for ( let i = 0 ; i < 3 ; i ++ ) {
64
- // Go to scripts
65
49
await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
66
50
await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
67
51
68
- // Go back to home
69
52
await page . getByRole ( 'link' , { name : 'Home' } ) . click ( )
70
53
await expect ( page . getByRole ( 'link' , { name : 'Posts' } ) ) . toBeVisible ( )
71
54
}
72
55
73
- // Final navigation to scripts
74
56
await page . getByRole ( 'link' , { name : 'Scripts' , exact : true } ) . click ( )
75
57
await expect ( page . getByTestId ( 'scripts-test-heading' ) ) . toBeInViewport ( )
76
58
77
- // Count script tags - should still be exactly 1
78
59
const finalCount = await page . evaluate ( ( ) => {
79
60
return document . querySelectorAll ( 'script[src="script.js"]' ) . length
80
61
} )
81
62
expect ( finalCount ) . toBe ( 1 )
82
-
83
- // Verify the script is still working
84
63
expect ( await page . evaluate ( 'window.SCRIPT_1' ) ) . toBe ( true )
85
64
} )
86
65
87
66
test ( 'should not create duplicate inline scripts' , async ( { page } ) => {
88
- // Navigate directly to inline scripts route (SSR scenario)
89
67
await page . goto ( '/inline-scripts' )
90
68
91
69
await expect (
92
70
page . getByTestId ( 'inline-scripts-test-heading' ) ,
93
71
) . toBeInViewport ( )
94
72
95
- // Count specific inline scripts
96
73
const script1Count = await page . evaluate ( ( ) => {
97
74
const scripts = Array . from ( document . querySelectorAll ( 'script:not([src])' ) )
98
75
return scripts . filter (
@@ -111,28 +88,22 @@ test.describe('Script Duplication Prevention', () => {
111
88
) . length
112
89
} )
113
90
114
- // Should have exactly one of each inline script
115
91
expect ( script1Count ) . toBe ( 1 )
116
92
expect ( script2Count ) . toBe ( 1 )
117
-
118
- // Verify the scripts executed correctly
119
93
expect ( await page . evaluate ( 'window.INLINE_SCRIPT_1' ) ) . toBe ( true )
120
94
expect ( await page . evaluate ( 'window.INLINE_SCRIPT_2' ) ) . toBe ( 'test' )
121
95
} )
122
96
123
97
test ( 'should not create duplicate inline scripts during client-side navigation' , async ( {
124
98
page,
125
99
} ) => {
126
- // Start from home page
127
100
await page . goto ( '/' )
128
101
129
- // Navigate to inline scripts route (client-side navigation)
130
102
await page . getByRole ( 'link' , { name : 'Inline Scripts' } ) . click ( )
131
103
await expect (
132
104
page . getByTestId ( 'inline-scripts-test-heading' ) ,
133
105
) . toBeInViewport ( )
134
106
135
- // Count inline scripts after first navigation
136
107
const firstNavScript1Count = await page . evaluate ( ( ) => {
137
108
const scripts = Array . from ( document . querySelectorAll ( 'script:not([src])' ) )
138
109
return scripts . filter (
@@ -143,7 +114,6 @@ test.describe('Script Duplication Prevention', () => {
143
114
} )
144
115
expect ( firstNavScript1Count ) . toBe ( 1 )
145
116
146
- // Navigate away and back
147
117
await page . getByRole ( 'link' , { name : 'Home' } ) . click ( )
148
118
await expect ( page . getByRole ( 'link' , { name : 'Posts' } ) ) . toBeVisible ( )
149
119
@@ -152,7 +122,6 @@ test.describe('Script Duplication Prevention', () => {
152
122
page . getByTestId ( 'inline-scripts-test-heading' ) ,
153
123
) . toBeInViewport ( )
154
124
155
- // Count inline scripts after second navigation - should still be 1
156
125
const secondNavScript1Count = await page . evaluate ( ( ) => {
157
126
const scripts = Array . from ( document . querySelectorAll ( 'script:not([src])' ) )
158
127
return scripts . filter (
@@ -163,7 +132,6 @@ test.describe('Script Duplication Prevention', () => {
163
132
} )
164
133
expect ( secondNavScript1Count ) . toBe ( 1 )
165
134
166
- // Verify the scripts are still working
167
135
expect ( await page . evaluate ( 'window.INLINE_SCRIPT_1' ) ) . toBe ( true )
168
136
expect ( await page . evaluate ( 'window.INLINE_SCRIPT_2' ) ) . toBe ( 'test' )
169
137
} )
0 commit comments