/
index.test.ts
61 lines (44 loc) · 1.45 KB
/
index.test.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
import { useActiveElement } from '.'
describe('useActiveElement', () => {
let shadowHost: HTMLElement
let input: HTMLInputElement
let shadowInput: HTMLInputElement
let shadowRoot: ShadowRoot
beforeEach(() => {
shadowHost = document.createElement('div')
shadowRoot = shadowHost.attachShadow({ mode: 'open' })
input = document.createElement('input')
shadowInput = input.cloneNode() as HTMLInputElement
shadowRoot.appendChild(shadowInput)
document.body.appendChild(input)
document.body.appendChild(shadowHost)
})
afterEach(() => {
shadowHost.remove()
input.remove()
})
it('should be defined', () => {
expect(useActiveElement).toBeDefined()
})
it('should initialise correctly', () => {
const activeElement = useActiveElement()
expect(activeElement.value).to.equal(document.body)
})
it('should initialise with already-active element', () => {
input.focus()
const activeElement = useActiveElement()
expect(activeElement.value).to.equal(input)
})
it('should accept custom document', () => {
const activeElement = useActiveElement({ document: shadowRoot })
shadowInput.focus()
expect(activeElement.value).to.equal(shadowInput)
})
it('should observe focus/blur events', () => {
const activeElement = useActiveElement()
input.focus()
expect(activeElement.value).to.equal(input)
input.blur()
expect(activeElement.value).to.equal(document.body)
})
})