@@ -18,25 +18,37 @@ const roundClass = ns.m('round')
1818const circleClass = ns . m ( 'circle' )
1919const loadingClass = ns . m ( 'loading' )
2020
21+ // 测试辅助函数
22+ function createWrapper ( props = { } , slots = { } ) {
23+ return mount ( Button , {
24+ props,
25+ slots : {
26+ default : 'Button' ,
27+ ...slots ,
28+ } ,
29+ } )
30+ }
31+
32+ function createShallowWrapper ( props = { } , slots = { } ) {
33+ return shallowMount ( Button , {
34+ props,
35+ slots : {
36+ default : 'Button' ,
37+ ...slots ,
38+ } ,
39+ } )
40+ }
41+
2142describe ( 'button' , ( ) => {
2243 it ( 'dom' , ( ) => {
23- const wrapper = shallowMount ( Button , {
24- slots : {
25- default : '确定' ,
26- } ,
27- } )
28-
29- // 元素是否存在
44+ const wrapper = createShallowWrapper ( { } , { default : '确定' } )
3045 expect ( wrapper . find ( baseClass ) . exists ( ) ) . toBeTruthy ( )
31-
32- // 默认插槽的文本是否正确
3346 expect ( wrapper . find ( baseClass ) . text ( ) ) . toBe ( '确定' )
34-
3547 wrapper . unmount ( )
3648 } )
3749
3850 it ( 'type' , async ( ) => {
39- const wrapper = shallowMount ( Button , { props : { type : 'primary' } } )
51+ const wrapper = createShallowWrapper ( { type : 'primary' } )
4052 expect ( wrapper . find ( getTypeClass ( 'primary' ) ) . exists ( ) ) . toBeTruthy ( )
4153
4254 await wrapper . setProps ( { type : 'success' } )
@@ -53,189 +65,92 @@ describe('button', () => {
5365 } )
5466
5567 it ( 'size' , async ( ) => {
56- const wrapper = shallowMount ( Button , { props : { size : 'small' } } )
68+ const wrapper = createShallowWrapper ( { size : 'small' } )
5769 expect ( wrapper . find ( getSizeClass ( 'small' ) ) . exists ( ) ) . toBeTruthy ( )
5870
5971 await wrapper . setProps ( { size : 'large' } )
6072 expect ( wrapper . find ( getSizeClass ( 'large' ) ) . exists ( ) ) . toBeTruthy ( )
6173 } )
6274
6375 it ( 'round' , async ( ) => {
64- const wrapper = shallowMount ( Button , { props : { round : true } } )
76+ const wrapper = createShallowWrapper ( { round : true } )
6577 expect ( wrapper . find ( roundClass ) . exists ( ) ) . toBeTruthy ( )
6678 } )
6779
6880 it ( 'circle' , async ( ) => {
69- const wrapper = shallowMount ( Button , { props : { circle : true } } )
81+ const wrapper = createShallowWrapper ( { circle : true } )
7082 expect ( wrapper . find ( circleClass ) . exists ( ) ) . toBeTruthy ( )
7183 } )
7284
73- it ( 'disabled ' , async ( ) => {
85+ it ( 'click events ' , async ( ) => {
7486 const handleClick = vi . fn ( )
75- const wrapper = shallowMount ( Button , { props : { disabled : true } } )
76-
77- // 检查是否应用了禁用样式类
78- const disabledClass = ns . m ( 'disabled' ) . substring ( 1 ) // 移除开头的点
79- expect ( wrapper . find ( 'button' ) . classes ( ) ) . toContain ( disabledClass )
80-
81- await wrapper . trigger ( 'click' )
82- expect ( handleClick ) . not . toBeCalled ( )
83- } )
84-
85- it ( 'emits click event when clicked' , async ( ) => {
86- const handleClick = vi . fn ( )
87- const wrapper = mount ( Button , {
88- slots : {
89- default : 'Click me' ,
90- } ,
91- attrs : {
92- onClick : handleClick ,
93- } ,
94- } )
9587
88+ // Test normal click
89+ const wrapper = createWrapper ( { } , { default : 'Click me' } )
90+ wrapper . element . addEventListener ( 'click' , handleClick )
9691 await wrapper . trigger ( 'click' )
9792 expect ( handleClick ) . toBeCalled ( )
98-
9993 wrapper . unmount ( )
100- } )
101-
102- it ( 'does not emit click event when disabled' , async ( ) => {
103- const handleClick = vi . fn ( )
104- const wrapper = mount ( Button , {
105- props : {
106- disabled : true ,
107- } ,
108- slots : {
109- default : 'Click me' ,
110- } ,
111- attrs : {
112- onClick : handleClick ,
113- } ,
114- } )
11594
116- await wrapper . trigger ( 'click' )
95+ // Test disabled - no click
96+ handleClick . mockClear ( )
97+ const disabledWrapper = createWrapper ( { disabled : true } )
98+ const disabledClass = ns . m ( 'disabled' ) . substring ( 1 )
99+ expect ( disabledWrapper . find ( 'button' ) . classes ( ) ) . toContain ( disabledClass )
100+ disabledWrapper . element . addEventListener ( 'click' , handleClick )
101+ await disabledWrapper . trigger ( 'click' )
117102 expect ( handleClick ) . not . toBeCalled ( )
118-
119- wrapper . unmount ( )
103+ disabledWrapper . unmount ( )
104+
105+ // Test loading - no click
106+ handleClick . mockClear ( )
107+ const loadingWrapper = createWrapper ( { loading : true } )
108+ expect ( loadingWrapper . find ( loadingClass ) . exists ( ) ) . toBeTruthy ( )
109+ expect ( loadingWrapper . find ( 'button' ) . attributes ( 'disabled' ) ) . toBe ( '' )
110+ expect ( loadingWrapper . find ( 'button' ) . classes ( ) ) . toContain ( disabledClass )
111+ loadingWrapper . element . addEventListener ( 'click' , handleClick )
112+ await loadingWrapper . trigger ( 'click' )
113+ expect ( handleClick ) . not . toBeCalled ( )
114+ loadingWrapper . unmount ( )
120115 } )
121116
122117 it ( 'renders icon slot when provided' , ( ) => {
123- const wrapper = mount ( Button , {
124- slots : {
125- icon : '<i class="cc-icon-heart"></i>' ,
126- default : 'Like' ,
127- } ,
118+ const wrapper = createWrapper ( { } , {
119+ icon : '<i class="cc-icon-heart"></i>' ,
120+ default : 'Like' ,
128121 } )
129-
130122 expect ( wrapper . find ( '.cc-icon-heart' ) . exists ( ) ) . toBe ( true )
131-
132123 wrapper . unmount ( )
133124 } )
134125
135126 it ( 'applies plain style when plain prop is true' , async ( ) => {
136- const wrapper = shallowMount ( Button , {
137- props : {
138- type : 'primary' ,
139- plain : true ,
140- } ,
141- } )
142-
127+ const wrapper = createShallowWrapper ( { type : 'primary' , plain : true } )
143128 expect ( wrapper . find ( ns . m ( 'plain-primary' ) ) . exists ( ) ) . toBeTruthy ( )
144-
145129 wrapper . unmount ( )
146130 } )
147131
148132 it ( 'sets nativeType attribute correctly' , ( ) => {
149- const wrapper = mount ( Button , {
150- props : {
151- nativeType : 'submit' ,
152- } ,
153- slots : {
154- default : 'Submit' ,
155- } ,
156- } )
157-
133+ const wrapper = createWrapper ( { nativeType : 'submit' } , { default : 'Submit' } )
158134 expect ( wrapper . find ( 'button' ) . attributes ( 'type' ) ) . toBe ( 'submit' )
159-
160135 wrapper . unmount ( )
161136 } )
162137
163138 it ( 'sets autofocus attribute when autofocus is true' , ( ) => {
164- const wrapper = mount ( Button , {
165- props : {
166- autofocus : true ,
167- } ,
168- slots : {
169- default : 'Focus' ,
170- } ,
171- } )
172-
139+ const wrapper = createWrapper ( { autofocus : true } , { default : 'Focus' } )
173140 expect ( wrapper . find ( 'button' ) . attributes ( 'autofocus' ) ) . toBe ( '' )
174-
175- wrapper . unmount ( )
176- } )
177-
178- it ( 'shows loading state when loading prop is true' , async ( ) => {
179- const wrapper = shallowMount ( Button , { props : { loading : true } } )
180- expect ( wrapper . find ( loadingClass ) . exists ( ) ) . toBeTruthy ( )
181- expect ( wrapper . find ( 'button' ) . attributes ( 'disabled' ) ) . toBe ( '' )
182-
183- // 检查是否应用了禁用样式类
184- const disabledClass = ns . m ( 'disabled' ) . substring ( 1 ) // 移除开头的点
185- expect ( wrapper . find ( 'button' ) . classes ( ) ) . toContain ( disabledClass )
186-
187- wrapper . unmount ( )
188- } )
189-
190- it ( 'does not emit click event when loading' , async ( ) => {
191- const handleClick = vi . fn ( )
192- const wrapper = mount ( Button , {
193- props : {
194- loading : true ,
195- } ,
196- slots : {
197- default : 'Loading' ,
198- } ,
199- attrs : {
200- onClick : handleClick ,
201- } ,
202- } )
203-
204- await wrapper . trigger ( 'click' )
205- expect ( handleClick ) . not . toBeCalled ( )
206-
207141 wrapper . unmount ( )
208142 } )
209143
210144 it ( 'renders icon when icon prop is provided' , ( ) => {
211- const wrapper = mount ( Button , {
212- props : {
213- icon : 'cc-icon-star' ,
214- } ,
215- slots : {
216- default : 'Star' ,
217- } ,
218- } )
219-
145+ const wrapper = createWrapper ( { icon : 'cc-icon-star' } , { default : 'Star' } )
220146 expect ( wrapper . find ( '.cc-icon-star' ) . exists ( ) ) . toBe ( true )
221-
222147 wrapper . unmount ( )
223148 } )
224149
225150 it ( 'renders loading icon when loading is true' , ( ) => {
226- const wrapper = mount ( Button , {
227- props : {
228- loading : true ,
229- } ,
230- slots : {
231- default : 'Loading' ,
232- } ,
233- } )
234-
151+ const wrapper = createWrapper ( { loading : true } , { default : 'Loading' } )
235152 expect ( wrapper . find ( ns . e ( 'loading-icon' ) ) . exists ( ) ) . toBe ( true )
236- // 验证加载图标是空的span元素
237153 expect ( wrapper . find ( ns . e ( 'loading-icon' ) ) . text ( ) ) . toBe ( '' )
238-
239154 wrapper . unmount ( )
240155 } )
241156} )
0 commit comments