You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte-kit-vice/src/uix/adom/dom-context.test.ts

57 lines
1.6 KiB

// @vitest-environment jsdom
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { DOMContext } from './dom-context.svelte'
describe('DOMContext', () => {
beforeEach(() => {
document.body.innerHTML = ''
})
it('falls back to the global document when no element is present', () => {
const node = document.createElement('div')
node.id = 'global-node'
document.body.appendChild(node)
const context = new DOMContext(() => null)
expect(context.getDocument()).toBe(document)
expect(context.getWindow()).toBe(window)
expect(context.getElementById('global-node')).toBe(node)
expect(context.querySelector('#global-node')).toBe(node)
})
it('scopes queries and active element to the element root', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })
const input = document.createElement('input')
input.id = 'shadow-input'
shadow.appendChild(input)
document.body.appendChild(host)
const context = new DOMContext(() => input)
input.focus()
expect(context.provider.current).toBe(shadow)
expect(context.querySelector('#shadow-input')).toBe(input)
expect(context.getActiveElement()).toBe(input)
expect(context.isActiveElement(input)).toBe(true)
})
it('proxies timers through the provider window', () => {
vi.useFakeTimers()
const context = new DOMContext(() => null)
const callback = vi.fn()
const timeoutId = context.setTimeout(callback, 10)
vi.advanceTimersByTime(10)
expect(callback).toHaveBeenCalledTimes(1)
context.clearTimeout(timeoutId)
vi.useRealTimers()
})
})

Powered by TurnKey Linux.