forked from vercel/next.js
/
is-equal-node.unit.test.js
48 lines (42 loc) · 1.7 KB
/
is-equal-node.unit.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @jest-environment jsdom
*/
/* eslint-env jest */
import { isEqualNode } from 'next/dist/client/head-manager'
const createScriptElement = (attrs = {}) => {
const el = document.createElement('script')
for (const k in attrs) el.setAttribute(k, attrs[k])
return el
}
describe('isEqualNode', () => {
it('Node should equal itself', () => {
const el = createScriptElement()
expect(isEqualNode(el, el)).toBe(true)
})
it('Node should equal equivalent node that has no nonce', () => {
const el1 = createScriptElement()
const el2 = createScriptElement()
expect(isEqualNode(el1, el2)).toBe(true)
})
it('Node should equal equivalent node that has same nonce property, even if the original node has no html nonce attribute value', () => {
const el1 = createScriptElement({ nonce: 'abc123' })
// Simulate Chrome/FF browser behavior of stripping off nonce value when adding element to the document
el1.setAttribute('nonce', '')
el1.nonce = 'abc123'
const el2 = createScriptElement({ nonce: 'abc123' })
expect(isEqualNode(el1, el2)).toBe(true)
})
it('Node should not equal node with different nonce value', () => {
const el1 = createScriptElement({ nonce: 'abc123' })
// Simulate Chrome/FF browser behavior of stripping off nonce value when adding element to the document
el1.setAttribute('nonce', '')
el1.nonce = 'abc123'
const el2 = createScriptElement({ nonce: 'xyz' })
expect(isEqualNode(el1, el2)).toBe(false)
})
it('Node should not equal node with differnt html attribute value', () => {
const el1 = createScriptElement({ src: '1.js' })
const el2 = createScriptElement({ src: '2.js' })
expect(isEqualNode(el1, el2)).toBe(false)
})
})