/
message-manager.test.tsx
178 lines (156 loc) Β· 4.89 KB
/
message-manager.test.tsx
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
import { h, nextTick, ref } from 'vue'
import { describe, expect, it, test, vi } from 'vitest'
import { getStyle } from '@element-plus/utils'
import { rAF } from '@element-plus/test-utils/tick'
import { ElMessage } from '..'
import Message from '../src/method'
const selector = '.el-message'
// TODO: testing the original transition with `nextTick`'
describe('Message on command', () => {
test('it should get component handle', async () => {
const handle = Message()
await rAF()
expect(document.querySelector(selector)).toBeTruthy()
handle.close()
await rAF()
await nextTick()
expect(document.querySelector(selector)).toBeFalsy()
})
test('it should be able to manually close a message', async () => {
const { close } = Message()
await rAF()
const element = document.querySelector(selector)
expect(element).toBeTruthy()
close()
await rAF()
await nextTick()
expect(document.querySelector(selector)).toBeNull()
})
test('it should close all messages', async () => {
const onClose = vi.fn()
const instances = []
for (let i = 0; i < 4; i++) {
const instance = Message({
duration: 0,
onClose,
})
instances.push(instance)
}
await rAF()
const elements = document.querySelectorAll(selector)
expect(elements.length).toBe(4)
Message.closeAll()
await rAF()
expect(onClose).toHaveBeenCalledTimes(4)
expect(document.querySelectorAll(selector).length).toBe(0)
})
test('it should close all messages of the specified type', async () => {
const onClose = vi.fn()
const instances = []
const success = 'success'
for (let i = 0; i < 4; i++) {
const instance = Message({
type: success,
duration: 0,
onClose,
})
instances.push(instance)
}
for (let i = 0; i < 2; i++) {
const instance = Message({
duration: 0,
onClose,
})
instances.push(instance)
}
await rAF()
const elements = document.querySelectorAll(selector)
const successElements = document.querySelectorAll(`${selector}--${success}`)
expect(elements.length).toBe(6)
expect(successElements.length).toBe(4)
Message.closeAll(success)
await rAF()
expect(onClose).toHaveBeenCalledTimes(4)
expect(document.querySelectorAll(selector).length).toBe(2)
Message.closeAll()
})
test('it should stack messages', async () => {
const messages = [Message(), Message(), Message()]
await rAF()
const elements = document.querySelectorAll(selector)
expect(elements.length).toBe(3)
const getTopValue = (elm: Element): number =>
Number.parseInt(getStyle(elm as HTMLElement, 'top'), 10)
const topValues: number[] = []
elements.forEach((e) => {
topValues.push(getTopValue(e))
})
for (let i = 1; i < topValues.length; i++) {
expect(topValues[i - 1]).toBeLessThan(topValues[i])
}
messages.forEach((m) => m.close())
})
test('it should have 4 other types of message', () => {
expect(Message.success).toBeInstanceOf(Function)
expect(Message.warning).toBeInstanceOf(Function)
expect(Message.info).toBeInstanceOf(Function)
expect(Message.error).toBeInstanceOf(Function)
})
test('it should appendTo specified HTMLElement', async () => {
const htmlElement = document.createElement('div')
const handle = Message({
appendTo: htmlElement,
})
await rAF()
expect(htmlElement.querySelector(selector)).toBeTruthy()
handle.close()
await rAF()
await nextTick()
expect(htmlElement.querySelector(selector)).toBeFalsy()
})
test('it should appendTo specified selector', async () => {
const htmlElement = document.createElement('div')
htmlElement.classList.add('message-manager')
document.body.appendChild(htmlElement)
const handle = Message({
appendTo: '.message-manager',
})
await rAF()
expect(htmlElement.querySelector(selector)).toBeTruthy()
handle.close()
await rAF()
await nextTick()
expect(htmlElement.querySelector(selector)).toBeFalsy()
})
test('it should render vnode function', async () => {
const i = ref(0)
Message({
message: () => h('div', i.value),
})
await rAF()
expect(document.querySelector(selector)?.textContent).toMatchInlineSnapshot(
`"0"`
)
i.value++
await rAF()
expect(document.querySelector(selector)?.textContent).toMatchInlineSnapshot(
`"1"`
)
})
describe('context inheritance', () => {
it('should globally inherit context correctly', () => {
expect(ElMessage._context).toBe(null)
const testContext = {
config: {
globalProperties: {},
},
_context: {},
}
ElMessage.install?.(testContext as any)
expect(ElMessage._context).not.toBe(null)
expect(ElMessage._context).toBe(testContext._context)
// clean up
ElMessage._context = null
})
})
})