|
| 1 | +import { h, markRaw } from 'vue' |
1 | 2 | import { mount } from '@vue/test-utils'
|
2 |
| -import { describe, expect, test } from 'vitest' |
| 3 | +import { vi, describe, expect, test } from 'vitest' |
3 | 4 | import FNotificationVue from '../src/notification.vue'
|
| 5 | +import { FIconCross, FIconInfo } from '@fighting-design/fighting-icon' |
| 6 | +import { FIGHTING_TYPE } from '../../_tokens' |
| 7 | +import type { NotificationPlacement } from '../src/interface' |
| 8 | +import type { NotificationProps } from '../src/props' |
| 9 | +import type { ComponentPublicInstance } from 'vue' |
4 | 10 |
|
5 |
| -describe('FNotification', () => { |
6 |
| - test('base', async () => { |
| 11 | +const NAME = 'Mikoto Misaka' |
| 12 | +const AXIOM = 'The electric light dancing at your fingertips is my unchanging faith' |
| 13 | + |
| 14 | +type NotificationInstance = ComponentPublicInstance<NotificationProps & { |
| 15 | + visible: boolean |
| 16 | +}> |
| 17 | + |
| 18 | +describe('FNotification', () => { |
| 19 | + test('basic render', async () => { |
7 | 20 | const wrapper = mount(FNotificationVue, {
|
8 |
| - props: { message: '这是一段内容', type: 'success' } |
| 21 | + props: { |
| 22 | + title: NAME, |
| 23 | + message: AXIOM, |
| 24 | + } |
9 | 25 | })
|
10 | 26 | expect(wrapper.find('.f-notification').exists()).toBe(true)
|
11 |
| - expect(wrapper.find('.f-notification__content').text()).toBe('这是一段内容') |
12 |
| - expect(wrapper.find('.f-notification__success').exists()).toBe(true) |
13 |
| - await wrapper.setProps({ type: 'primary' }) |
14 |
| - expect(wrapper.find('.f-notification__primary').exists()).toBe(true) |
15 |
| - await wrapper.setProps({ close: true }) |
16 |
| - expect(wrapper.find('.f-notification__close').exists()).toBe(true) |
| 27 | + expect(wrapper.find('.f-notification').attributes('style')).toContain('top: 20px') |
| 28 | + expect(wrapper.find('.f-notification__title').text()).toBe(NAME) |
| 29 | + expect(wrapper.find('.f-notification__message').text()).toBe(AXIOM) |
| 30 | + }) |
| 31 | + |
| 32 | + test('should render when the `title` prop is VNode', () => { |
| 33 | + const wrapper = mount(FNotificationVue, { |
| 34 | + props: { |
| 35 | + message: h('span', { class: 'name' }, NAME) |
| 36 | + } |
| 37 | + }) |
| 38 | + expect(wrapper.find('.name').text()).toBe(NAME) |
| 39 | + }) |
| 40 | + |
| 41 | + test('should render when the `message` prop is VNode', () => { |
| 42 | + const wrapper = mount(FNotificationVue, { |
| 43 | + props: { |
| 44 | + message: h('span', { class: 'axiom' }, AXIOM) |
| 45 | + } |
| 46 | + }) |
| 47 | + expect(wrapper.find('.axiom').text()).toBe(AXIOM) |
| 48 | + }) |
| 49 | + |
| 50 | + test('type', () => { |
| 51 | + FIGHTING_TYPE.forEach((type) => { |
| 52 | + const wrapper = mount(FNotificationVue, { |
| 53 | + props: { |
| 54 | + type |
| 55 | + } |
| 56 | + }) |
| 57 | + expect(wrapper.find(`.f-notification__${type}`).exists()).toBe(true) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + test('round', () => { |
| 62 | + const wrapper = mount(FNotificationVue, { |
| 63 | + props: { |
| 64 | + round: true |
| 65 | + } |
| 66 | + }) |
| 67 | + expect(wrapper.find('.f-notification__round').exists()).toBe(true) |
| 68 | + }) |
| 69 | + |
| 70 | + test('icon', () => { |
| 71 | + const wrapper = mount(FNotificationVue, { |
| 72 | + props: { |
| 73 | + icon: markRaw(FIconInfo) |
| 74 | + } |
| 75 | + }) |
| 76 | + expect(wrapper.findComponent(FIconInfo).exists()).toBe(true) |
| 77 | + }) |
| 78 | + |
| 79 | + test('color', () => { |
| 80 | + const wrapper = mount(FNotificationVue, { |
| 81 | + props: { |
| 82 | + color: 'red' |
| 83 | + } |
| 84 | + }) |
| 85 | + expect(wrapper.find('.f-notification').attributes('style')).toContain('--notification-color: red') |
| 86 | + }) |
| 87 | + |
| 88 | + test('background', () => { |
| 89 | + const wrapper = mount(FNotificationVue, { |
| 90 | + props: { |
| 91 | + background: 'red' |
| 92 | + } |
| 93 | + }) |
| 94 | + expect(wrapper.find('.f-notification').attributes('style')).toContain('--notification-background: red') |
| 95 | + }) |
| 96 | + |
| 97 | + test('offset', () => { |
| 98 | + const wrapper = mount(FNotificationVue, { |
| 99 | + props: { |
| 100 | + offset: 40 |
| 101 | + } |
| 102 | + }) |
| 103 | + expect(wrapper.find('.f-notification').attributes('style')).toContain('top: 40px') |
| 104 | + }) |
| 105 | + |
| 106 | + test('zIndex', () => { |
| 107 | + const wrapper = mount(FNotificationVue, { |
| 108 | + props: { |
| 109 | + zIndex: 100 |
| 110 | + } |
| 111 | + }) |
| 112 | + expect(wrapper.find('.f-notification').attributes('style')).toContain('--notification-z-index: 100') |
| 113 | + }) |
| 114 | + |
| 115 | + test('placement', () => { |
| 116 | + const notificationPlacement: NotificationPlacement[] = ['top-left', 'top-right', 'bottom-left', 'bottom-right'] |
| 117 | + notificationPlacement.forEach((placement) => { |
| 118 | + const wrapper = mount(FNotificationVue, { |
| 119 | + props: { |
| 120 | + placement |
| 121 | + } |
| 122 | + }) |
| 123 | + expect(wrapper.find('.f-notification').classes()).toContain(`f-notification__${placement}`) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('close action', () => { |
| 128 | + test('notification should be closable when close button is clicked', async () => { |
| 129 | + const wrapper = mount(FNotificationVue, { |
| 130 | + props: { |
| 131 | + close: true |
| 132 | + } |
| 133 | + }) |
| 134 | + const closeBtn = wrapper.find('.f-notification__close') |
| 135 | + expect(closeBtn.exists()).toBe(true) |
| 136 | + await closeBtn.trigger('click') |
| 137 | + expect(wrapper.find('.f-notification').isVisible()).toBe(false) |
| 138 | + }) |
| 139 | + |
| 140 | + test('closeBtn is a icon', () => { |
| 141 | + const wrapper = mount(FNotificationVue, { |
| 142 | + props: { |
| 143 | + close: true, |
| 144 | + closeBtn: markRaw(FIconCross) |
| 145 | + } |
| 146 | + }) |
| 147 | + expect(wrapper.findComponent(FIconCross).exists()).toBe(true) |
| 148 | + }) |
| 149 | + |
| 150 | + test('closeBtn is a string type', () => { |
| 151 | + const wrapper = mount(FNotificationVue, { |
| 152 | + props: { |
| 153 | + close: true, |
| 154 | + closeBtn: 'close' |
| 155 | + } |
| 156 | + }) |
| 157 | + expect(wrapper.find('.f-notification__close').text()).toBe('close') |
| 158 | + }) |
| 159 | + |
| 160 | + test('onClose callback should be invoked when the close button is clicked', async () => { |
| 161 | + const onClose = vi.fn() |
| 162 | + const wrapper = mount(FNotificationVue, { |
| 163 | + props: { |
| 164 | + close: true, |
| 165 | + onClose |
| 166 | + } |
| 167 | + }) |
| 168 | + const closeBtn = wrapper.find('.f-notification__close') |
| 169 | + await closeBtn.trigger('click') |
| 170 | + expect(onClose).toHaveBeenCalled() |
| 171 | + }) |
| 172 | + |
| 173 | + test('notification should be closable after duration', async () => { |
| 174 | + vi.useFakeTimers() |
| 175 | + const wrapper = mount(FNotificationVue, { |
| 176 | + props: { duration: 1000 } |
| 177 | + }) |
| 178 | + const vm = wrapper.vm as unknown as NotificationInstance |
| 179 | + expect(vm.visible).toBe(true) |
| 180 | + vi.runAllTimers() |
| 181 | + expect(vm.visible).toBe(false) |
| 182 | + vi.useRealTimers() |
| 183 | + }) |
| 184 | + |
| 185 | + test('notification should not be closable when duration is set to 0', async () => { |
| 186 | + vi.useFakeTimers() |
| 187 | + const wrapper = mount(FNotificationVue, { |
| 188 | + props: { duration: 0 } |
| 189 | + }) |
| 190 | + const vm = wrapper.vm as unknown as NotificationInstance |
| 191 | + expect(vm.visible).toBe(true) |
| 192 | + vi.runAllTimers() |
| 193 | + expect(vm.visible).toBe(true) |
| 194 | + vi.useRealTimers() |
| 195 | + }) |
| 196 | + |
| 197 | + test('notification should prevent closure when mouse is hovered over it', async () => { |
| 198 | + vi.useFakeTimers() |
| 199 | + const wrapper = mount(FNotificationVue, { |
| 200 | + props: { duration: 1000 } |
| 201 | + }) |
| 202 | + const vm = wrapper.vm as unknown as NotificationInstance |
| 203 | + expect(vm.visible).toBe(true) |
| 204 | + await wrapper.find('.f-notification').trigger('mouseenter') |
| 205 | + vi.runAllTimers() |
| 206 | + expect(vm.visible).toBe(true) |
| 207 | + await wrapper.find('.f-notification').trigger('mouseleave') |
| 208 | + vi.runAllTimers() |
| 209 | + expect(vm.visible).toBe(false) |
| 210 | + vi.useRealTimers() |
| 211 | + }) |
17 | 212 | })
|
18 | 213 | })
|
0 commit comments