bv-toast.spec.js
3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { createWrapper, mount } from '@vue/test-utils'
import { Vue } from '../../../vue'
import { waitNT, waitRAF } from '../../../../tests/utils'
import { ToastPlugin } from '../index'
Vue.use(ToastPlugin)
describe('$bvToast', () => {
it('$bvToast.show() and $bvToast.hide() works', async () => {
const App = {
render(h) {
return h(
'b-toast',
{
props: {
id: 'test1',
static: true,
visible: false,
noAutoHide: true
}
},
'content'
)
}
}
const wrapper = mount(App, {
attachTo: document.body
})
expect(wrapper.vm).toBeDefined()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.vm.$bvToast).toBeDefined()
expect(wrapper.vm.$bvToast.show).toBeDefined()
expect(typeof wrapper.vm.$bvToast.show).toEqual('function')
expect(wrapper.vm.$bvToast.hide).toBeDefined()
expect(typeof wrapper.vm.$bvToast.hide).toEqual('function')
expect(wrapper.find('.toast').exists()).toBe(false)
wrapper.vm.$bvToast.show('test1')
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.find('.toast').exists()).toBe(true)
wrapper.vm.$bvToast.hide('test1')
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.find('.toast').exists()).toBe(false)
wrapper.destroy()
})
it('$bvModal.toast() works', async () => {
const App = {
render(h) {
return h('div', 'app')
}
}
const wrapper = mount(App, {
attachTo: document.body
})
expect(wrapper.vm).toBeDefined()
// `$bvModal.toast`
expect(wrapper.vm.$bvToast).toBeDefined()
const bvToast = wrapper.vm.$bvToast
expect(bvToast.toast).toBeDefined()
expect(typeof bvToast.toast).toEqual('function')
// Trigger a toast
bvToast.toast('message', {
id: 'test2',
title: 'title',
noAutoHide: true
})
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
// Find the toast
const toast = document.querySelector('#test2')
expect(toast).toBeDefined()
expect(toast).not.toEqual(null)
const $toast = createWrapper(toast)
expect($toast.element.tagName).toBe('DIV')
// Find header
expect($toast.find('.toast-header').exists()).toBe(true)
expect($toast.find('.toast-header').text()).toContain('title')
// Find body
expect($toast.find('.toast-body').exists()).toBe(true)
expect($toast.find('.toast-body').text()).toContain('message')
// Find the Close button and click it
expect($toast.findAll('button').length).toBe(1)
const $button = $toast.find('button')
expect($button.classes()).toContain('close')
await $button.trigger('click')
await waitRAF()
await waitRAF()
await waitRAF()
await waitRAF()
// Toast should be gone from DOM
expect(document.querySelector('#test2')).toBe(null)
})
})