/
happy-dom.test.ts
130 lines (101 loc) · 4.05 KB
/
happy-dom.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* eslint-disable no-restricted-globals */
/**
* @vitest-environment happy-dom
*/
/* eslint-disable vars-on-top */
import { expect, it, vi } from 'vitest'
declare global {
// eslint-disable-next-line no-var
var __property_dom: unknown
// eslint-disable-next-line no-var
var happyDOM: any
}
it('defaults URL to localhost:3000', () => {
expect(location.href).toBe('http://localhost:3000/')
})
it('disableCSSFileLoading is false by default because we didn\'t change options', () => {
expect(window.happyDOM.settings.disableCSSFileLoading).toBe(false)
})
it('defined on self/window are defined on global', () => {
expect(self).toBeDefined()
expect(window).toBeDefined()
expect(top).toBeDefined()
expect(parent).toBeDefined()
expect(self.__property_dom).not.toBeDefined()
expect(window.__property_dom).not.toBeDefined()
expect(globalThis.__property_dom).not.toBeDefined()
globalThis.__property_dom = 'defined_value'
expect(__property_dom).toBe('defined_value')
expect(self.__property_dom).toBe('defined_value')
expect(window.__property_dom).toBe('defined_value')
expect(globalThis.__property_dom).toBe('defined_value')
self.__property_dom = 'test_value'
expect(__property_dom).toBe('test_value')
expect(self.__property_dom).toBe('test_value')
expect(window.__property_dom).toBe('test_value')
expect(globalThis.__property_dom).toBe('test_value')
window.__property_dom = 'new_value'
expect(__property_dom).toBe('new_value')
expect(self.__property_dom).toBe('new_value')
expect(window.__property_dom).toBe('new_value')
expect(globalThis.__property_dom).toBe('new_value')
globalThis.__property_dom = 'global_value'
expect(__property_dom).toBe('global_value')
expect(self.__property_dom).toBe('global_value')
expect(window.__property_dom).toBe('global_value')
expect(globalThis.__property_dom).toBe('global_value')
const obj = {}
self.__property_dom = obj
expect(self.__property_dom).toBe(obj)
expect(window.__property_dom).toBe(obj)
expect(globalThis.__property_dom).toBe(obj)
})
it('usage with defineProperty', () => {
Object.defineProperty(self, '__property_dom', {
get: () => 'self_property',
configurable: true,
})
expect(__property_dom).toBe('self_property')
expect(self.__property_dom).toBe('self_property')
expect(globalThis.__property_dom).toBe('self_property')
expect(window.__property_dom).toBe('self_property')
Object.defineProperty(window, '__property_dom', {
get: () => 'window_property',
configurable: true,
})
expect(__property_dom).toBe('window_property')
expect(self.__property_dom).toBe('window_property')
expect(globalThis.__property_dom).toBe('window_property')
expect(window.__property_dom).toBe('window_property')
Object.defineProperty(globalThis, '__property_dom', {
get: () => 'global_property',
configurable: true,
})
expect(__property_dom).toBe('global_property')
expect(self.__property_dom).toBe('global_property')
expect(globalThis.__property_dom).toBe('global_property')
expect(window.__property_dom).toBe('global_property')
})
it('can call global functions without window works as expected', async () => {
const noop = vi.fn()
expect(() => addEventListener('abort', noop)).not.toThrow()
expect(() => scrollTo()).not.toThrow()
expect(() => requestAnimationFrame(noop)).not.toThrow()
expect(() => window.requestAnimationFrame(noop)).not.toThrow()
expect(() => self.requestAnimationFrame(noop)).not.toThrow()
expect(() => globalThis.requestAnimationFrame(noop)).not.toThrow()
})
it('globals are the same', () => {
expect(window).toBe(globalThis)
expect(window).toBe(global)
expect(window.globalThis).toBe(globalThis)
expect(window.Blob).toBe(globalThis.Blob)
expect(window.globalThis.Blob).toBe(globalThis.Blob)
expect(Blob).toBe(globalThis.Blob)
})
it.skipIf(import.meta.env.VITEST_VM_POOL)('default view references global object', () => {
expect(document.defaultView).toBe(window)
expect(document.defaultView).toBe(globalThis)
const el = document.createElement('div')
expect(el.ownerDocument.defaultView).toBe(globalThis)
})