@@ -54,80 +54,83 @@ describe('@next/third-parties basic usage', () => {
54
54
expect ( dataLayer2 . length ) . toBe ( 2 )
55
55
} )
56
56
57
- it ( 'renders GTM with consent management for multiple CMP platforms ' , async ( ) => {
57
+ it ( 'renders GTM with consent management support ' , async ( ) => {
58
58
const browser = await next . browser ( '/gtm-consent' )
59
59
await waitFor ( 1000 )
60
60
61
- // Test standard GTM (should have type="application/javascript" by default)
62
- const standardScripts = await browser . elementsByCss (
63
- 'script[src*="GTM-STANDARD"]'
64
- )
65
- expect ( standardScripts . length ) . toBe ( 1 )
66
-
67
- const standardType : string | null = await browser . eval (
68
- 'document.querySelector("#_next-gtm-init").getAttribute("type")'
69
- )
70
- expect ( standardType ) . toBe ( 'application/javascript' )
71
-
72
- // Test Usercentrics GTM (should have type="text/plain" and data-usercentrics)
73
- const usercentricsInitScripts = await browser . elementsByCss (
74
- 'script[type="text/plain"][data-usercentrics="Google Tag Manager"]'
75
- )
76
- expect ( usercentricsInitScripts . length ) . toBe ( 2 ) // init + external script
77
-
78
- const usercentricsScripts = await browser . elementsByCss (
79
- 'script[src*="GTM-USERCENTRICS"][type="text/plain"]'
80
- )
81
- expect ( usercentricsScripts . length ) . toBe ( 1 )
82
-
83
- // Test OneTrust GTM (should have type="text/plain" and data-one-trust-category)
84
- const onetrustInitScripts = await browser . elementsByCss (
85
- 'script[type="text/plain"][data-one-trust-category="C0002"]'
86
- )
87
- expect ( onetrustInitScripts . length ) . toBe ( 2 ) // init + external script
88
-
89
- const onetrustScripts = await browser . elementsByCss (
90
- 'script[src*="GTM-ONETRUST"][type="text/plain"]'
91
- )
92
- expect ( onetrustScripts . length ) . toBe ( 1 )
93
-
94
- // Test Cookiebot GTM (should have type="text/plain" and data-cookieconsent)
95
- const cookiebotInitScripts = await browser . elementsByCss (
96
- 'script[type="text/plain"][data-cookieconsent="statistics"]'
97
- )
98
- expect ( cookiebotInitScripts . length ) . toBe ( 2 ) // init + external script
99
-
100
- const cookiebotScripts = await browser . elementsByCss (
101
- 'script[src*="GTM-COOKIEBOT"][type="text/plain"]'
102
- )
103
- expect ( cookiebotScripts . length ) . toBe ( 1 )
104
-
105
- // Test Didomi GTM (should have type="text/plain" and data-didomi-purposes)
106
- const didomiInitScripts = await browser . elementsByCss (
107
- 'script[type="text/plain"][data-didomi-purposes="analytics"]'
108
- )
109
- expect ( didomiInitScripts . length ) . toBe ( 2 ) // init + external script
110
-
111
- const didomiScripts = await browser . elementsByCss (
112
- 'script[src*="GTM-DIDOMI"][type="text/plain"]'
113
- )
114
- expect ( didomiScripts . length ) . toBe ( 1 )
115
-
116
- // Test custom consent GTM (multiple data attributes)
117
- const customInitScripts = await browser . elementsByCss (
118
- 'script[type="text/plain"][data-consent-category="analytics"]'
119
- )
120
- expect ( customInitScripts . length ) . toBe ( 2 ) // init + external script
121
-
122
- const customScripts = await browser . elementsByCss (
123
- 'script[src*="GTM-CUSTOM"][type="text/plain"][data-consent-required="true"]'
124
- )
125
- expect ( customScripts . length ) . toBe ( 1 )
126
-
127
- // Test that consent-managed scripts don't execute until consent is given
61
+ // Test data for different CMP platforms
62
+ const cmpScenarios = [
63
+ {
64
+ name : 'standard' ,
65
+ gtmId : 'GTM-STANDARD' ,
66
+ expectedType : 'application/javascript' ,
67
+ dataAttribute : null ,
68
+ shouldExecute : true ,
69
+ } ,
70
+ {
71
+ name : 'usercentrics' ,
72
+ gtmId : 'GTM-USERCENTRICS' ,
73
+ expectedType : 'text/plain' ,
74
+ dataAttribute : 'data-usercentrics="Google Tag Manager"' ,
75
+ shouldExecute : false ,
76
+ } ,
77
+ {
78
+ name : 'onetrust' ,
79
+ gtmId : 'GTM-ONETRUST' ,
80
+ expectedType : 'text/plain' ,
81
+ dataAttribute : 'data-one-trust-category="C0002"' ,
82
+ shouldExecute : false ,
83
+ } ,
84
+ {
85
+ name : 'cookiebot' ,
86
+ gtmId : 'GTM-COOKIEBOT' ,
87
+ expectedType : 'text/plain' ,
88
+ dataAttribute : 'data-cookieconsent="statistics"' ,
89
+ shouldExecute : false ,
90
+ } ,
91
+ {
92
+ name : 'didomi' ,
93
+ gtmId : 'GTM-DIDOMI' ,
94
+ expectedType : 'text/plain' ,
95
+ dataAttribute : 'data-didomi-purposes="analytics"' ,
96
+ shouldExecute : false ,
97
+ } ,
98
+ {
99
+ name : 'custom' ,
100
+ gtmId : 'GTM-CUSTOM' ,
101
+ expectedType : 'text/plain' ,
102
+ dataAttribute : 'data-consent-category="analytics"' ,
103
+ shouldExecute : false ,
104
+ } ,
105
+ ]
106
+
107
+ // Test each CMP scenario
108
+ for ( const scenario of cmpScenarios ) {
109
+ // Verify external GTM script has correct type and attributes
110
+ const externalScripts = await browser . elementsByCss (
111
+ `script[src*="${ scenario . gtmId } "][type="${ scenario . expectedType } "]`
112
+ )
113
+ expect ( externalScripts . length ) . toBe ( 1 )
114
+
115
+ // For consent-managed scripts, verify data attributes are present
116
+ if ( scenario . dataAttribute ) {
117
+ const scriptsWithDataAttr = await browser . elementsByCss (
118
+ `script[type="${ scenario . expectedType } "][${ scenario . dataAttribute } ]`
119
+ )
120
+ expect ( scriptsWithDataAttr . length ) . toBe ( 2 ) // init + external script
121
+ }
122
+ }
123
+
124
+ // Verify script execution behavior
128
125
const dataLayer : unknown [ ] = await browser . eval ( 'window.dataLayer' )
129
- // Only the standard GTM should have initialized dataLayer
126
+ // Only the standard GTM (without consent management) should initialize dataLayer
130
127
expect ( dataLayer . length ) . toBe ( 1 )
128
+
129
+ // Verify standard GTM init script has correct default type
130
+ const standardInitType : string | null = await browser . eval (
131
+ 'document.querySelector("script[id*=\\"_next-gtm-init\\"]").getAttribute("type")'
132
+ )
133
+ expect ( standardInitType ) . toBe ( 'application/javascript' )
131
134
} )
132
135
133
136
it ( 'renders GA' , async ( ) => {
0 commit comments