Skip to content

Commit 96200aa

Browse files
committed
refactor: CToast component refactor, functions simplification
1 parent 11025b2 commit 96200aa

File tree

2 files changed

+50
-59
lines changed

2 files changed

+50
-59
lines changed

src/components/toast/CToast.vue

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ export default {
4949
data () {
5050
return {
5151
isShowed: this.show,
52-
hidding: false,
53-
timeout: null,
54-
hiddingTimeout: null
52+
hideTimeout: null,
53+
closeTimeout: null
5554
}
5655
},
5756
watch: {
@@ -85,55 +84,40 @@ export default {
8584
this.$nextTick(() => {
8685
if (this.props.autohide) {
8786
this.setAutohide()
88-
} else if (this.hidding === true) {
89-
this.finishHidding()
9087
}
9188
})
9289
},
93-
close (restoreOnHover = false) {
94-
if (this.isShowed === false) {
95-
return
96-
}
97-
90+
close () {
9891
this.isShowed = false
9992
this.$emit('update:show', false)
100-
this.$el.removeEventListener('mouseout', this.onHoverOut)
10193
this.$el.removeEventListener('mouseover', this.onHover)
102-
103-
if (this.props.fade) {
104-
this.setHiddingMode(restoreOnHover)
105-
}
94+
this.$el.removeEventListener('mouseover', this.restoreHiddingToast)
95+
this.$el.removeEventListener('mouseout', this.onHoverOut)
10696
},
107-
setHiddingMode (restoreOnHover) {
108-
this.hidding = true
109-
if (restoreOnHover) {
110-
this.$el.addEventListener('mouseover', this.restoreHiddingToast)
111-
}
112-
clearTimeout(this.timeout)
113-
this.hiddingTimeout = setTimeout(this.finishHidding, 1500)
97+
setHiddingMode () {
98+
this.isShowed = false
99+
this.$el.removeEventListener('mouseover', this.onHover)
100+
this.closeTimeout = setTimeout(() => this.close(), 1500)
101+
this.$el.addEventListener('mouseover', this.restoreHiddingToast)
114102
},
115-
finishHidding () {
116-
this.$el.removeEventListener('mouseover', this.restoreHiddingToast)
117-
this.hidding = false
118-
clearTimeout(this.hiddingTimeout)
103+
restoreHiddingToast () {
104+
clearTimeout(this.closeTimeout)
105+
this.display()
119106
},
120107
onHover () {
121108
this.$el.removeEventListener('mouseover', this.onHover)
122-
clearTimeout(this.timeout)
109+
clearTimeout(this.hideTimeout)
123110
this.$el.addEventListener('mouseout', this.onHoverOut)
124111
},
125112
onHoverOut () {
126113
this.$el.removeEventListener('mouseout', this.onHoverOut)
127114
this.setAutohide()
128115
},
129116
setAutohide () {
130-
this.timeout = setTimeout(() => {
131-
this.close(true)
117+
this.hideTimeout = setTimeout(() => {
118+
this.setHiddingMode()
132119
}, this.props.autohide)
133120
this.$el.addEventListener('mouseover', this.onHover)
134-
},
135-
restoreHiddingToast () {
136-
this.display()
137121
}
138122
}
139123
}

src/components/toast/tests/CToast.spec.js

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ const customWrapper = mount(Component, {
1515
show: true,
1616
position: 'bottom-center',
1717
header: 'title',
18-
autohide: 10,
19-
closeButton: true
18+
autohide: 1000
2019
},
2120
slots: {
2221
default: 'CToast body'
@@ -33,35 +32,43 @@ describe(ComponentName, () => {
3332
it('renders correctly custom wrapper', () => {
3433
expect(customWrapper.element).toMatchSnapshot()
3534
})
36-
it('closes by watcher correctly', () => {
37-
wrapper.setProps({ show: false })
38-
wrapper.vm.$nextTick(() => expect(wrapper.vm.isShowed).toBe(false))
39-
})
35+
4036
it('closes by watcher correctly', () => {
4137
wrapper.setProps({ show: false })
4238
expect(wrapper.vm.isShowed).toBe(false)
4339
})
44-
it('closes by close button correctly', () => {
45-
customWrapper.find('.close').trigger('click')
46-
expect(customWrapper.vm.isShowed).toBe(false)
47-
})
48-
// it('autohiding works correctly', (done) => {
49-
// customWrapper.vm.display()
50-
// setTimeout(() => {
51-
// customWrapper.trigger('mouseover')
52-
// customWrapper.trigger('mouseout')
53-
// }, 9)
40+
// it('closes by close button correctly', () => {
41+
// wrapper.find('.close').trigger('click')
42+
// expect(wrapper.vm.isShowed).toBe(false)
43+
// })
44+
it('autohiding works correctly', (done) => {
45+
setTimeout(() => {
46+
customWrapper.find('div').trigger('mouseover')
47+
customWrapper.find('div').trigger('mouseout')
48+
}, 900)
5449

55-
// setTimeout(() => {
56-
// expect(customWrapper.vm.isShowed).toBe(true)
57-
// }, 18)
50+
setTimeout(() => {
51+
expect(customWrapper.vm.isShowed).toBe(true)
52+
}, 1100)
5853

59-
// setTimeout(() => {
60-
// expect(customWrapper.vm.isShowed).toBe(false)
61-
// customWrapper.trigger('mouseover')
62-
// customWrapper.trigger('mouseout')
63-
// expect(customWrapper.vm.isShowed).toBe(true)
64-
// done()
65-
// }, 25)
66-
// })
54+
setTimeout(() => {
55+
expect(customWrapper.vm.closeTimeout).toBeTruthy()
56+
expect(customWrapper.vm.isShowed).toBe(false)
57+
customWrapper.vm.restoreHiddingToast()
58+
expect(customWrapper.vm.isShowed).toBe(true)
59+
}, 2000)
60+
61+
setTimeout(() => {
62+
expect(customWrapper.emitted()['update:show']).toBeTruthy()
63+
done()
64+
}, 4600)
65+
66+
67+
})
68+
// it('closes by watcher correctly', () => {
69+
// wrapper.vm.$nextTick(() => {
70+
// wrapper.find('.close').trigger('click')
71+
// expect(wrapper.vm.isShowed).toBe(false)
72+
// })
73+
// })
6774
})

0 commit comments

Comments
 (0)