@hashicorp/react-components 1.1.8 → 1.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/package.json +4 -8
  2. package/src/components/nav-panel/components/flight-icon/index.tsx +51 -0
  3. package/src/components/nav-panel/components/flight-icon/style.module.css +3 -0
  4. package/src/components/nav-panel/components/link-list/index.tsx +15 -9
  5. package/src/components/nav-panel/components/link-list/style.module.css +10 -10
  6. package/src/components/nav-panel/components/panel/column/style.module.css +3 -3
  7. package/src/components/nav-panel/components/panel/root/style.module.css +6 -6
  8. package/src/components/nav-panel/components/promo/index.tsx +15 -9
  9. package/src/components/nav-panel/components/promo/style.module.css +27 -13
  10. package/src/components/nav-panel/components/text/index.tsx +34 -0
  11. package/src/components/nav-panel/product-panel/index.tsx +39 -14
  12. package/src/components/nav-panel/product-panel/style.module.css +78 -37
  13. package/src/components/nav-panel/standard-panel/index.tsx +3 -0
  14. package/src/components/nav-panel/standard-panel/style.module.css +2 -2
  15. package/src/lib/get-localized-link-props/index.test.ts +30 -0
  16. package/src/lib/get-localized-link-props/index.ts +45 -0
  17. package/src/lib/parse-url/index.test.ts +40 -1
  18. package/src/lib/parse-url/index.ts +75 -9
  19. package/src/lib/send-posthog-event/index.ts +22 -0
  20. package/src/lib/supported-locales/index.ts +1 -0
  21. package/src/lib/track-nav-click-event/index.ts +4 -4
  22. package/src/lib/v/index.ts +4 -5
  23. package/src/components/actions/index.tsx +0 -107
  24. package/src/components/actions/style.module.css +0 -28
  25. package/src/components/actions/types.ts +0 -73
  26. package/src/components/call-to-action/index.tsx +0 -205
  27. package/src/components/call-to-action/styles/variant-centered.module.css +0 -93
  28. package/src/components/call-to-action/styles/variant-compact-grid.module.css +0 -88
  29. package/src/components/call-to-action/styles/variant-compact.module.css +0 -91
  30. package/src/components/call-to-action/styles/variant-links.module.css +0 -85
  31. package/src/components/card/README.md +0 -11
  32. package/src/components/card/card.tsx +0 -45
  33. package/src/components/card/index.test.tsx +0 -69
  34. package/src/components/card/index.tsx +0 -14
  35. package/src/components/card/newsroom-card/index.test.tsx +0 -39
  36. package/src/components/card/newsroom-card/index.tsx +0 -37
  37. package/src/components/card/newsroom-card/style.module.css +0 -12
  38. package/src/components/card/partner-card/index.test.tsx +0 -57
  39. package/src/components/card/partner-card/index.tsx +0 -69
  40. package/src/components/card/partner-card/style.module.css +0 -24
  41. package/src/components/card/person-card/index.test.tsx +0 -71
  42. package/src/components/card/person-card/index.tsx +0 -93
  43. package/src/components/card/person-card/style.module.css +0 -33
  44. package/src/components/card/primitives.tsx +0 -176
  45. package/src/components/card/promo-card/index.tsx +0 -51
  46. package/src/components/card/resource-card/index.test.tsx +0 -48
  47. package/src/components/card/resource-card/index.tsx +0 -37
  48. package/src/components/card/style.module.css +0 -145
  49. package/src/components/card/types.ts +0 -66
  50. package/src/components/card/unified-card/index.tsx +0 -64
  51. package/src/components/consent-manager/README.md +0 -73
  52. package/src/components/consent-manager/components/banner/banner.module.css +0 -23
  53. package/src/components/consent-manager/components/banner/banner.test.tsx +0 -31
  54. package/src/components/consent-manager/components/banner/banner.tsx +0 -73
  55. package/src/components/consent-manager/components/dialog/dialog.module.css +0 -145
  56. package/src/components/consent-manager/components/dialog/dialog.test.tsx +0 -194
  57. package/src/components/consent-manager/components/dialog/dialog.tsx +0 -339
  58. package/src/components/consent-manager/img/icn_close.tsx +0 -19
  59. package/src/components/consent-manager/index.test.tsx +0 -334
  60. package/src/components/consent-manager/index.tsx +0 -186
  61. package/src/components/consent-manager/loader/README.md +0 -29
  62. package/src/components/consent-manager/loader/categories.ts +0 -22
  63. package/src/components/consent-manager/loader/index.tsx +0 -94
  64. package/src/components/consent-manager/loader/presets/enterprise.ts +0 -12
  65. package/src/components/consent-manager/loader/presets/oss.ts +0 -10
  66. package/src/components/consent-manager/loader/services.ts +0 -45
  67. package/src/components/consent-manager/scripts/custom.tsx +0 -59
  68. package/src/components/consent-manager/scripts/segment.tsx +0 -87
  69. package/src/components/consent-manager/style.module.css +0 -9
  70. package/src/components/consent-manager/types.ts +0 -55
  71. package/src/components/consent-manager/util/cookies.test.ts +0 -75
  72. package/src/components/consent-manager/util/cookies.ts +0 -70
  73. package/src/components/consent-manager/util/integrations.test.ts +0 -115
  74. package/src/components/consent-manager/util/integrations.ts +0 -110
  75. package/src/components/intro/index.tsx +0 -82
  76. package/src/components/intro/style.module.css +0 -67
  77. package/src/components/intro/types.ts +0 -44
  78. package/src/components/marketo-form/CHANGELOG.md +0 -122
  79. package/src/components/marketo-form/README.md +0 -1
  80. package/src/components/marketo-form/contexts/FormMetadata.ts +0 -11
  81. package/src/components/marketo-form/docs.mdx +0 -197
  82. package/src/components/marketo-form/fixtures/index.ts +0 -185
  83. package/src/components/marketo-form/form/index.tsx +0 -369
  84. package/src/components/marketo-form/index.test.tsx +0 -320
  85. package/src/components/marketo-form/index.ts +0 -11
  86. package/src/components/marketo-form/package.original.json +0 -30
  87. package/src/components/marketo-form/partials/error-message/index.tsx +0 -12
  88. package/src/components/marketo-form/partials/error-message/style.module.css +0 -13
  89. package/src/components/marketo-form/partials/field/index.tsx +0 -90
  90. package/src/components/marketo-form/partials/field-wrapper/index.tsx +0 -26
  91. package/src/components/marketo-form/partials/fields/checkbox-field/index.tsx +0 -32
  92. package/src/components/marketo-form/partials/fields/email-field/index.tsx +0 -35
  93. package/src/components/marketo-form/partials/fields/form-page-url-field/index.tsx +0 -21
  94. package/src/components/marketo-form/partials/fields/hidden-field/index.tsx +0 -37
  95. package/src/components/marketo-form/partials/fields/htmltext-field/index.tsx +0 -21
  96. package/src/components/marketo-form/partials/fields/htmltext-field/style.module.css +0 -18
  97. package/src/components/marketo-form/partials/fields/name-field/index.tsx +0 -19
  98. package/src/components/marketo-form/partials/fields/name-field/style.module.css +0 -13
  99. package/src/components/marketo-form/partials/fields/radio-buttons-field/index.tsx +0 -48
  100. package/src/components/marketo-form/partials/fields/select-field/index.tsx +0 -39
  101. package/src/components/marketo-form/partials/fields/select-field/style.module.css +0 -5
  102. package/src/components/marketo-form/partials/fields/telephone-field/index.tsx +0 -30
  103. package/src/components/marketo-form/partials/fields/text-field/index.tsx +0 -30
  104. package/src/components/marketo-form/partials/fields/textarea-field/index.tsx +0 -29
  105. package/src/components/marketo-form/partials/label/index.tsx +0 -24
  106. package/src/components/marketo-form/partials/label/style.module.css +0 -12
  107. package/src/components/marketo-form/partials/visibility-rule/index.tsx +0 -84
  108. package/src/components/marketo-form/props.js +0 -66
  109. package/src/components/marketo-form/server/api-routes.ts +0 -109
  110. package/src/components/marketo-form/server/client.ts +0 -98
  111. package/src/components/marketo-form/server/index.ts +0 -8
  112. package/src/components/marketo-form/types.ts +0 -268
  113. package/src/components/marketo-form/utils.test.tsx +0 -103
  114. package/src/components/marketo-form/utils.ts +0 -449
  115. package/src/components/next-steps/index.tsx +0 -137
  116. package/src/components/next-steps/style.module.css +0 -270
  117. package/src/components/next-steps/types.ts +0 -63
  118. package/src/components/product-badge/index.test.tsx +0 -11
  119. package/src/components/product-badge/index.tsx +0 -42
  120. package/src/components/product-badge/style.module.css +0 -76
  121. package/src/components/products-used/index.tsx +0 -84
  122. package/src/components/products-used/style.module.css +0 -87
  123. package/src/components/products-used/types.ts +0 -31
  124. package/src/components/related-content/index.test.tsx +0 -74
  125. package/src/components/related-content/index.tsx +0 -56
  126. package/src/components/related-content/style.module.css +0 -89
  127. package/src/components/related-content/types.ts +0 -21
  128. package/src/components/subnav/helpers/areBasePathsMatching.ts +0 -13
  129. package/src/components/subnav/helpers/traverse/index.ts +0 -42
  130. package/src/components/subnav/helpers/useHeightRef.ts +0 -42
  131. package/src/components/subnav/helpers/useNavRef.ts +0 -30
  132. package/src/components/subnav/helpers/useOverflowRef.ts +0 -85
  133. package/src/components/subnav/helpers/useStuckRef.ts +0 -51
  134. package/src/components/subnav/index.tsx +0 -199
  135. package/src/components/subnav/partials/CtaLinks/github-stars-link/formatStarCount/index.test.ts +0 -26
  136. package/src/components/subnav/partials/CtaLinks/github-stars-link/formatStarCount/index.ts +0 -22
  137. package/src/components/subnav/partials/CtaLinks/github-stars-link/index.tsx +0 -87
  138. package/src/components/subnav/partials/CtaLinks/github-stars-link/parseGithubUrl/index.test.ts +0 -29
  139. package/src/components/subnav/partials/CtaLinks/github-stars-link/parseGithubUrl/index.ts +0 -32
  140. package/src/components/subnav/partials/CtaLinks/github-stars-link/style.module.css +0 -85
  141. package/src/components/subnav/partials/CtaLinks/icons/github-logo.js +0 -20
  142. package/src/components/subnav/partials/CtaLinks/icons/github.svg.js +0 -7
  143. package/src/components/subnav/partials/CtaLinks/icons/star.js +0 -20
  144. package/src/components/subnav/partials/CtaLinks/index.tsx +0 -130
  145. package/src/components/subnav/partials/CtaLinks/style.module.css +0 -45
  146. package/src/components/subnav/partials/DropdownTrigger/index.tsx +0 -35
  147. package/src/components/subnav/partials/DropdownTrigger/style.module.css +0 -36
  148. package/src/components/subnav/partials/MenuItemsDefault/index.tsx +0 -151
  149. package/src/components/subnav/partials/MenuItemsDefault/style.module.css +0 -91
  150. package/src/components/subnav/partials/MenuItemsOverflow/index.tsx +0 -139
  151. package/src/components/subnav/partials/MenuItemsOverflow/style.module.css +0 -82
  152. package/src/components/subnav/partials/TitleLink/index.tsx +0 -113
  153. package/src/components/subnav/partials/TitleLink/mktg-logos/all-community-product-logos_on-dark.ts +0 -9
  154. package/src/components/subnav/partials/TitleLink/mktg-logos/all-community-product-logos_on-light.ts +0 -9
  155. package/src/components/subnav/partials/TitleLink/mktg-logos/all-primary-logos_on-dark.ts +0 -23
  156. package/src/components/subnav/partials/TitleLink/mktg-logos/all-primary-logos_on-light.ts +0 -23
  157. package/src/components/subnav/partials/TitleLink/style.module.css +0 -43
  158. package/src/components/subnav/partials/nav-item-text/index.tsx +0 -37
  159. package/src/components/subnav/partials/nav-item-text/style.module.css +0 -15
  160. package/src/components/subnav/style.module.css +0 -68
  161. package/src/components/subnav/subnav-consts.ts +0 -1
  162. package/src/components/subnav/theme.module.css +0 -50
