Skip to content

Commit

Permalink
✅ fix unit tests for IE
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Oct 17, 2022
1 parent 13d5248 commit b347287
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isIE } from '@datadog/browser-core'
import type { IsolatedDom } from '../../../../test/createIsolatedDom'
import { createIsolatedDom } from '../../../../test/createIsolatedDom'
import { getSelectorsFromElement } from './getSelectorsFromElement'
import { getSelectorsFromElement, supportScopeSelector } from './getSelectorsFromElement'

describe('getSelectorFromElement', () => {
let isolatedDom: IsolatedDom
Expand Down Expand Up @@ -131,7 +131,14 @@ describe('getSelectorFromElement', () => {
<button data-testid="foo"></button>
</div>
`)
).toBe('BODY>BUTTON[data-testid="foo"]')
).toBe(
supportScopeSelector()
? 'BODY>BUTTON[data-testid="foo"]'
: // Degraded support for browsers not supporting scoped selector: the selector is still
// correct, but its quality is a bit worse, as using a stable attribute reduce the
// chances of matching a completely unrelated element.
'BODY>BUTTON'
)
})
})
})
Expand All @@ -158,15 +165,20 @@ describe('getSelectorFromElement', () => {
})

it('only consider direct descendants (>) of the parent element', () => {
if (isIE()) {
pending('IE does not support :scope selectors')
}
expect(
getCombinedSelector(`
<div><div><button></button></div></div>
<div><button target></button></div>
`)
).toBe('BODY>DIV>BUTTON')
).toBe(
supportScopeSelector()
? 'BODY>DIV>BUTTON'
: // Degraded support for browsers not supporting scoped selector: the selector is still
// correct, but its quality is a bit worse as using a `nth-of-type` selector is a bit
// too specific and might not match if an element is conditionally inserted before the
// target.
'BODY>DIV:nth-of-type(2)>BUTTON'
)
})
})

Expand All @@ -192,7 +204,15 @@ describe('getSelectorFromElement', () => {
<div><div><button></button></div></div>
<div><button target></button></div>
`)
).toBe('BODY>DIV>BUTTON')
).toBe(
supportScopeSelector()
? 'BODY>DIV>BUTTON'
: // Degraded support for browsers not supporting scoped selector: the selector is still
// correct, but its quality is a bit worse as using a `nth-of-type` selector is a bit
// too specific and might not match if an element is conditionally inserted before the
// target.
'BODY>DIV:nth-of-type(2)>BUTTON'
)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ function combineSelector(parent: string, child: string | undefined): string {
}

let supportScopeSelectorCache: boolean | undefined
function supportScopeSelector() {
export function supportScopeSelector() {
return false
if (supportScopeSelectorCache === undefined) {
try {
document.querySelector(':scope')
Expand Down

0 comments on commit b347287

Please sign in to comment.