Skip to content

Commit 2f2c7f9

Browse files
committed
test: add notification test
1 parent d7b144b commit 2f2c7f9

File tree

3 files changed

+258
-2
lines changed

3 files changed

+258
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { asyncExpect } from '@/tests/utils'
2+
import notification from '..'
3+
4+
describe('notification', () => {
5+
beforeEach(() => {
6+
document.body.outerHTML = ''
7+
})
8+
9+
afterEach(() => {
10+
notification.destroy()
11+
})
12+
13+
it('should be able to hide manually', async () => {
14+
notification.open({
15+
message: 'Notification Title',
16+
duration: 0,
17+
key: '1',
18+
})
19+
await asyncExpect(() => {
20+
notification.open({
21+
message: 'Notification Title',
22+
duration: 0,
23+
key: '2',
24+
})
25+
})
26+
await asyncExpect(() => {
27+
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(2)
28+
notification.close('1')
29+
}, 0)
30+
await asyncExpect(() => {
31+
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(1)
32+
notification.close('2')
33+
}, 0)
34+
await asyncExpect(() => {
35+
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(0)
36+
}, 0)
37+
})
38+
39+
it('should be able to destroy globally', async () => {
40+
notification.open({
41+
message: 'Notification Title',
42+
duration: 0,
43+
})
44+
await asyncExpect(() => {
45+
notification.open({
46+
message: 'Notification Title',
47+
duration: 0,
48+
})
49+
})
50+
await asyncExpect(() => {
51+
expect(document.querySelectorAll('.ant-notification').length).toBe(1)
52+
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(2)
53+
notification.destroy()
54+
}, 0)
55+
await asyncExpect(() => {
56+
expect(document.querySelectorAll('.ant-notification').length).toBe(0)
57+
expect(document.querySelectorAll('.ant-notification-notice').length).toBe(0)
58+
}, 0)
59+
})
60+
61+
it('should be able to destroy after config', () => {
62+
notification.config({
63+
bottom: 100,
64+
})
65+
notification.destroy()
66+
})
67+
68+
it('should be able to open with icon', (done) => {
69+
const openNotificationWithIcon = async (type) => {
70+
const iconPrefix = '.ant-notification-notice-icon'
71+
notification[type]({
72+
message: 'Notification Title',
73+
duration: 0,
74+
description: 'This is the content of the notification.',
75+
})
76+
await asyncExpect(() => {
77+
expect(document.querySelectorAll(`${iconPrefix}-${type}`).length).toBe(1)
78+
}, 0)
79+
};
80+
['success', 'info', 'warning', 'error'].forEach((type) => {
81+
openNotificationWithIcon(type)
82+
})
83+
done()
84+
})
85+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { asyncExpect } from '@/tests/utils'
2+
import notification from '..'
3+
4+
describe('Notification.placement', () => {
5+
afterEach(() => notification.destroy())
6+
7+
function $$ (className) {
8+
return document.body.querySelectorAll(className)
9+
}
10+
11+
function getStyle (el, prop, getComputedStyle, style) {
12+
getComputedStyle = getComputedStyle || window.getComputedStyle
13+
style = getComputedStyle ? getComputedStyle(el) : el.currentStyle
14+
15+
// If a css property's value is `auto`, it will return an empty string.
16+
return prop ? style[prop] : style
17+
}
18+
19+
function open (args) {
20+
notification.open({
21+
message: 'Notification Title',
22+
description: 'This is the content of the notification.',
23+
...args,
24+
})
25+
}
26+
27+
function config (args) {
28+
notification.config({
29+
...args,
30+
})
31+
open()
32+
}
33+
34+
it('change notification placement by `open` method', async () => {
35+
const defaultTop = '24px'
36+
const defaultBottom = '24px'
37+
let style
38+
39+
// topLeft
40+
open({
41+
placement: 'topLeft',
42+
})
43+
await asyncExpect(() => {
44+
style = getStyle($$('.ant-notification-topLeft')[0])
45+
expect(style.top).toBe(defaultTop)
46+
expect(style.left).toBe('0px')
47+
expect(style.bottom).toBe('')
48+
})
49+
open({
50+
placement: 'topLeft',
51+
})
52+
await asyncExpect(() => {
53+
expect($$('.ant-notification-topLeft').length).toBe(1)
54+
})
55+
// topRight
56+
open({
57+
placement: 'topRight',
58+
})
59+
await asyncExpect(() => {
60+
style = getStyle($$('.ant-notification-topRight')[0])
61+
expect(style.top).toBe(defaultTop)
62+
expect(style.right).toBe('0px')
63+
expect(style.bottom).toBe('')
64+
})
65+
open({
66+
placement: 'topRight',
67+
})
68+
await asyncExpect(() => {
69+
expect($$('.ant-notification-topRight').length).toBe(1)
70+
})
71+
// bottomRight
72+
open({
73+
placement: 'bottomRight',
74+
})
75+
await asyncExpect(() => {
76+
style = getStyle($$('.ant-notification-bottomRight')[0])
77+
expect(style.top).toBe('')
78+
expect(style.right).toBe('0px')
79+
expect(style.bottom).toBe(defaultBottom)
80+
})
81+
open({
82+
placement: 'bottomRight',
83+
})
84+
await asyncExpect(() => {
85+
expect($$('.ant-notification-bottomRight').length).toBe(1)
86+
})
87+
// bottomLeft
88+
open({
89+
placement: 'bottomLeft',
90+
})
91+
await asyncExpect(() => {
92+
style = getStyle($$('.ant-notification-bottomLeft')[0])
93+
expect(style.top).toBe('')
94+
expect(style.left).toBe('0px')
95+
expect(style.bottom).toBe(defaultBottom)
96+
})
97+
open({
98+
placement: 'bottomLeft',
99+
})
100+
await asyncExpect(() => {
101+
expect($$('.ant-notification-bottomLeft').length).toBe(1)
102+
})
103+
await asyncExpect(() => {
104+
105+
})
106+
await asyncExpect(() => {
107+
108+
})
109+
})
110+
111+
it('change notification placement by `config` method', () => {
112+
let style
113+
114+
// topLeft
115+
config({
116+
placement: 'topLeft',
117+
top: '50px',
118+
bottom: '50px',
119+
})
120+
style = getStyle($$('.ant-notification-topLeft')[0])
121+
expect(style.top).toBe('50px')
122+
expect(style.left).toBe('0px')
123+
expect(style.bottom).toBe('')
124+
125+
// topRight
126+
config({
127+
placement: 'topRight',
128+
top: '100px',
129+
bottom: '50px',
130+
})
131+
style = getStyle($$('.ant-notification-topRight')[0])
132+
expect(style.top).toBe('100px')
133+
expect(style.right).toBe('0px')
134+
expect(style.bottom).toBe('')
135+
136+
// bottomRight
137+
config({
138+
placement: 'bottomRight',
139+
top: '50px',
140+
bottom: '100px',
141+
})
142+
style = getStyle($$('.ant-notification-bottomRight')[0])
143+
expect(style.top).toBe('')
144+
expect(style.right).toBe('0px')
145+
expect(style.bottom).toBe('100px')
146+
147+
// bottomLeft
148+
config({
149+
placement: 'bottomLeft',
150+
top: 100,
151+
bottom: 50,
152+
})
153+
style = getStyle($$('.ant-notification-bottomLeft')[0])
154+
expect(style.top).toBe('')
155+
expect(style.left).toBe('0px')
156+
expect(style.bottom).toBe('50px')
157+
})
158+
it('change notification mountNode by `config` method', () => {
159+
const $container = document.createElement('div')
160+
document.body.appendChild($container)
161+
config({
162+
top: '50px',
163+
bottom: '100px',
164+
getContainer () {
165+
return $container
166+
},
167+
})
168+
expect($container.querySelector('.ant-notification')).not.toBe(null)
169+
$container.remove()
170+
})
171+
})

components/notification/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ function setNotificationConfig (options) {
2828
defaultPlacement = placement
2929
}
3030
if (bottom !== undefined) {
31-
defaultBottom = bottom
31+
defaultBottom = typeof bottom === 'number' ? `${bottom}px` : bottom
3232
}
3333
if (top !== undefined) {
34-
defaultTop = top
34+
defaultTop = typeof top === 'number' ? `${top}px` : top
3535
}
3636
if (getContainer !== undefined) {
3737
defaultGetContainer = getContainer

0 commit comments

Comments
 (0)