@@ -1,339 +0,0 @@
1
- /*
2
- dialog.js
3
- Managing preferences dialog
4
- */
5
-
6
- import { useState, useEffect, useRef } from 'react'
7
- import Link from 'next/link'
8
- import getIntegrations, {
9
- type ConsentManagerServiceWithOrigin,
10
- } from '../../util/integrations'
11
- import Button from '@hashicorp/react-design-system-components/src/components/button'
12
- import ButtonSet from '@hashicorp/react-design-system-components/src/components/button-set'
13
- import { ToggleBase } from '@hashicorp/react-design-system-components/src/components/form/toggle'
14
- import Text from '@hashicorp/react-design-system-components/src/components/text'
15
- import Card from '@hashicorp/react-design-system-components/src/components/card'
16
- import FlightIcon from '@hashicorp/react-design-system-components/src/components/flight-icon'
17
- import classNames from 'classnames'
18
- import s from './dialog.module.css'
19
- import type { ConsentManagerService, ConsentManagerCategory } from '../../types'
20
-
21
- interface ConsentPreferencesProps {
22
- preferences: { loadAll?: boolean }
23
- categories: ConsentManagerCategory[]
24
- segmentServices?: ConsentManagerService[]
25
- additionalServices?: ConsentManagerService[]
26
- segmentWriteKey?: string
27
- privacyPolicyLink?: string
28
- cookiePolicyLink?: string
29
- saveAndLoadAnalytics: (consent: unknown) => void
30
- version?: number
31
- showDialog: boolean
32
- }
33
-
34
- interface ConsentPreferencesState {
35
- version: number
36
- groupedIntegrations: Record<string, ConsentManagerServiceWithOrigin[]>
37
- consent: { loadAll?: boolean }
38
- categories: Record<string, string>
39
- showCategories: Record<string, boolean>
40
- }
41
-
42
- const ConsentPreferences: React.FC<ConsentPreferencesProps> = (props) => {
43
- const [state, setState] = useState<ConsentPreferencesState>({
44
- version: props.version || 0,
45
- groupedIntegrations: {},
46
- consent: props.preferences,
47
- categories: props.categories.reduce((obj, category) => {
48
- obj[category.name] = category.description
49
- return obj
50
- }, {}),
51
- showCategories: {},
52
- })
53
- const dialogRef = useRef<HTMLDialogElement>(null)
54
-
55
- useEffect(() => {
56
- // Set up integrations
57
- getIntegrations(
58
- props.segmentServices,
59
- props.additionalServices,
60
- props.segmentWriteKey
61
- ).then((groupedIntegrations) => {
62
- setState((prevState) => ({
63
- ...prevState,
64
- groupedIntegrations,
65
- }))
66
- })
67
- }, [props.segmentServices, props.additionalServices, props.segmentWriteKey])
68
-
69
- useEffect(() => {
70
- const dialog = dialogRef.current
71
- if (!dialog) return
72
-
73
- const currentState = dialog.open ? 'open' : 'closed'
74
- const correctState = props.showDialog ? 'open' : 'closed'
75
-
76
- const isCorrect = currentState === correctState
77
- const shouldClose = currentState === 'open' && correctState === 'closed'
78
- const shouldOpen = currentState === 'closed' && correctState === 'open'
79
-
80
- if (isCorrect) return
81
-
82
- if (shouldClose) {
83
- dialog.close()
84
- } else if (shouldOpen) {
85
- dialog.showModal()
86
- }
87
- }, [props.showDialog])
88
-
89
- const handleToggle = (
90
- integrationName: string,
91
- integrationOrigin: string,
92
- event: React.FormEvent<HTMLInputElement>
93
- ) => {
94
- const currentConsent = state.consent || {}
95
-
96
- // If user has { loadAll: true } and they toggle, make sure to preserve
97
- // the loadAll settings by setting everything to true first. This setting
98
- // gets reset at the end of the function.
99
- if (currentConsent.loadAll) {
100
- Object.keys(state.groupedIntegrations).forEach((category) => {
101
- state.groupedIntegrations[category].forEach((tool) => {
102
- currentConsent[tool.origin] = currentConsent[tool.origin] || {}
103
- currentConsent[tool.origin][tool.name] = true
104
- })
105
- })
106
- }
107
-
108
- if (integrationOrigin === 'categories') {
109
- state.groupedIntegrations[integrationName].forEach((tool) => {
110
- currentConsent[tool.origin] = currentConsent[tool.origin] || {}
111
- currentConsent[tool.origin][tool.name] = event.currentTarget.checked
112
- })
113
- } else {
114
- currentConsent[integrationOrigin] =
115
- currentConsent[integrationOrigin] || {}
116
- currentConsent[integrationOrigin][integrationName] =
117
- event.currentTarget.checked
118
- }
119
-
120
- delete currentConsent.loadAll
121
- setState((prevState) => ({
122
- ...prevState,
123
- consent: currentConsent,
124
- }))
125
- }
126
-
127
- const handleFold = (categoryName) => {
128
- const categories = { ...state.showCategories }
129
- categories[categoryName] = !categories[categoryName]
130
- setState((prevState) => ({
131
- ...prevState,
132
- showCategories: categories,
133
- }))
134
- }
135
-
136
- const getCategoryToggle = (category) => {
137
- return state.groupedIntegrations[category].some((tool) => {
138
- return (
139
- state.consent &&
140
- Object.prototype.hasOwnProperty.call(state.consent, tool.origin) &&
141
- Object.prototype.hasOwnProperty.call(
142
- state.consent[tool.origin],
143
- tool.name
144
- ) &&
145
- state.consent[tool.origin][tool.name]
146
- )
147
- })
148
- }
149
-
150
- // Build each individual category and integration row
151
- const buildCategory = (
152
- consentManagerServices: ConsentManagerServiceWithOrigin[],
153
- name: string
154
- ) => {
155
- const categoryItems = consentManagerServices.map((service) => {
156
- const categoryItemTitleID = `consent-manager-categoryItemTitle-${service.name}`
157
-
158
- return (
159
- <Card
160
- level="mid"
161
- hasBorder
162
- key={service.name}
163
- className={s.categoryItem}
164
- >
165
- <div className={s.categoryItemHeader}>
166
- <Text.Display
167
- id={categoryItemTitleID}
168
- tag="h4"
169
- color="strong"
170
- className={s.categoryItemTitle}
171
- >
172
- {service.name}
173
- </Text.Display>
174
- <ToggleBase
175
- data-testid={`category-toggle-${service.name}`}
176
- toggleOnEnter
177
- field={{
178
- 'aria-labelledby': categoryItemTitleID,
179
- onChange: (e) => handleToggle(service.name, service.origin, e),
180
- checked: Boolean(
181
- state.consent.loadAll ||
182
- (state.consent &&
183
- state.consent[service.origin] &&
184
- state.consent[service.origin][service.name])
185
- ),
186
- }}
187
- />
188
- </div>
189
- <Text.Body
190
- tag="p"
191
- color="primary"
192
- className={s.categoryItemDescription}
193
- >
194
- {service.description}
195
- </Text.Body>
196
- </Card>
197
- )
198
- })
199
-
200
- const categoryHeaderTitleID = `consent-manager-categoryHeaderTitle-${name}`
201
- const categoryListID = `consent-manager-categoryHeaderTitle-${name}-list`
202
-
203
- return (
204
- <div className={s.category} key={name}>
205
- <div className={s.categoryHeader}>
206
- <Text.Display
207
- id={categoryHeaderTitleID}
208
- tag="h3"
209
- size="200"
210
- color="strong"
211
- >
212
- {name}
213
- </Text.Display>
214
- {!state.showCategories[name] && (
215
- <ToggleBase
216
- toggleOnEnter
217
- field={{
218
- 'aria-labelledby': categoryHeaderTitleID,
219
- onChange: (e) => handleToggle(name, 'categories', e),
220
- checked: state.consent.loadAll || getCategoryToggle(name),
221
- }}
222
- />
223
- )}
224
- <button
225
- className={classNames(s.categoryFoldTrigger, {
226
- [s.shown]: state.showCategories[name],
227
- })}
228
- onClick={() => {
229
- handleFold(name)
230
- }}
231
- aria-label={
232
- state.showCategories[name]
233
- ? `See less in ${name}`
234
- : `See more in ${name}`
235
- }
236
- aria-expanded={state.showCategories[name] ? `true` : `false`}
237
- aria-controls={
238
- state.showCategories[name] ? categoryListID : undefined
239
- }
240
- >
241
- <FlightIcon name="chevron-down" size={24} />
242
- </button>
243
- </div>
244
- {state.showCategories[name] && (
245
- <div className={s.categoryFold} id={categoryListID}>
246
- <Text.Body tag="p" color="primary">
247
- {state.categories[name]}
248
- </Text.Body>
249
- {categoryItems}
250
- </div>
251
- )}
252
- </div>
253
- )
254
- }
255
- const categories = Object.keys(state.groupedIntegrations).map((category) => {
256
- return buildCategory(state.groupedIntegrations[category], category)
257
- })
258
-
259
- return (
260
- <dialog
261
- aria-labelledby="consent-mgr-dialog-title"
262
- className={s.root}
263
- data-testid="consent-mgr-dialog"
264
- ref={dialogRef}
265
- >
266
- <div className={s.visibleDialog}>
267
- <div className={s.dialogHeader}>
268
- <Text.Display
269
- id="consent-mgr-dialog-title"
270
- tag="h2"
271
- size="300"
272
- color="strong"
273
- >
274
- Manage cookies
275
- </Text.Display>
276
- </div>
277
- <div className={s.dialogBody}>
278
- <Text.Body tag="p" size="300" color="primary">
279
- HashiCorp uses data collected by cookies and JavaScript libraries to
280
- improve your browsing experience, analyze site traffic, and increase
281
- the overall performance of our site. By using our website, you’re
282
- agreeing to our{' '}
283
- <Link
284
- href={
285
- props.privacyPolicyLink ??
286
- 'https://www.hashicorp.com/trust/privacy'
287
- }
288
- target="_blank"
289
- rel="noopener noreferrer"
290
- data-testid="privacy-policy-link"
291
- >
292
- Privacy Policy
293
- </Link>{' '}
294
- and{' '}
295
- <Link
296
- href={
297
- props.cookiePolicyLink ??
298
- 'https://www.hashicorp.com/trust/privacy/cookies'
299
- }
300
- target="_blank"
301
- rel="noopener noreferrer"
302
- data-testid="cookie-policy-link"
303
- >
304
- Cookie Policy
305
- </Link>
306
- .
307
- </Text.Body>
308
- <Text.Body tag="p" size="300" color="faint">
309
- The categories below outline which companies and tools we use for
310
- collecting data. To opt out of a category of data collection, set
311
- the toggle to “Off” and save your preferences.
312
- </Text.Body>
313
- <div id="outline">{categories}</div>
314
- </div>
315
- <div className={s.dialogFooter}>
316
- <ButtonSet>
317
- <Button
318
- text="Save preferences"
319
- color="secondary"
320
- onClick={() => {
321
- props.saveAndLoadAnalytics(state.consent)
322
- }}
323
- />
324
- <Button
325
- autoFocus
326
- text="Accept all"
327
- color="primary"
328
- onClick={() => {
329
- props.saveAndLoadAnalytics({ loadAll: true })
330
- }}
331
- />
332
- </ButtonSet>
333
- </div>
334
- </div>
335
- </dialog>
336
- )
337
- }
338
-
339
- export default ConsentPreferences
@@ -1,19 +0,0 @@
1
- export default function CloseIcon() {
2
- return (
3
- <svg
4
- width="16"
5
- height="16"
6
- viewBox="0 0 16 16"
7
- xmlns="http://www.w3.org/2000/svg"
8
- >
9
- <title>Close Button</title>
10
- <path
11
- d="M1.778 1.778l12.541 12.541m-.097-12.541L1.681 14.319"
12
- stroke="#FFF"
13
- strokeWidth="2"
14
- fill="none"
15
- strokeLinecap="square"
16
- />
17
- </svg>
18
- )
19
- }
@@ -1,334 +0,0 @@
1
- import { fireEvent, render, screen, act } from '@testing-library/react'
2
-
3
- const defaultProps = {
4
- version: 0,
5
- showDialog: false,
6
- segmentWriteKey: 'iyi06c432UL7SB1r3fQReec4bNwFyzkW',
7
- privacyPolicyLink: 'https://www.hashicorp.com/privacy',
8
- companyName: 'HashiCorp',
9
- segmentServices: [
10
- {
11
- name: 'Example Name',
12
- category: 'Example Category',
13
- description:
14
- 'A short description of what the service is and how your company uses the data.',
15
- },
16
- ],
17
- additionalServices: [
18
- {
19
- name: 'Name of the service',
20
- category: 'Example Category',
21
- description:
22
- 'A short description of what the service is and how your company uses the data.',
23
- body: '',
24
- url: 'http://www.an-optional-url-for-a-script-to-add-to-the-page.com',
25
- async: true,
26
- },
27
- {
28
- name: 'Name of the service 2',
29
- category: 'Example Category',
30
- description: 'A script with additional elements to be injected',
31
- body: '',
32
- url: 'https://source-url-of-script.com',
33
- async: true,
34
- addToBody: true,
35
- dataAttrs: [
36
- {
37
- name: 'test',
38
- value: 'foobar',
39
- },
40
- ],
41
- },
42
- {
43
- name: 'Name of the service 3',
44
- category: 'Example Category',
45
- description: 'A script with additional elements to be injected',
46
- body: 'window.foo = "bar"',
47
- async: true,
48
- dataAttrs: [
49
- {
50
- name: 'test',
51
- value: 'foobar',
52
- },
53
- ],
54
- },
55
- {
56
- name: 'Name of the conditionally loaded service',
57
- category: 'Example Category with conditionally loaded service',
58
- description: 'A script with additional elements to be injected',
59
- body: 'window.foo = "bar"',
60
- url: 'https://source-of-conditionally-loaded-script.com',
61
- async: true,
62
- dataAttrs: [
63
- {
64
- name: 'test',
65
- value: 'foobar',
66
- },
67
- ],
68
- shouldLoad: () => false,
69
- },
70
- ],
71
- categories: [
72
- {
73
- name: 'Example Category',
74
- description: 'A short description of the category',
75
- },
76
- ],
77
- container: '#consent-manager',
78
- }
79
-
80
- beforeAll(() => {
81
- HTMLDialogElement.prototype.show = vi.fn()
82
- HTMLDialogElement.prototype.showModal = vi.fn()
83
- HTMLDialogElement.prototype.close = vi.fn()
84
- })
85
-
86
- describe('ConsentManager', () => {
87
- afterEach(() => {
88
- vi.resetModules()
89
- })
90
-
91
- test('shows the banner if the forceShow prop is true', async () => {
92
- const { default: ConsentManager } = await import('./')
93
- render(<ConsentManager {...defaultProps} forceShow={true} />)
94
- expect(screen.getByTestId('consent-banner')).toBeInTheDocument()
95
- })
96
-
97
- test('sets existing preferences and does not show the banner if preferences are already set', async () => {
98
- await runWithMockedImport(
99
- './util/cookies',
100
- {
101
- loadPreferences: () => {
102
- return { loadAll: false, segment: false, version: 0 }
103
- },
104
- // eslint-disable-next-line @typescript-eslint/no-empty-function
105
- savePreferences: () => {},
106
- },
107
- (MockedConsentManager) => {
108
- render(<MockedConsentManager {...defaultProps} forceShow={false} />)
109
- expect(screen.queryByTestId('consent-banner')).not.toBeInTheDocument()
110
- }
111
- )
112
- })
113
-
114
- test('shows the banner if preferences are set but version has increased', async () => {
115
- await runWithMockedImport(
116
- './util/cookies',
117
- {
118
- loadPreferences: () => {
119
- return { loadAll: false, segment: false, version: 1 }
120
- },
121
- // eslint-disable-next-line @typescript-eslint/no-empty-function
122
- savePreferences: () => {},
123
- },
124
- (MockedConsentManager) => {
125
- render(
126
- <MockedConsentManager
127
- {...defaultProps}
128
- forceShow={false}
129
- version={2}
130
- />
131
- )
132
- expect(screen.queryByTestId('consent-banner')).toBeInTheDocument()
133
- }
134
- )
135
- })
136
-
137
- test('if the manage preferences button is clicked, opens the dialog and makes body un-scrollable', async () => {
138
- const { default: ConsentManager } = await import('./')
139
-
140
- render(<ConsentManager {...defaultProps} forceShow={true} />)
141
- fireEvent.click(screen.getByTestId('manage-preferences'))
142
- expect(screen.getByTestId('consent-mgr-dialog')).toBeInTheDocument()
143
- expect(document.body.className).toBe('g-noscroll')
144
- })
145
-
146
- test('opens the dialog when the open function is called', async () => {
147
- const { default: ConsentManager, open } = await import('./')
148
- render(<ConsentManager {...defaultProps} />)
149
- act(() => open())
150
- expect(screen.getByTestId('consent-mgr-dialog')).toBeInTheDocument()
151
- })
152
-
153
- test('if accept all button is clicked, any open dialogs are closed and all analytics are loaded', async () => {
154
- let prefs
155
- await runWithMockedImport(
156
- './util/cookies',
157
- {
158
- // eslint-disable-next-line @typescript-eslint/no-empty-function
159
- loadPreferences: () => {},
160
- savePreferences: (preferences) => {
161
- prefs = preferences
162
- },
163
- },
164
- (MockedConsentManager) => {
165
- render(<MockedConsentManager {...defaultProps} forceShow={true} />)
166
- fireEvent.click(screen.getByTestId('accept'))
167
- }
168
- )
169
- expect(prefs.loadAll).toBe(true)
170
- })
171
-
172
- test('loads segment and additional services if loadAll is passed', async () => {
173
- await runWithMockedImport(
174
- './util/cookies',
175
- {
176
- loadPreferences: () => {
177
- return { loadAll: true, segment: { foo: 'bar' } }
178
- },
179
- // eslint-disable-next-line @typescript-eslint/no-empty-function
180
- savePreferences: () => {},
181
- },
182
- (MockedConsentManager) => {
183
- render(
184
- <MockedConsentManager
185
- {...defaultProps}
186
- forceShow={false}
187
- version={2}
188
- />
189
- )
190
- const html = document.body.innerHTML
191
- // script was injected
192
- expect(html).toMatch(
193
- /<script type="text\/javascript" src="https:\/\/artemis.hashicorp.com\/script/
194
- )
195
- // all services loaded, as well as custom segment
196
- expect(html).toMatch(
197
- /analytics\.load\("iyi06c432UL7SB1r3fQReec4bNwFyzkW", {"integrations":{"All":true,"Segment\.io":true,"foo":"bar"}}\);/
198
- )
199
-
200
- // custom service loaded
201
- expect(html).toMatch(
202
- /src="http:\/\/www.an-optional-url-for-a-script-to-add-to-the-page\.com"/
203
- )
204
-
205
- // conditionally loaded service not loaded
206
- expect(html).not.toMatch(
207
- /src="https:\/\/source-of-conditionally-loaded-script\.com"/
208
- )
209
-
210
- // custom inline script
211
- expect(html).toMatch(/window\.foo = "bar"/)
212
- }
213
- )
214
- })
215
-
216
- describe('handles custom events', () => {
217
- test('manage preferences callback', async () => {
218
- const fn = vi.fn()
219
- await runWithMockedImport(
220
- './components/dialog',
221
- {
222
- default: () => {
223
- return <p data-testid="mocked-dialog">test</p>
224
- },
225
- },
226
- (MockedConsentManager) => {
227
- render(
228
- <MockedConsentManager
229
- {...defaultProps}
230
- forceShow={true}
231
- onManagePreferences={fn}
232
- />
233
- )
234
- fireEvent.click(screen.getByTestId('manage-preferences'))
235
- expect(fn).toHaveBeenCalled()
236
- }
237
- )
238
- })
239
-
240
- test('accept all callback', async () => {
241
- const fn = vi.fn()
242
- await runWithMockedImport(
243
- './components/dialog',
244
- {
245
- default: () => {
246
- return <p data-testid="mocked-dialog">test</p>
247
- },
248
- },
249
- (MockedConsentManager) => {
250
- render(
251
- <MockedConsentManager
252
- {...defaultProps}
253
- forceShow={true}
254
- onAcceptAll={fn}
255
- />
256
- )
257
- fireEvent.click(screen.getByTestId('accept'))
258
- expect(fn).toHaveBeenCalled()
259
- }
260
- )
261
- })
262
- })
263
-
264
- describe('handles conditionally loaded services in dialog', () => {
265
- test('additional services category is not in dialog when shouldLoad() is false in all category services', async () => {
266
- const { default: ConsentManager, open } = await import('./')
267
- render(<ConsentManager {...defaultProps} />)
268
- act(() => open())
269
-
270
- const shownCategory = await screen.findByText('Example Category')
271
- const hiddenCategory = screen.queryByText(
272
- 'Example Category with conditionally loaded service'
273
- )
274
- expect(shownCategory).toBeInTheDocument()
275
- expect(hiddenCategory).not.toBeInTheDocument()
276
- })
277
-
278
- test('additional services category is in dialog when shouldLoad() is false in some category services but service is not in dialog', async () => {
279
- const newService = {
280
- name: 'Name of the new service',
281
- category: 'Example Category with conditionally loaded service',
282
- description: 'A script with additional elements to be injected',
283
- body: 'window.foo = "bar"',
284
- async: true,
285
- dataAttrs: [
286
- {
287
- name: 'test',
288
- value: 'foobar',
289
- },
290
- ],
291
- }
292
-
293
- const { default: ConsentManager, open } = await import('./')
294
- render(
295
- <ConsentManager
296
- {...{
297
- ...defaultProps,
298
- additionalServices: [
299
- ...defaultProps.additionalServices,
300
- newService,
301
- ],
302
- }}
303
- />
304
- )
305
- act(() => open())
306
- const category = await screen.findByText(
307
- 'Example Category with conditionally loaded service'
308
- )
309
-
310
- expect(category).toBeInTheDocument()
311
- const showMoreButton = category.nextSibling!.nextSibling!
312
- // Open category accordion item to make services visible
313
- fireEvent.click(showMoreButton)
314
- const shouldLoadService = await screen.findByText(
315
- 'Name of the new service'
316
- )
317
- const shouldNotLoadService = screen.queryByText(
318
- 'Name of the conditionally loaded service'
319
- )
320
- expect(shouldLoadService).toBeInTheDocument()
321
- expect(shouldNotLoadService).not.toBeInTheDocument()
322
- })
323
- })
324
- })
325
-
326
- // Given an internal module name and mock implementation, mocks the given module and returns a version
327
- // of the component with the module mocked.
328
- async function runWithMockedImport(module, mock, cb) {
329
- vi.doMock(module, () => mock)
330
- return import('./').then(({ default: ConsentManager, open }) => {
331
- cb(ConsentManager, open)
332
- vi.doUnmock(module)
333
- })
334
- }