|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import { asyncExpect } from '@/tests/utils' |
| 3 | +import message from '..' |
| 4 | + |
| 5 | +describe('message', () => { |
| 6 | + beforeEach(() => { |
| 7 | + document.body.outerHTML = '' |
| 8 | + }) |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + message.destroy() |
| 12 | + }) |
| 13 | + |
| 14 | + it('should be able to config top', async () => { |
| 15 | + message.config({ |
| 16 | + top: '100px', |
| 17 | + }) |
| 18 | + message.info('whatever') |
| 19 | + await asyncExpect(() => { |
| 20 | + expect(document.querySelectorAll('.ant-message')[0].style.top).toBe('100px') |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should be able to config getContainer', () => { |
| 25 | + message.config({ |
| 26 | + getContainer: () => { |
| 27 | + const div = document.createElement('div') |
| 28 | + div.className = 'custom-container' |
| 29 | + document.body.appendChild(div) |
| 30 | + return div |
| 31 | + }, |
| 32 | + }) |
| 33 | + message.info('whatever') |
| 34 | + expect(document.querySelectorAll('.custom-container').length).toBe(1) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should be able to hide manually', async () => { |
| 38 | + const hide1 = message.info('whatever', 0) |
| 39 | + const hide2 = message.info('whatever', 0) |
| 40 | + await asyncExpect(() => { |
| 41 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(2) |
| 42 | + hide1() |
| 43 | + }, 0) |
| 44 | + await asyncExpect(() => { |
| 45 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(1) |
| 46 | + hide2() |
| 47 | + }, 0) |
| 48 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(0) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should be able to destroy globally', async () => { |
| 52 | + await asyncExpect(() => { |
| 53 | + message.info('whatever', 0) |
| 54 | + }) |
| 55 | + await asyncExpect(() => { |
| 56 | + message.info('whatever', 0) |
| 57 | + }) |
| 58 | + await asyncExpect(() => { |
| 59 | + expect(document.querySelectorAll('.ant-message').length).toBe(1) |
| 60 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(2) |
| 61 | + }) |
| 62 | + await asyncExpect(() => { |
| 63 | + message.destroy() |
| 64 | + }) |
| 65 | + await asyncExpect(() => { |
| 66 | + expect(document.querySelectorAll('.ant-message').length).toBe(0) |
| 67 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(0) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should not need to use duration argument when using the onClose arguments', () => { |
| 72 | + message.info('whatever', () => {}) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should have the default duration when using the onClose arguments', (done) => { |
| 76 | + const defaultDuration = 3 |
| 77 | + const now = Date.now() |
| 78 | + message.info('whatever', () => { |
| 79 | + // calculate the approximately duration value |
| 80 | + const aboutDuration = parseInt((Date.now() - now) / 1000, 10) |
| 81 | + expect(aboutDuration).toBe(defaultDuration) |
| 82 | + done() |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + // https:// github.com/ant-design/ant-design/issues/8201 |
| 87 | + it('should hide message correctly', async () => { |
| 88 | + let hide |
| 89 | + const Test = { |
| 90 | + mounted () { |
| 91 | + hide = message.loading('Action in progress..', 0) |
| 92 | + }, |
| 93 | + render () { |
| 94 | + return <div>test</div> |
| 95 | + }, |
| 96 | + } |
| 97 | + mount(Test, { sync: false }) |
| 98 | + await asyncExpect(() => { |
| 99 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(1) |
| 100 | + hide() |
| 101 | + }, 0) |
| 102 | + await asyncExpect(() => { |
| 103 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(0) |
| 104 | + }, 0) |
| 105 | + }) |
| 106 | + |
| 107 | + // https://github.com/ant-design/ant-design/issues/8201 |
| 108 | + it('should destroy messages correctly', async () => { |
| 109 | + // eslint-disable-next-line |
| 110 | + const Test = { |
| 111 | + mounted () { |
| 112 | + message.loading('Action in progress1..', 0) |
| 113 | + message.loading('Action in progress2..', 0) |
| 114 | + setTimeout(() => message.destroy(), 1000) |
| 115 | + }, |
| 116 | + render () { |
| 117 | + return <div>test</div> |
| 118 | + }, |
| 119 | + } |
| 120 | + mount(Test, { sync: false }) |
| 121 | + |
| 122 | + await asyncExpect(() => { |
| 123 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(2) |
| 124 | + }, 0) |
| 125 | + await asyncExpect(() => { |
| 126 | + expect(document.querySelectorAll('.ant-message-notice').length).toBe(0) |
| 127 | + }, 1500) |
| 128 | + }) |
| 129 | +}) |
0 commit comments