Commit 23e20abf6c50590fd516074b987522ef687e9c36
1 parent
4991f4ec
DRY message/index.js and add unit tests
Showing
2 changed files
with
85 additions
and
40 deletions
Show diff stats
src/components/message/index.js
| @@ -4,8 +4,11 @@ const prefixCls = 'ivu-message'; | @@ -4,8 +4,11 @@ const prefixCls = 'ivu-message'; | ||
| 4 | const iconPrefixCls = 'ivu-icon'; | 4 | const iconPrefixCls = 'ivu-icon'; |
| 5 | const prefixKey = 'ivu_message_key_'; | 5 | const prefixKey = 'ivu_message_key_'; |
| 6 | 6 | ||
| 7 | -let defaultDuration = 1.5; | ||
| 8 | -let top; | 7 | +const defaults = { |
| 8 | + top: 24, | ||
| 9 | + duration: 1.5 | ||
| 10 | +}; | ||
| 11 | + | ||
| 9 | let messageInstance; | 12 | let messageInstance; |
| 10 | let name = 1; | 13 | let name = 1; |
| 11 | 14 | ||
| @@ -21,14 +24,14 @@ function getMessageInstance () { | @@ -21,14 +24,14 @@ function getMessageInstance () { | ||
| 21 | messageInstance = messageInstance || Notification.newInstance({ | 24 | messageInstance = messageInstance || Notification.newInstance({ |
| 22 | prefixCls: prefixCls, | 25 | prefixCls: prefixCls, |
| 23 | styles: { | 26 | styles: { |
| 24 | - top: `${top}px` | 27 | + top: `${defaults.top}px` |
| 25 | } | 28 | } |
| 26 | }); | 29 | }); |
| 27 | 30 | ||
| 28 | return messageInstance; | 31 | return messageInstance; |
| 29 | } | 32 | } |
| 30 | 33 | ||
| 31 | -function notice (content = '', duration = defaultDuration, type, onClose = function () {}, closable = false) { | 34 | +function notice (content = '', duration = defaults.duration, type, onClose = function () {}, closable = false) { |
| 32 | const iconType = iconTypes[type]; | 35 | const iconType = iconTypes[type]; |
| 33 | 36 | ||
| 34 | // if loading | 37 | // if loading |
| @@ -66,56 +69,34 @@ export default { | @@ -66,56 +69,34 @@ export default { | ||
| 66 | name: 'Message', | 69 | name: 'Message', |
| 67 | 70 | ||
| 68 | info (options) { | 71 | info (options) { |
| 69 | - const type = typeof options; | ||
| 70 | - if (type === 'string') { | ||
| 71 | - options = { | ||
| 72 | - content: options | ||
| 73 | - }; | ||
| 74 | - } | ||
| 75 | - return notice(options.content, options.duration, 'info', options.onClose, options.closable); | 72 | + return this.message('info', options); |
| 76 | }, | 73 | }, |
| 77 | success (options) { | 74 | success (options) { |
| 78 | - const type = typeof options; | ||
| 79 | - if (type === 'string') { | ||
| 80 | - options = { | ||
| 81 | - content: options | ||
| 82 | - }; | ||
| 83 | - } | ||
| 84 | - return notice(options.content, options.duration, 'success', options.onClose, options.closable); | 75 | + return this.message('success', options); |
| 85 | }, | 76 | }, |
| 86 | warning (options) { | 77 | warning (options) { |
| 87 | - const type = typeof options; | ||
| 88 | - if (type === 'string') { | ||
| 89 | - options = { | ||
| 90 | - content: options | ||
| 91 | - }; | ||
| 92 | - } | ||
| 93 | - return notice(options.content, options.duration, 'warning', options.onClose, options.closable); | 78 | + return this.message('warning', options); |
| 94 | }, | 79 | }, |
| 95 | error (options) { | 80 | error (options) { |
| 96 | - const type = typeof options; | ||
| 97 | - if (type === 'string') { | ||
| 98 | - options = { | ||
| 99 | - content: options | ||
| 100 | - }; | ||
| 101 | - } | ||
| 102 | - return notice(options.content, options.duration, 'error', options.onClose, options.closable); | 81 | + return this.message('error', options); |
| 103 | }, | 82 | }, |
| 104 | loading (options) { | 83 | loading (options) { |
| 105 | - const type = typeof options; | ||
| 106 | - if (type === 'string') { | 84 | + return this.message('loading', options); |
| 85 | + }, | ||
| 86 | + message(type, options){ | ||
| 87 | + if (typeof options === 'string') { | ||
| 107 | options = { | 88 | options = { |
| 108 | content: options | 89 | content: options |
| 109 | }; | 90 | }; |
| 110 | } | 91 | } |
| 111 | - return notice(options.content, options.duration, 'loading', options.onClose, options.closable); | 92 | + return notice(options.content, options.duration, type, options.onClose, options.closable); |
| 112 | }, | 93 | }, |
| 113 | config (options) { | 94 | config (options) { |
| 114 | - if (options.top) { | ||
| 115 | - top = options.top; | 95 | + if (options.top || options.top === 0) { |
| 96 | + defaults.top = options.top; | ||
| 116 | } | 97 | } |
| 117 | - if (options.duration) { | ||
| 118 | - defaultDuration = options.duration; | 98 | + if (options.duration || options.duration === 0) { |
| 99 | + defaults.duration = options.duration; | ||
| 119 | } | 100 | } |
| 120 | }, | 101 | }, |
| 121 | destroy () { | 102 | destroy () { |
| @@ -123,4 +104,4 @@ export default { | @@ -123,4 +104,4 @@ export default { | ||
| 123 | messageInstance = null; | 104 | messageInstance = null; |
| 124 | instance.destroy('ivu-message'); | 105 | instance.destroy('ivu-message'); |
| 125 | } | 106 | } |
| 126 | -}; | ||
| 127 | \ No newline at end of file | 107 | \ No newline at end of file |
| 108 | +}; |
| 1 | +import {createVue, destroyVM, waitForIt} from '../util'; | ||
| 2 | + | ||
| 3 | +describe('Message.vue', () => { | ||
| 4 | + let vm; | ||
| 5 | + afterEach(() => { | ||
| 6 | + destroyVM(vm); | ||
| 7 | + }); | ||
| 8 | + | ||
| 9 | + it('should open a info message by default', done => { | ||
| 10 | + vm = createVue({render: () => {}}); | ||
| 11 | + const testMessage = 'Hello world!'; | ||
| 12 | + let messageContainer = null; | ||
| 13 | + vm.$Message.info({ | ||
| 14 | + content: testMessage, | ||
| 15 | + duration: 200 // too long so we can test | ||
| 16 | + }); | ||
| 17 | + | ||
| 18 | + const selector = '.ivu-message-notice-content-text .ivu-message-info'; | ||
| 19 | + const checkMessageOpens = () => (messageContainer = document.querySelector(selector)); | ||
| 20 | + | ||
| 21 | + waitForIt(checkMessageOpens, function() { | ||
| 22 | + expect(messageContainer.textContent.trim()).to.equal(testMessage); | ||
| 23 | + messageContainer.parentElement.removeChild(messageContainer); | ||
| 24 | + done(); | ||
| 25 | + }); | ||
| 26 | + }); | ||
| 27 | + | ||
| 28 | + it('should open specific messages of different types', function(done) { | ||
| 29 | + vm = createVue({render: () => {}}); | ||
| 30 | + const testMessage = type => `Hello world! this is a ${type} message`; | ||
| 31 | + const tests = ['info', 'success', 'warning', 'error', 'loading'].reduce((tests, type) => { | ||
| 32 | + return tests.concat({ | ||
| 33 | + type: type, | ||
| 34 | + message: testMessage(type), | ||
| 35 | + class: 'ivu-message-' + type | ||
| 36 | + }); | ||
| 37 | + }, []); | ||
| 38 | + let domElements = []; | ||
| 39 | + | ||
| 40 | + for (const {type, message} of tests) { | ||
| 41 | + vm.$Message[type]({ | ||
| 42 | + content: message, | ||
| 43 | + duration: 10 // long so we can test | ||
| 44 | + }); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + const checkAllMessageOpens = () => { | ||
| 48 | + domElements = document.querySelectorAll('.ivu-message-custom-content'); | ||
| 49 | + return domElements.length == tests.length && domElements; | ||
| 50 | + }; | ||
| 51 | + | ||
| 52 | + waitForIt(checkAllMessageOpens, function() { | ||
| 53 | + const verify = {}; | ||
| 54 | + domElements.forEach(el => { | ||
| 55 | + const message = el.textContent.trim(); | ||
| 56 | + const test = tests.find(test => test.message == message); | ||
| 57 | + verify[test.type] = true; | ||
| 58 | + expect(el.classList.contains(test.class)).to.equal(true); | ||
| 59 | + }); | ||
| 60 | + expect(Object.keys(verify).length).to.equal(tests.length); | ||
| 61 | + done(); | ||
| 62 | + }); | ||
| 63 | + }); | ||
| 64 | +}); |