2022-03-25 15:35:56 +08:00
|
|
|
import { h, nextTick, ref } from 'vue'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { describe, expect, it, test, vi } from 'vitest'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { getStyle } from '@element-plus/utils'
|
2021-01-31 18:43:33 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
2022-02-24 11:24:34 +08:00
|
|
|
import { ElMessage } from '..'
|
2022-06-22 09:01:23 +08:00
|
|
|
import Message from '../src/method'
|
2020-10-03 20:55:23 +08:00
|
|
|
|
|
|
|
const selector = '.el-message'
|
2021-01-31 18:43:33 +08:00
|
|
|
// TODO: testing the original transition with `nextTick`'
|
2020-10-03 20:55:23 +08:00
|
|
|
|
|
|
|
describe('Message on command', () => {
|
|
|
|
test('it should get component handle', async () => {
|
|
|
|
const handle = Message()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
2020-10-03 20:55:23 +08:00
|
|
|
expect(document.querySelector(selector)).toBeTruthy()
|
|
|
|
handle.close()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
|
|
|
await nextTick()
|
2020-10-03 20:55:23 +08:00
|
|
|
expect(document.querySelector(selector)).toBeFalsy()
|
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
test('it should be able to manually close a message', async () => {
|
|
|
|
const { close } = Message()
|
|
|
|
await rAF()
|
2020-10-03 20:55:23 +08:00
|
|
|
const element = document.querySelector(selector)
|
|
|
|
expect(element).toBeTruthy()
|
2021-01-31 18:43:33 +08:00
|
|
|
close()
|
|
|
|
await rAF()
|
|
|
|
await nextTick()
|
2020-10-03 20:55:23 +08:00
|
|
|
expect(document.querySelector(selector)).toBeNull()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('it should close all messages', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const onClose = vi.fn()
|
2021-01-28 10:46:26 +08:00
|
|
|
const instances = []
|
2020-10-03 20:55:23 +08:00
|
|
|
for (let i = 0; i < 4; i++) {
|
2021-01-28 10:46:26 +08:00
|
|
|
const instance = Message({
|
2021-01-31 18:43:33 +08:00
|
|
|
duration: 0,
|
2020-10-03 20:55:23 +08:00
|
|
|
onClose,
|
|
|
|
})
|
2021-01-28 10:46:26 +08:00
|
|
|
instances.push(instance)
|
2020-10-03 20:55:23 +08:00
|
|
|
}
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
2020-10-03 20:55:23 +08:00
|
|
|
const elements = document.querySelectorAll(selector)
|
|
|
|
expect(elements.length).toBe(4)
|
|
|
|
Message.closeAll()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
2020-10-03 20:55:23 +08:00
|
|
|
expect(onClose).toHaveBeenCalledTimes(4)
|
|
|
|
expect(document.querySelectorAll(selector).length).toBe(0)
|
|
|
|
})
|
|
|
|
|
2022-06-22 19:57:43 +08:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
test('it should stack messages', async () => {
|
2020-10-03 20:55:23 +08:00
|
|
|
const messages = [Message(), Message(), Message()]
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
2020-10-03 20:55:23 +08:00
|
|
|
const elements = document.querySelectorAll(selector)
|
|
|
|
expect(elements.length).toBe(3)
|
|
|
|
|
|
|
|
const getTopValue = (elm: Element): number =>
|
2022-03-08 14:03:32 +08:00
|
|
|
Number.parseInt(getStyle(elm as HTMLElement, 'top'), 10)
|
2020-10-03 20:55:23 +08:00
|
|
|
|
2022-06-21 19:50:16 +08:00
|
|
|
const topValues: number[] = []
|
2021-09-04 19:29:28 +08:00
|
|
|
elements.forEach((e) => {
|
2020-10-03 20:55:23 +08:00
|
|
|
topValues.push(getTopValue(e))
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 1; i < topValues.length; i++) {
|
|
|
|
expect(topValues[i - 1]).toBeLessThan(topValues[i])
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
messages.forEach((m) => m.close())
|
2020-10-03 20:55:23 +08:00
|
|
|
})
|
2020-11-26 19:56:10 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2021-10-30 22:09:36 +08:00
|
|
|
|
|
|
|
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()
|
|
|
|
})
|
2022-02-24 11:24:34 +08:00
|
|
|
|
2022-03-11 20:19:31 +08:00
|
|
|
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"`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-02-24 11:24:34 +08:00
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
2020-10-03 20:55:23 +08:00
|
|
|
})
|