/
index.test.ts
190 lines (147 loc) · 4.75 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { promiseTimeout } from '@vueuse/shared'
import { describe, expect, it, vi } from 'vitest'
import { useMutationObserver } from '.'
describe('useMutationObserver', () => {
it('should be defined', () => {
expect(useMutationObserver).toBeDefined()
})
it('should work with attributes', async () => {
const cb = vi.fn()
const target = document.createElement('div')
target.setAttribute('id', 'header')
useMutationObserver(target, cb, {
attributes: true,
})
target.setAttribute('id', 'footer')
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(1)
target.setAttribute('id', 'header')
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(2)
const record = cb.mock.calls[0][0][0]
expect(record).toBeInstanceOf(MutationRecord)
expect(record.target).toBe(target)
})
it('should work with childList', async () => {
const target = document.createElement('div')
const cb = vi.fn()
useMutationObserver(target, cb, {
childList: true,
})
target.appendChild(document.createElement('div'))
await promiseTimeout(10)
expect(cb).toHaveBeenCalled()
})
it('should work with subtree', async () => {
const target = document.createElement('div')
const cb = vi.fn()
useMutationObserver(target, cb, {
subtree: true,
childList: true,
})
const child = document.createElement('div')
target.appendChild(child)
await promiseTimeout(10)
expect(cb).toHaveBeenCalled()
child.appendChild(document.createElement('div'))
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(2)
})
it('should work with characterData', async () => {
const target = document.createTextNode('123')
const cb = vi.fn()
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
useMutationObserver(target, cb, {
characterData: true,
})
target.data = 'content'
await promiseTimeout(10)
expect(cb).toHaveBeenCalled()
target.data = 'footer'
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(2)
})
it('should work with attributeFilter', async () => {
const target = document.createElement('div')
const cb = vi.fn()
useMutationObserver(target, cb, {
attributes: true,
attributeFilter: ['id'],
})
target.setAttribute('id', 'footer')
await promiseTimeout(10)
expect(cb).toHaveBeenCalled()
target.setAttribute('class', 'footer')
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(1)
})
it('should work with attributeOldValue', async () => {
const target = document.createElement('div')
const cb = vi.fn()
useMutationObserver(target, cb, {
attributes: true,
attributeOldValue: true,
})
target.setAttribute('id', 'footer')
await promiseTimeout(10)
expect(cb).toHaveBeenCalled()
const record = cb.mock.calls[0][0][0]
expect(record.oldValue).toBe(null)
target.setAttribute('id', 'header')
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(2)
const record2 = cb.mock.calls[1][0][0]
expect(record2.oldValue).toBe('footer')
})
it('should work with characterDataOldValue', async () => {
const target = document.createTextNode('123')
const cb = vi.fn()
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
useMutationObserver(target, cb, {
characterData: true,
characterDataOldValue: true,
})
target.data = 'content'
await promiseTimeout(10)
expect(cb).toHaveBeenCalled()
const record = cb.mock.calls[0][0][0]
expect(record.oldValue).toBe('123')
target.data = 'footer'
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(2)
const record2 = cb.mock.calls[1][0][0]
expect(record2.oldValue).toBe('content')
})
it('should work with stop', async () => {
const target = document.createElement('div')
const cb = vi.fn()
const { stop } = useMutationObserver(target, cb, {
attributes: true,
})
target.setAttribute('id', 'footer')
await promiseTimeout(10)
expect(cb).toHaveBeenCalled()
stop()
target.setAttribute('id', 'header')
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(1)
})
it('should work with takeRecords', async () => {
const target = document.createElement('div')
const cb = vi.fn()
const { takeRecords } = useMutationObserver(target, cb, {
attributes: true,
})
target.setAttribute('id', 'footer')
await promiseTimeout(10)
expect(cb).toHaveBeenCalledTimes(1)
target.setAttribute('id', 'header')
const records = takeRecords()
await promiseTimeout(10)
expect(records).toHaveLength(1)
expect(records![0].target).toBe(target)
expect(cb).toHaveBeenCalledTimes(1)
})
})