@@ -27,6 +27,55 @@ describe('Switch 组件测试', () => {
2727 expect ( container . children [ 0 ] . classList . contains ( 't-size-s' ) ) . toBeTruthy ( ) ;
2828 } ) ;
2929
30+ test ( 'shape' , async ( ) => {
31+ const { container, rerender, queryByText } = render ( < Switch label = { [ '开' , '关' ] } /> ) ;
32+ expect ( container . children [ 0 ] . classList . contains ( 't-switch--shape-circle' ) ) . toBeTruthy ( ) ;
33+ expect ( queryByText ( '关' ) ) . toBeInTheDocument ( ) ;
34+
35+ rerender ( < Switch shape = "round" /> ) ;
36+ expect ( container . children [ 0 ] . classList . contains ( 't-switch--shape-round' ) ) . toBeTruthy ( ) ;
37+
38+ rerender ( < Switch shape = "line" label = { [ '开' , '关' ] } size = "small" loading /> ) ;
39+ expect ( container . children [ 0 ] . classList . contains ( 't-switch--shape-line' ) ) . toBeTruthy ( ) ;
40+ expect ( container . children [ 0 ] . classList . contains ( 't-size-s' ) ) . toBeTruthy ( ) ;
41+ expect ( container . children [ 0 ] . classList . contains ( 't-is-loading' ) ) . toBeTruthy ( ) ;
42+ expect ( queryByText ( '关' ) ) . not . toBeInTheDocument ( ) ;
43+ expect ( container . querySelector ( '.t-switch__content' ) ) . toBeFalsy ( ) ;
44+
45+ const label = vi . fn ( ( ) => '关' ) ;
46+ rerender ( < Switch shape = "line" label = { label } /> ) ;
47+ expect ( label ) . not . toBeCalled ( ) ;
48+ } ) ;
49+
50+ test ( 'line shape aria-label' , async ( ) => {
51+ const { container, rerender } = render ( < Switch shape = "line" label = { [ '开启' , '关闭' ] } /> ) ;
52+ expect ( container . firstChild ) . toHaveAttribute ( 'aria-label' , '关闭' ) ;
53+
54+ fireEvent . click ( container . firstChild ) ;
55+ expect ( container . firstChild ) . toHaveAttribute ( 'aria-label' , '开启' ) ;
56+
57+ // 独立 render,innerChecked 默认 false
58+ const { container : container2 } = render ( < Switch shape = "line" /> ) ;
59+ expect ( container2 . firstChild ) . toHaveAttribute ( 'aria-label' , '已关闭' ) ;
60+
61+ rerender ( < Switch shape = "line" label = { [ '开' , '关' ] } aria-label = "自定义开关" /> ) ;
62+ expect ( container . firstChild ) . toHaveAttribute ( 'aria-label' , '自定义开关' ) ;
63+
64+ rerender ( < Switch shape = "circle" label = { [ '开' , '关' ] } /> ) ;
65+ expect ( container . firstChild ) . not . toHaveAttribute ( 'aria-label' ) ;
66+ } ) ;
67+
68+ test ( 'aria-disabled' , async ( ) => {
69+ const { container } = render ( < Switch disabled /> ) ;
70+ expect ( container . firstChild ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
71+
72+ const { container : container2 } = render ( < Switch loading /> ) ;
73+ expect ( container2 . firstChild ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
74+
75+ const { container : container3 } = render ( < Switch /> ) ;
76+ expect ( container3 . firstChild ) . toHaveAttribute ( 'aria-disabled' , 'false' ) ;
77+ } ) ;
78+
3079 test ( 'disabled' , async ( ) => {
3180 const clickFn = vi . fn ( ) ;
3281 const { container } = render ( < Switch disabled /> ) ;
@@ -37,7 +86,9 @@ describe('Switch 组件测试', () => {
3786 test ( 'onChange' , async ( ) => {
3887 const clickFn = vi . fn ( ) ;
3988 const { container } = render ( < Switch onChange = { clickFn } /> ) ;
89+ expect ( container . firstChild ) . toHaveAttribute ( 'aria-checked' , 'false' ) ;
4090 fireEvent . click ( container . firstChild ) ;
91+ expect ( container . firstChild ) . toHaveAttribute ( 'aria-checked' , 'true' ) ;
4192 expect ( clickFn ) . toHaveBeenCalledTimes ( 1 ) ;
4293 } ) ;
4394
0 commit comments