Skip to content

Commit f1e50bf

Browse files
committed
test: use node "react-dom/server" to avoid Testing Library issue
1 parent cfe991a commit f1e50bf

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

tests/components/UsercentricsScript.test.tsx

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,64 @@
1-
import { render } from '@testing-library/react'
1+
/** @jest-environment node */
2+
23
import React from 'react'
4+
import { renderToStaticMarkup } from 'react-dom/server'
35

46
import { UsercentricsScript } from '../../src/components/UsercentricsScript.js'
57

68
describe('Usercentrics', () => {
79
describe('components', () => {
810
describe('UsercentricsScript', () => {
911
it('should render basic props', () => {
10-
const { container } = render(<UsercentricsScript settingsId="1234" />)
12+
const result = renderToStaticMarkup(<UsercentricsScript settingsId="1234" />)
1113

12-
expect(container.firstChild).toMatchInlineSnapshot(`
13-
<script
14-
async=""
15-
data-settings-id="1234"
16-
id="usercentrics-cmp"
17-
src="https://app.usercentrics.eu/browser-ui/latest/loader.js"
18-
/>
19-
`)
14+
expect(result).toMatchInlineSnapshot(
15+
`"<script async="" data-settings-id="1234" id="usercentrics-cmp" src="https://app.usercentrics.eu/browser-ui/latest/loader.js"></script>"`,
16+
)
2017
})
2118

2219
it('should render preview attribute', () => {
23-
const { container } = render(<UsercentricsScript settingsId="1234" version="preview" />)
20+
const result = renderToStaticMarkup(<UsercentricsScript settingsId="1234" version="preview" />)
2421

25-
expect(container.firstChild).toHaveAttribute('data-version', 'preview')
22+
expect(result).toMatch('data-version="preview"')
2623
})
2724

2825
it('should allow disabling default async prop', () => {
29-
const { container } = render(<UsercentricsScript settingsId="1234" async={undefined} defer />)
26+
const result = renderToStaticMarkup(<UsercentricsScript settingsId="1234" async={undefined} defer />)
3027

31-
expect(container.firstChild).not.toHaveAttribute('async')
32-
expect(container.firstChild).toHaveAttribute('defer', '')
28+
expect(result).not.toMatch('async')
29+
expect(result).toMatch('defer')
3330
})
3431

3532
it('should allow specifying UI version', () => {
36-
const { container } = render(<UsercentricsScript settingsId="1234" uiVersion="3.24.0" />)
33+
const result = renderToStaticMarkup(<UsercentricsScript settingsId="1234" uiVersion="3.24.0" />)
3734

38-
expect(container.firstChild).toHaveAttribute(
39-
'src',
40-
'https://app.usercentrics.eu/browser-ui/3.24.0/loader.js',
41-
)
35+
expect(result).toMatch('src="https://app.usercentrics.eu/browser-ui/3.24.0/loader.js"')
4236
})
4337

4438
it('should allow passing integrity prop', () => {
45-
const { container } = render(
39+
const result = renderToStaticMarkup(
4640
<UsercentricsScript
4741
settingsId="1234"
4842
uiVersion="3.24.0"
4943
integrity="sha384-WRloNuM/QNkzJ4GkUAZgJ5CWgTFhVjsLKVQbACSHGOifUvw2WJk1QaY9mphkn96U"
5044
/>,
5145
)
5246

53-
expect(container.firstChild).toHaveAttribute(
54-
'integrity',
55-
'sha384-WRloNuM/QNkzJ4GkUAZgJ5CWgTFhVjsLKVQbACSHGOifUvw2WJk1QaY9mphkn96U',
47+
expect(result).toMatch(
48+
'integrity="sha384-WRloNuM/QNkzJ4GkUAZgJ5CWgTFhVjsLKVQbACSHGOifUvw2WJk1QaY9mphkn96U"',
5649
)
5750
})
5851

5952
it('should not allow the src prop', () => {
60-
const { container } = render(
53+
const result = renderToStaticMarkup(
6154
<UsercentricsScript
6255
settingsId="1234"
6356
/** @ts-expect-error: Type 'string' is not assignable to type 'undefined'. */
6457
src="test.js"
6558
/>,
6659
)
6760

68-
expect(container.firstChild).toHaveAttribute(
69-
'src',
70-
'https://app.usercentrics.eu/browser-ui/latest/loader.js',
71-
)
61+
expect(result).toMatch('src="https://app.usercentrics.eu/browser-ui/latest/loader.js"')
7262
})
7363
})
7464
})

0 commit comments

Comments
 (0)