|
| 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 | +}) |
0 commit comments