/
basic.test.ts
40 lines (30 loc) · 1.12 KB
/
basic.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
import type { IWindow } from 'happy-dom'
import { beforeEach, describe, it, vi } from 'vitest'
import '../src/my-button'
declare global {
interface Window extends IWindow {}
}
describe('Button with increment', () => {
beforeEach(async() => {
document.body.innerHTML = '<my-button name="World"></my-button>'
await window.happyDOM.whenAsyncComplete()
await new Promise(resolve => setTimeout(resolve, 0))
})
function getInsideButton(): HTMLElement | null | undefined {
return document.body.querySelector('my-button')?.shadowRoot?.querySelector('button')
}
it('should increment the count on each click', () => {
getInsideButton()?.click()
expect(getInsideButton()?.innerText).toContain('1')
})
it('should show name props', () => {
getInsideButton()
expect(document.body.querySelector('my-button')?.shadowRoot?.innerHTML).toContain('World')
})
it('should dispatch count event on button click', () => {
const spyClick = vi.fn()
document.querySelector('my-button')!.addEventListener('count', spyClick)
getInsideButton()?.click()
expect(spyClick).toHaveBeenCalled()
})
})