2020-08-04 19:03:20 +08:00
|
|
|
import Notification, { close, closeAll } from '../src/notify'
|
2020-08-06 17:53:58 +08:00
|
|
|
import type { INotificationHandle } from '../src/notification'
|
2020-08-04 19:03:20 +08:00
|
|
|
|
|
|
|
jest.useFakeTimers()
|
|
|
|
|
|
|
|
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()
|
2020-08-04 19:03:20 +08:00
|
|
|
expect(document.querySelector(selector)).toBeDefined()
|
|
|
|
jest.runAllTicks()
|
2020-08-06 17:53:58 +08:00
|
|
|
handle.close()
|
|
|
|
expect(document.querySelector(selector)).toBeNull()
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('it should be able to close notification by manually close', () => {
|
2020-08-06 00:09:09 +08:00
|
|
|
Notification()
|
2020-08-04 19:03:20 +08:00
|
|
|
const element = document.querySelector(selector)
|
|
|
|
expect(element).toBeDefined()
|
|
|
|
close(element.id)
|
|
|
|
expect(document.querySelector(selector)).toBeNull()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('it should close all notifications', () => {
|
2020-08-06 17:53:58 +08:00
|
|
|
const notifications: INotificationHandle[] = []
|
2020-08-06 00:09:09 +08:00
|
|
|
const onClose = jest.fn()
|
2020-08-04 19:03:20 +08:00
|
|
|
for (let i = 0; i < 4; i++) {
|
2020-08-06 00:09:09 +08:00
|
|
|
notifications.push(Notification({
|
|
|
|
onClose,
|
|
|
|
}))
|
2020-08-04 19:03:20 +08:00
|
|
|
}
|
|
|
|
expect(document.querySelectorAll(selector).length).toBe(4)
|
|
|
|
closeAll()
|
2020-08-06 00:09:09 +08:00
|
|
|
for (let i = 0; i < notifications.length; i++) {
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(4)
|
|
|
|
}
|
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) {
|
|
|
|
Notification[type]()
|
|
|
|
expect(document.querySelector(`.el-icon-${type}`)).toBeDefined()
|
|
|
|
}
|
|
|
|
})
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|