Skip to content

Commit

Permalink
change message component data to props & add test
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoyu committed Oct 17, 2017
1 parent 89bcfc4 commit a204e24
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 19 deletions.
80 changes: 80 additions & 0 deletions components/message/__test__/index.test.js
@@ -0,0 +1,80 @@
import message from '..'
import MessageBox from '../message-box'
import Message from '../message'
import { creatComponentVm, creatVueVm, simulateEvent, nextTick, renderVmString } from 'util.js'
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout))

describe('message', () => {
afterEach(() => {
message.destroy()
})

if('message-box should has prefixCls class', () => {
const vm = creatComponentVm(MessageBox, {
prefixCls: 'test-message-box'
})
expect(vm.$el.classList.contains('test-message-box')).toBeTruthy()
})

if('should has prefixCls class', () => {
const vm = creatComponentVm(Message, {
prefixCls: 'test-message'
})
expect(vm.$el.classList.contains('test-message-notice')).toBeTruthy()
expect(vm.$el.classList.contains('test-message-notice-content')).toBeTruthy()
})

if('should has type class', () => {
const vm = creatComponentVm(Message, {
type: 'success'
})
expect(vm.$el.classList.contains('ant-message-success')).toBeTruthy()
})

it('should be able to config top', () => {
message.config({
top: 100,
})
message.info('whatever')
expect(document.querySelectorAll('.ant-message')[0].style.top).toBe('100px')
})

it('should be able to config getContainer', () => {
message.config({
getContainer: () => {
const div = document.createElement('div')
div.className = 'custom-container'
document.body.appendChild(div)
return div
}
})
message.info('whatever')
expect(document.querySelectorAll('.custom-container').length).toBe(1)
})

it('should be able to hide manually', async () => {
const hide1 = message.info('whatever', 0)
const hide2 = message.info('whatever', 0)
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2)
await delay(100)
hide1()
setTimeout(() => {
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1)
}, 200)
await delay(100)
hide2()
setTimeout(() => {
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0)
}, 200)
})

it('should be able to destroy globally', () => {
message.info('whatever', 0)
message.info('whatever', 0)
expect(document.querySelectorAll('.ant-message').length).toBe(1)
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2)
message.destroy()
expect(document.querySelectorAll('.ant-message').length).toBe(0)
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0)
})
})
6 changes: 3 additions & 3 deletions components/message/index.js
Expand Up @@ -16,9 +16,9 @@ let getContainer

function getBoxEl () {
boxInstance = boxInstance || new MessageBoxConstructor({
data: {
propsData: {
prefixCls,
styles: { top: defaultTop }
styles: { top: `${defaultTop}px` }
}
})
boxInstance.vm = boxInstance.$mount()
Expand All @@ -43,7 +43,7 @@ function notice (content, duration, type, onClose) {
}
let id = `${prefixCls}${seed++}`
let messageInstance = new MessageConstructor({
data: options
propsData: options
})
messageInstance.id = id
messageInstance.vm = messageInstance.$mount()
Expand Down
12 changes: 8 additions & 4 deletions components/message/message-box.vue
Expand Up @@ -5,10 +5,14 @@
<script>
export default {
name: 'messageBox',
data () {
return {
prefixCls: 'ant-message-box',
styles: {}
props: {
prefixCls: {
type: String,
default: 'ant-message-box'
},
styles: {
type: Object,
default: {}
}
}
}
Expand Down
36 changes: 26 additions & 10 deletions components/message/message.vue
Expand Up @@ -12,8 +12,8 @@
</template>

<script>
import AtuTransition from '@/transition'
import Icon from '@/icon'
import AtuTransition from '../transition'
import Icon from '../icon'
const IconTypes = {
info: 'info-circle',
success: 'check-circle',
Expand All @@ -24,24 +24,40 @@ const IconTypes = {
export default {
name: 'message',
props: {
content: {
type: String,
default: ''
},
duration: {
type: Number,
default: 3
},
onClose: {
type: Function
},
prefixCls: {
type: String,
default: 'ant-message'
},
type: {
type: String,
default: 'info'
}
},
data () {
return {
prefixCls: 'ant-message',
visible: false,
content: '',
duration: 3,
type: 'info',
onClose: null,
closed: false,
timer: null
}
},
computed: {
iconType () {
return IconTypes[this.type]
},
durationMS () {
return this.duration * 1000
},
iconType () {
return IconTypes[this.type]
}
},
components: {
Expand Down
4 changes: 2 additions & 2 deletions tests/util.js
Expand Up @@ -14,7 +14,7 @@ export const creatComponentVm = (component, propsData) => {

/**
* 模仿事件,多用于测试组件的点击等事件。
* @param {HTMLDOM} dom 原生dom 例如 vm.$el
* @param {HTMLDOM} dom 原生dom 例如 vm.$el
* @param {*} event 需要模仿的事件
*/
export const simulateEvent = (dom, event) => {
Expand Down Expand Up @@ -53,7 +53,7 @@ export const creatVueVm = (options) => {
/**
* Vue 提供的 nextTick 方法
* 一些依赖 DOM 更新结果的断言必须在nextTick中执行
* @param {*} callback
* @param {*} callback
*/
export const nextTick = (callback) => {
Vue.nextTick(() => {
Expand Down

0 comments on commit a204e24

Please sign in to comment.