Skip to content

Commit

Permalink
[REPLAY] Refactor unit tests for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibautGeriz committed Feb 15, 2023
1 parent 16b0ad0 commit 61bdbbd
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions packages/rum-core/src/browser/htmlDomUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,42 @@ describe('isElementNode', () => {

if (!isIE()) {
describe('isShadowRoot', () => {
const parent = document.createElement('div')
parent.attachShadow({ mode: 'open' })
const form = document.createElement('form')
const input = document.createElement('input')
input.setAttribute('name', 'host')
form.appendChild(input)
const parameters: Array<[Node, boolean]> = [
[parent.shadowRoot!, true],
[form, false],
[parent, false],
[document.body, false],
[input, false],
[document.createTextNode('hello'), false],
[document.createComment('hello'), false],
const notShadowDom: Node[] = [
document,
document.head,
document.body,
document.createElement('div'),
document.createTextNode('hello'),
document.createComment('hello'),
]

parameters.forEach(([element, result]) => {
it(`should return ${String(result)} for "${String(element)}"`, () => {
expect(isNodeShadowRoot(element)).toBe(result)
notShadowDom.forEach((element) => {
it(`should return false for "${String(element.nodeName)}"`, () => {
expect(isNodeShadowRoot(element)).toBe(false)
})
})

it('should return true for shadow root but not its host', () => {
const parent = document.createElement('div')
const shadowRoot = parent.attachShadow({ mode: 'open' })
expect(isNodeShadowRoot(parent)).toBe(false)
expect(isNodeShadowRoot(shadowRoot)).toBe(true)
})

it('should return false for a[href] since it has a host property', () => {
const link = document.createElement('a')
link.setAttribute('href', 'http://localhost/some/path')
expect(link.host).toBeTruthy()
expect(isNodeShadowRoot(link)).toBe(false)
})

it('should return false for form > input[name="host"] since the host property will be added to the form element', () => {
const form = document.createElement('form')
const input = document.createElement('input')
input.setAttribute('name', 'host')
form.appendChild(input)
expect(isNodeShadowRoot(form)).toBe(false)
})
})
}

Expand Down

0 comments on commit 61bdbbd

Please sign in to comment.