2022-03-25 15:35:56 +08:00
|
|
|
import { h, nextTick } from 'vue'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { afterEach, describe, expect, it, test, vi } from 'vitest'
|
2021-01-31 18:43:33 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
2021-08-24 13:36:48 +08:00
|
|
|
import Notification, { closeAll } from '../src/notify'
|
2022-03-02 10:55:51 +08:00
|
|
|
import { ElNotification } from '..'
|
2021-08-24 13:36:48 +08:00
|
|
|
|
2021-09-22 01:19:35 +08:00
|
|
|
import type { NotificationHandle } from '../src/notification'
|
2020-08-04 19:03:20 +08:00
|
|
|
|
|
|
|
const selector = '.el-notification'
|
|
|
|
|
|
|
|
describe('Notification on command', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
closeAll()
|
|
|
|
})
|
|
|
|
|
2020-08-06 17:53:58 +08:00
|
|
|
test('it should get component handle', async () => {
|
|
|
|
const handle = Notification()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
2020-08-04 19:03:20 +08:00
|
|
|
expect(document.querySelector(selector)).toBeDefined()
|
2021-01-31 18:43:33 +08:00
|
|
|
|
2020-08-06 17:53:58 +08:00
|
|
|
handle.close()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
2020-12-10 11:09:18 +08:00
|
|
|
await nextTick()
|
2020-08-06 17:53:58 +08:00
|
|
|
expect(document.querySelector(selector)).toBeNull()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
document.querySelector('[class^="container_notification"]')
|
|
|
|
).toBeNull()
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
test('it should be able to render vnode', async () => {
|
|
|
|
const testClassName = 'test-classname'
|
|
|
|
const { close } = Notification({
|
|
|
|
duration: 0,
|
|
|
|
message: h('div', { class: testClassName }, 'test-content'),
|
|
|
|
})
|
|
|
|
|
|
|
|
await rAF()
|
|
|
|
expect(document.querySelector(`.${testClassName}`)).toBeDefined()
|
|
|
|
close()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('it should be able to close notification by manually close', async () => {
|
|
|
|
const { close } = Notification({
|
|
|
|
duration: 0,
|
|
|
|
})
|
|
|
|
await rAF()
|
2020-08-04 19:03:20 +08:00
|
|
|
|
|
|
|
const element = document.querySelector(selector)
|
|
|
|
expect(element).toBeDefined()
|
2021-01-31 18:43:33 +08:00
|
|
|
close()
|
|
|
|
await rAF()
|
|
|
|
await nextTick()
|
|
|
|
|
2020-08-04 19:03:20 +08:00
|
|
|
expect(document.querySelector(selector)).toBeNull()
|
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
test('it should close all notifications', async () => {
|
2021-09-22 01:19:35 +08:00
|
|
|
const notifications: NotificationHandle[] = []
|
2022-04-19 12:46:57 +08:00
|
|
|
const onClose = vi.fn()
|
2020-08-04 19:03:20 +08:00
|
|
|
for (let i = 0; i < 4; i++) {
|
2021-09-04 19:29:28 +08:00
|
|
|
notifications.push(
|
|
|
|
Notification({
|
|
|
|
onClose,
|
|
|
|
duration: 0,
|
|
|
|
})
|
|
|
|
)
|
2020-08-04 19:03:20 +08:00
|
|
|
}
|
2022-04-19 12:46:57 +08:00
|
|
|
// vi.runAllTicks()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
|
|
|
|
2020-08-04 19:03:20 +08:00
|
|
|
expect(document.querySelectorAll(selector).length).toBe(4)
|
|
|
|
closeAll()
|
2022-04-19 12:46:57 +08:00
|
|
|
// vi.runAllTicks()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(notifications.length)
|
2020-08-04 19:03:20 +08:00
|
|
|
expect(document.querySelectorAll(selector).length).toBe(0)
|
|
|
|
})
|
|
|
|
|
2020-08-06 17:53:58 +08:00
|
|
|
test('it should be able to render all types notification', () => {
|
|
|
|
for (const type of ['success', 'warning', 'error', 'info'] as const) {
|
2021-08-24 13:36:48 +08:00
|
|
|
Notification[type]({})
|
2020-08-06 17:53:58 +08:00
|
|
|
expect(document.querySelector(`.el-icon-${type}`)).toBeDefined()
|
|
|
|
}
|
|
|
|
})
|
2021-10-30 22:09:36 +08:00
|
|
|
|
|
|
|
test('it should appendTo specified HTMLElement', async () => {
|
|
|
|
const htmlElement = document.createElement('div')
|
|
|
|
const handle = Notification({
|
|
|
|
appendTo: htmlElement,
|
|
|
|
})
|
|
|
|
await rAF()
|
|
|
|
expect(htmlElement.querySelector(selector)).toBeDefined()
|
|
|
|
|
|
|
|
handle.close()
|
|
|
|
await rAF()
|
|
|
|
await nextTick()
|
|
|
|
expect(htmlElement.querySelector(selector)).toBeNull()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('it should appendTo specified selector', async () => {
|
|
|
|
const htmlElement = document.createElement('div')
|
|
|
|
htmlElement.classList.add('notification-manager')
|
|
|
|
document.body.appendChild(htmlElement)
|
|
|
|
const handle = Notification({
|
|
|
|
appendTo: '.notification-manager',
|
|
|
|
})
|
|
|
|
await rAF()
|
|
|
|
expect(htmlElement.querySelector(selector)).toBeDefined()
|
|
|
|
handle.close()
|
|
|
|
await rAF()
|
|
|
|
await nextTick()
|
|
|
|
expect(htmlElement.querySelector(selector)).toBeNull()
|
|
|
|
})
|
2022-03-02 10:55:51 +08:00
|
|
|
describe('context inheritance', () => {
|
|
|
|
it('should globally inherit context correctly', () => {
|
|
|
|
expect(ElNotification._context).toBe(null)
|
|
|
|
const testContext = {
|
|
|
|
config: {
|
|
|
|
globalProperties: {},
|
|
|
|
},
|
|
|
|
_context: {},
|
|
|
|
}
|
|
|
|
ElNotification.install?.(testContext as any)
|
|
|
|
expect(ElNotification._context).not.toBe(null)
|
|
|
|
expect(ElNotification._context).toBe(testContext._context)
|
|
|
|
// clean up
|
|
|
|
ElNotification._context = null
|
|
|
|
})
|
|
|
|
})
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|