2021-01-31 18:43:33 +08:00
|
|
|
import { h, ComponentPublicInstance, nextTick } from 'vue'
|
2021-08-24 13:36:48 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
|
|
|
import makeMount from '@element-plus/test-utils/make-mount'
|
2021-01-31 18:43:33 +08:00
|
|
|
import * as domExports from '@element-plus/utils/dom'
|
|
|
|
import { EVENT_CODE } from '@element-plus/utils/aria'
|
2021-05-24 09:58:59 +08:00
|
|
|
import PopupManager from '@element-plus/utils/popup-manager'
|
2021-08-24 13:36:48 +08:00
|
|
|
import Notification from '../src/index.vue'
|
|
|
|
|
|
|
|
import type { VueWrapper } from '@vue/test-utils'
|
2020-08-04 19:03:20 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
|
|
|
jest.useFakeTimers()
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
const onClose = jest.fn()
|
2020-08-04 19:03:20 +08:00
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
const _mount = makeMount(Notification, {
|
|
|
|
props: {
|
|
|
|
onClose,
|
|
|
|
},
|
|
|
|
})
|
2020-08-04 19:03:20 +08:00
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
describe('Notification.vue', () => {
|
2020-08-04 19:03:20 +08:00
|
|
|
describe('render', () => {
|
|
|
|
test('basic render test', () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
slots: {
|
|
|
|
default: AXIOM,
|
|
|
|
},
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
|
|
|
|
const vm = wrapper.vm as ComponentPublicInstance<{
|
|
|
|
visible: boolean
|
|
|
|
typeClass: string
|
|
|
|
horizontalClass: string
|
|
|
|
positionStyle: Record<string, string>
|
|
|
|
}>
|
|
|
|
|
2020-08-04 19:03:20 +08:00
|
|
|
expect(wrapper.text()).toEqual(AXIOM)
|
2021-01-31 18:43:33 +08:00
|
|
|
expect(vm.visible).toBe(true)
|
|
|
|
expect(vm.typeClass).toBe('')
|
|
|
|
expect(vm.horizontalClass).toBe('right')
|
2021-05-24 09:58:59 +08:00
|
|
|
expect(vm.positionStyle).toEqual({ top: '0px','z-index': 0 })
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should be able to render VNode', () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
slots: {
|
2021-01-31 18:43:33 +08:00
|
|
|
default: h(
|
|
|
|
'span',
|
|
|
|
{
|
|
|
|
class: 'text-node',
|
|
|
|
},
|
|
|
|
AXIOM,
|
|
|
|
),
|
2020-08-04 19:03:20 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(wrapper.find('.text-node').exists()).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should be able to render raw HTML tag with dangerouslyUseHTMLString flag', () => {
|
|
|
|
const tagClass = 'test-class'
|
|
|
|
const HTMLWrapper = _mount({
|
|
|
|
props: {
|
|
|
|
dangerouslyUseHTMLString: true,
|
|
|
|
message: `<strong class=${tagClass}>${AXIOM}</strong>`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(HTMLWrapper.find(`.${tagClass}`).exists()).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should not be able to render raw HTML tag without dangerouslyUseHTMLString flag', () => {
|
|
|
|
const tagClass = 'test-class'
|
|
|
|
const HTMLWrapper = _mount({
|
|
|
|
props: {
|
|
|
|
dangerouslyUseHTMLString: false,
|
|
|
|
message: `<strong class=${tagClass}>${AXIOM}</strong>`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(HTMLWrapper.find(`.${tagClass}`).exists()).toBe(false)
|
|
|
|
})
|
2021-05-24 09:58:59 +08:00
|
|
|
|
|
|
|
test('should be able to render z-index style with zIndex flag', () => {
|
|
|
|
const zIndex = PopupManager.nextZIndex()
|
|
|
|
const wrapper = _mount({
|
|
|
|
props:{
|
|
|
|
zIndex: zIndex,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const vm = wrapper.vm as ComponentPublicInstance<{
|
|
|
|
positionStyle: Record<string, string>
|
|
|
|
}>
|
|
|
|
|
|
|
|
expect(vm.positionStyle).toEqual({ top: '0px','z-index': zIndex })
|
|
|
|
})
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('lifecycle', () => {
|
|
|
|
let onMock
|
|
|
|
let offMock
|
|
|
|
beforeEach(() => {
|
|
|
|
onMock = jest.spyOn(domExports, 'on').mockReset()
|
|
|
|
offMock = jest.spyOn(domExports, 'off').mockReset()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
onMock.mockRestore()
|
|
|
|
offMock.mockRestore()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should add event listener to target element when init', () => {
|
|
|
|
jest.spyOn(domExports, 'on')
|
|
|
|
jest.spyOn(domExports, 'off')
|
|
|
|
const wrapper = _mount({
|
|
|
|
slots: {
|
|
|
|
default: AXIOM,
|
|
|
|
},
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
expect(domExports.on).toHaveBeenCalled()
|
2020-08-04 19:03:20 +08:00
|
|
|
wrapper.unmount()
|
|
|
|
expect(domExports.off).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Notification.type', () => {
|
2020-08-06 17:53:58 +08:00
|
|
|
test('should be able to render typed notification', () => {
|
|
|
|
let wrapper: VueWrapper<ComponentPublicInstance>
|
|
|
|
|
|
|
|
for (const type of ['success', 'warning', 'info', 'error']) {
|
|
|
|
wrapper = _mount({
|
|
|
|
props: {
|
|
|
|
type,
|
|
|
|
},
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
expect(wrapper.find('.el-notification__icon').classes()).toContain(
|
|
|
|
`el-icon-${type}`,
|
|
|
|
)
|
2020-08-06 17:53:58 +08:00
|
|
|
}
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should not be able to render invalid type icon', () => {
|
|
|
|
const type = 'some-type'
|
|
|
|
const wrapper = _mount({
|
|
|
|
props: {
|
|
|
|
type,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
expect(wrapper.find('.el-notification__icon').classes()).not.toContain(
|
|
|
|
`el-icon-${type}`,
|
|
|
|
)
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('event handlers', () => {
|
|
|
|
test('it should be able to close the notification by clicking close button', async () => {
|
|
|
|
const onClose = jest.fn()
|
|
|
|
const wrapper = _mount({
|
|
|
|
slots: {
|
|
|
|
default: AXIOM,
|
|
|
|
},
|
|
|
|
props: { onClose },
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
2020-08-04 19:03:20 +08:00
|
|
|
|
|
|
|
const closeBtn = wrapper.find('.el-notification__closeBtn')
|
|
|
|
expect(closeBtn.exists()).toBe(true)
|
2021-01-31 18:43:33 +08:00
|
|
|
await closeBtn.trigger('click')
|
|
|
|
await rAF()
|
|
|
|
await nextTick()
|
2020-08-04 19:03:20 +08:00
|
|
|
expect(onClose).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
test('should be able to close after duration', async () => {
|
2020-08-04 19:03:20 +08:00
|
|
|
const duration = 100
|
|
|
|
const wrapper = _mount({
|
|
|
|
props: {
|
|
|
|
duration,
|
|
|
|
},
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
const vm = wrapper.vm as ComponentPublicInstance<{ visible: boolean; }>
|
|
|
|
|
2020-08-04 19:03:20 +08:00
|
|
|
jest.runAllTimers()
|
2021-01-31 18:43:33 +08:00
|
|
|
|
|
|
|
await rAF()
|
|
|
|
expect(vm.visible).toBe(false)
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should be able to prevent close itself when hover over', async () => {
|
|
|
|
const duration = 100
|
|
|
|
const wrapper = _mount({
|
|
|
|
props: {
|
|
|
|
duration,
|
|
|
|
},
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
|
|
|
|
const vm = wrapper.vm as ComponentPublicInstance<{ visible: boolean; }>
|
|
|
|
|
|
|
|
await rAF()
|
2020-08-04 19:03:20 +08:00
|
|
|
await wrapper.find('[role=alert]').trigger('mouseenter')
|
|
|
|
jest.runAllTimers()
|
2021-01-31 18:43:33 +08:00
|
|
|
expect(vm.visible).toBe(true)
|
2020-08-04 19:03:20 +08:00
|
|
|
await wrapper.find('[role=alert]').trigger('mouseleave')
|
2021-01-31 18:43:33 +08:00
|
|
|
// expect(wrapper.vm.timer).not.toBe(null)
|
|
|
|
expect(vm.visible).toBe(true)
|
|
|
|
// expect(wrapper.vm.closed).toBe(false)
|
2020-08-04 19:03:20 +08:00
|
|
|
jest.runAllTimers()
|
2021-01-31 18:43:33 +08:00
|
|
|
expect(vm.visible).toBe(false)
|
|
|
|
// expect(wrapper.vm.timer).toBe(null)
|
|
|
|
// expect(wrapper.vm.closed).toBe(true)
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
test('should not be able to close when duration is set to 0', async () => {
|
2020-08-04 19:03:20 +08:00
|
|
|
const duration = 0
|
|
|
|
const wrapper = _mount({
|
|
|
|
props: {
|
|
|
|
duration,
|
|
|
|
},
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
|
|
|
|
const vm = wrapper.vm as ComponentPublicInstance<{ visible: boolean; }>
|
|
|
|
await rAF()
|
|
|
|
expect(vm.visible).toBe(true)
|
2020-08-04 19:03:20 +08:00
|
|
|
jest.runAllTimers()
|
2021-01-31 18:43:33 +08:00
|
|
|
|
|
|
|
await rAF()
|
|
|
|
expect(vm.visible).toBe(true)
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should be able to handle click event', async () => {
|
2021-01-31 18:43:33 +08:00
|
|
|
const onClick = jest.fn()
|
2020-08-04 19:03:20 +08:00
|
|
|
const wrapper = _mount({
|
|
|
|
props: {
|
|
|
|
duration: 0,
|
2021-01-31 18:43:33 +08:00
|
|
|
onClick,
|
2020-08-04 19:03:20 +08:00
|
|
|
},
|
|
|
|
})
|
2020-08-06 00:09:09 +08:00
|
|
|
|
2020-08-04 19:03:20 +08:00
|
|
|
await wrapper.trigger('click')
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(1)
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should be able to delete timer when press delete', async () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
slots: {
|
|
|
|
default: AXIOM,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-01-31 18:43:33 +08:00
|
|
|
const vm = wrapper.vm as ComponentPublicInstance<{ visible: boolean; }>
|
|
|
|
|
2020-08-04 19:03:20 +08:00
|
|
|
const event = new KeyboardEvent('keydown', {
|
2020-09-22 21:01:10 +08:00
|
|
|
code: EVENT_CODE.backspace,
|
2020-08-04 19:03:20 +08:00
|
|
|
babels: true,
|
|
|
|
} as any)
|
|
|
|
document.dispatchEvent(event)
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
|
|
|
expect(vm.visible).toBe(true)
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should be able to close the notification immediately when press esc', async () => {
|
|
|
|
const wrapper = _mount({
|
|
|
|
slots: {
|
|
|
|
default: AXIOM,
|
|
|
|
},
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
const vm = wrapper.vm as ComponentPublicInstance<{ visible: boolean; }>
|
2020-08-04 19:03:20 +08:00
|
|
|
// Same as above
|
|
|
|
const event = new KeyboardEvent('keydown', {
|
2020-09-22 21:01:10 +08:00
|
|
|
code: EVENT_CODE.esc,
|
2020-08-04 19:03:20 +08:00
|
|
|
// eslint-disable-next-line
|
|
|
|
} as any)
|
2021-01-31 18:43:33 +08:00
|
|
|
|
2020-08-04 19:03:20 +08:00
|
|
|
document.dispatchEvent(event)
|
|
|
|
jest.runAllTimers()
|
2021-01-31 18:43:33 +08:00
|
|
|
await rAF()
|
|
|
|
expect(vm.visible).toBe(false)
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|