11import { enableAutoUnmount , mount } from '@vue/test-utils'
22import { afterEach , describe , expect , it } from 'vitest'
33import BAlert from './BAlert.vue'
4+ import BCloseButton from './BButton/BCloseButton.vue'
45
56describe ( 'alert' , ( ) => {
67 enableAutoUnmount ( afterEach )
@@ -95,65 +96,127 @@ describe('alert', () => {
9596 expect ( wrapper . text ( ) ) . toBe ( 'foobar' )
9697 } )
9798
98- it ( 'has button child if prop dismissible' , ( ) => {
99+ it ( 'does not have BCloseButton when prop dismissible is false ' , ( ) => {
99100 const wrapper = mount ( BAlert , {
100- props : { modelValue : true , dismissible : true } ,
101+ props : { dismissible : false } ,
101102 } )
102- const $button = wrapper . find ( 'button' )
103- expect ( $button . exists ( ) ) . toBe ( true )
103+ const $closebutton = wrapper . findComponent ( BCloseButton )
104+ expect ( $closebutton . exists ( ) ) . toBe ( false )
104105 } )
105106
106- it ( 'does not have button child if prop dismissible false ' , ( ) => {
107+ it ( 'button is BCloseButton when not slot dismissible' , ( ) => {
107108 const wrapper = mount ( BAlert , {
108- props : { modelValue : true , dismissible : false } ,
109+ props : { dismissible : true , modelValue : true } ,
109110 } )
110- const $button = wrapper . find ( 'button' )
111- expect ( $button . exists ( ) ) . toBe ( false )
111+ const $closebutton = wrapper . findComponent ( BCloseButton )
112+ expect ( $closebutton . exists ( ) ) . toBe ( true )
112113 } )
113114
114- it ( 'button child has static attribute type button' , ( ) => {
115+ it ( 'BCloseButton is given prop ariaLabel to be dismissLabel' , ( ) => {
116+ const wrapper = mount ( BAlert , {
117+ props : { dismissible : true , modelValue : true , dismissLabel : 'foobar' } ,
118+ } )
119+ const $closebutton = wrapper . getComponent ( BCloseButton )
120+ expect ( $closebutton . props ( 'ariaLabel' ) ) . toBe ( 'foobar' )
121+ } )
122+
123+ it ( 'BCloseButton has attr data-bs-dismiss to be alert' , ( ) => {
124+ const wrapper = mount ( BAlert , {
125+ props : { dismissible : true , modelValue : true } ,
126+ } )
127+ const $closebutton = wrapper . getComponent ( BCloseButton )
128+ expect ( $closebutton . attributes ( 'data-bs-dismiss' ) ) . toBe ( 'alert' )
129+ } )
130+
131+ it ( 'button child on click emits update:modelValue' , async ( ) => {
115132 const wrapper = mount ( BAlert , {
116133 props : { modelValue : true , dismissible : true } ,
117134 } )
118135 const $button = wrapper . get ( 'button' )
119- expect ( $button . attributes ( 'type' ) ) . toBe ( 'button' )
136+ await $button . trigger ( 'click' )
137+ expect ( wrapper . emitted ( ) ) . toHaveProperty ( 'update:modelValue' )
120138 } )
121139
122- it ( 'button child has static class btn-close' , ( ) => {
140+ it ( 'button child on click emits dismissed' , async ( ) => {
123141 const wrapper = mount ( BAlert , {
124142 props : { modelValue : true , dismissible : true } ,
125143 } )
126144 const $button = wrapper . get ( 'button' )
127- expect ( $button . classes ( ) ) . toContain ( 'btn-close' )
145+ await $button . trigger ( 'click' )
146+ expect ( wrapper . emitted ( ) ) . toHaveProperty ( 'dismissed' )
128147 } )
129148
130- it ( 'button child has static attribute data-bs-dismiss alert' , ( ) => {
149+ it ( 'button child on click emits update:modelValue and gives value false if prop modelValue boolean' , async ( ) => {
131150 const wrapper = mount ( BAlert , {
132151 props : { modelValue : true , dismissible : true } ,
133152 } )
134153 const $button = wrapper . get ( 'button' )
135- expect ( $button . attributes ( 'data-bs-dismiss' ) ) . toBe ( 'alert' )
154+ await $button . trigger ( 'click' )
155+ expect ( wrapper . emitted ( 'update:modelValue' ) ) . toHaveLength ( 1 )
156+ const [ event ] = wrapper . emitted ( 'update:modelValue' ) ?? [ ]
157+ expect ( event ) . toEqual ( [ false ] )
136158 } )
137159
138- it ( 'button child has aria-label Close by default' , ( ) => {
160+ it ( 'button child on click emits update:modelValue and gives value 0 if prop modelValue number' , async ( ) => {
161+ const wrapper = mount ( BAlert , {
162+ props : { modelValue : 1000 , dismissible : true } ,
163+ } )
164+ const $button = wrapper . get ( 'button' )
165+ await $button . trigger ( 'click' )
166+ expect ( wrapper . emitted ( 'update:modelValue' ) ) . toHaveLength ( 1 )
167+ const [ event ] = wrapper . emitted ( 'update:modelValue' ) ?? [ ]
168+ expect ( event ) . toEqual ( [ 0 ] )
169+ } )
170+
171+ it ( 'does not have BCloseButton when slot dismissible' , ( ) => {
172+ const wrapper = mount ( BAlert , {
173+ props : { dismissible : true , modelValue : true } ,
174+ slots : { dismissible : 'foobar' } ,
175+ } )
176+ const $closebutton = wrapper . findComponent ( BCloseButton )
177+ expect ( $closebutton . exists ( ) ) . toBe ( false )
178+ } )
179+
180+ it ( 'has button child if prop dismissible and slot dismissible' , ( ) => {
181+ const wrapper = mount ( BAlert , {
182+ props : { modelValue : true , dismissible : true } ,
183+ slots : { dismissible : 'foobar' } ,
184+ } )
185+ const $button = wrapper . find ( 'button' )
186+ expect ( $button . exists ( ) ) . toBe ( true )
187+ } )
188+
189+ it ( 'does not have button child if prop dismissible false' , ( ) => {
190+ const wrapper = mount ( BAlert , {
191+ props : { modelValue : true , dismissible : false } ,
192+ slots : { dismissible : 'foobar' } ,
193+ } )
194+ const $button = wrapper . find ( 'button' )
195+ expect ( $button . exists ( ) ) . toBe ( false )
196+ } )
197+
198+ it ( 'button child has static attribute type button' , ( ) => {
139199 const wrapper = mount ( BAlert , {
140200 props : { modelValue : true , dismissible : true } ,
201+ slots : { dismissible : 'foobar' } ,
141202 } )
142203 const $button = wrapper . get ( 'button' )
143- expect ( $button . attributes ( 'aria-label ' ) ) . toBe ( 'Close ' )
204+ expect ( $button . attributes ( 'type ' ) ) . toBe ( 'button ' )
144205 } )
145206
146- it ( 'button child has aria-label prop dismissLabel ' , ( ) => {
207+ it ( 'button child has static attribute data-bs-dismiss alert ' , ( ) => {
147208 const wrapper = mount ( BAlert , {
148- props : { modelValue : true , dismissible : true , dismissLabel : 'foobar' } ,
209+ props : { modelValue : true , dismissible : true } ,
210+ slots : { dismissible : 'foobar' } ,
149211 } )
150212 const $button = wrapper . get ( 'button' )
151- expect ( $button . attributes ( 'aria-label ' ) ) . toBe ( 'foobar ' )
213+ expect ( $button . attributes ( 'data-bs-dismiss ' ) ) . toBe ( 'alert ' )
152214 } )
153215
154216 it ( 'button child on click emits update:modelValue' , async ( ) => {
155217 const wrapper = mount ( BAlert , {
156218 props : { modelValue : true , dismissible : true } ,
219+ slots : { dismissible : 'foobar' } ,
157220 } )
158221 const $button = wrapper . get ( 'button' )
159222 await $button . trigger ( 'click' )
@@ -163,6 +226,7 @@ describe('alert', () => {
163226 it ( 'button child on click emits dismissed' , async ( ) => {
164227 const wrapper = mount ( BAlert , {
165228 props : { modelValue : true , dismissible : true } ,
229+ slots : { dismissible : 'foobar' } ,
166230 } )
167231 const $button = wrapper . get ( 'button' )
168232 await $button . trigger ( 'click' )
@@ -172,6 +236,7 @@ describe('alert', () => {
172236 it ( 'button child on click emits update:modelValue and gives value false if prop modelValue boolean' , async ( ) => {
173237 const wrapper = mount ( BAlert , {
174238 props : { modelValue : true , dismissible : true } ,
239+ slots : { dismissible : 'foobar' } ,
175240 } )
176241 const $button = wrapper . get ( 'button' )
177242 await $button . trigger ( 'click' )
@@ -183,6 +248,7 @@ describe('alert', () => {
183248 it ( 'button child on click emits update:modelValue and gives value 0 if prop modelValue number' , async ( ) => {
184249 const wrapper = mount ( BAlert , {
185250 props : { modelValue : 1000 , dismissible : true } ,
251+ slots : { dismissible : 'foobar' } ,
186252 } )
187253 const $button = wrapper . get ( 'button' )
188254 await $button . trigger ( 'click' )
0 commit comments