@@ -5,11 +5,23 @@ import Affix from '../index';
55describe ( 'Affix 组件测试' , ( ) => {
66 const mockFn = vi . spyOn ( HTMLDivElement . prototype , 'getBoundingClientRect' ) ;
77 const mockScrollTo = async ( top : number ) => {
8- mockFn . mockImplementation ( ( ) => ( { top, bottom : 0 } ) ) ;
8+ mockFn . mockImplementation ( ( ) => ( {
9+ top,
10+ bottom : 0 ,
11+ left : 0 ,
12+ right : 0 ,
13+ height : 0 ,
14+ width : 0 ,
15+ x : 0 ,
16+ y : 0 ,
17+ toJSON : ( ) => ( { } ) ,
18+ } ) ) ;
919 } ;
20+
1021 beforeEach ( async ( ) => {
1122 await mockScrollTo ( 0 ) ;
1223 } ) ;
24+
1325 test ( 'render perfectly' , async ( ) => {
1426 const { queryByText } = render (
1527 < Affix >
@@ -19,24 +31,115 @@ describe('Affix 组件测试', () => {
1931
2032 expect ( queryByText ( '固钉' ) ) . toBeInTheDocument ( ) ;
2133 } ) ;
22- test ( 'offsetTop and onFixedChange and zIndex' , async ( ) => {
34+
35+ test ( 'className' , async ( ) => {
36+ const { container } = render (
37+ < Affix className = "custom-class-name" >
38+ < div > 固钉</ div >
39+ </ Affix > ,
40+ ) ;
41+
42+ const affixElement = container . querySelector ( '.custom-class-name' ) ;
43+ expect ( affixElement ) . not . toBeNull ( ) ;
44+ expect ( affixElement ?. className ) . toContain ( 'custom-class-name' ) ;
45+ } ) ;
46+
47+ test ( 'style' , async ( ) => {
48+ const { container } = render (
49+ < Affix style = { { background : 'red' } } className = "custom-class-name" >
50+ < div > 固钉</ div >
51+ </ Affix > ,
52+ ) ;
53+ const affixElement = container . querySelector ( '.custom-class-name' ) ;
54+ expect ( affixElement ) . not . toBeNull ( ) ;
55+ expect ( ( affixElement as HTMLElement ) ?. style . background ) . toBe ( 'red' ) ;
56+ } ) ;
57+
58+ test ( 'content' , async ( ) => {
59+ const Children = ( ) => < div > 固钉</ div > ;
60+ const { queryByText } = render ( < Affix content = { < Children /> } /> ) ;
61+ expect ( queryByText ( '固钉' ) ) . toBeInTheDocument ( ) ;
62+ } ) ;
63+
64+ test ( 'offsetTop trigger onFixedChange and zIndex' , async ( ) => {
65+ const onFixedChangeMock = vi . fn ( ) ;
66+
67+ const { getByText } = render (
68+ < Affix offsetTop = { 20 } onFixedChange = { onFixedChangeMock } zIndex = { 2 } >
69+ < div > 固钉</ div >
70+ </ Affix > ,
71+ ) ;
72+ // 默认
73+ expect ( onFixedChangeMock ) . toHaveBeenCalledTimes ( 0 ) ;
74+ expect ( getByText ( '固钉' ) . parentNode ) . not . toHaveClass ( 't-affix' ) ;
75+ expect ( getByText ( '固钉' ) . parentElement ?. style . zIndex ) . toBe ( '' ) ;
76+
77+ // offsetTop
78+ await mockScrollTo ( 30 ) ;
79+ await mockScrollTo ( 10 ) ;
80+ await mockTimeout ( ( ) => false , 200 ) ;
81+ expect ( onFixedChangeMock ) . toHaveBeenCalledTimes ( 1 ) ;
82+
83+ expect ( getByText ( '固钉' ) . parentNode ) . toHaveClass ( 't-affix' ) ;
84+ expect ( getByText ( '固钉' ) . parentElement ?. style . zIndex ) . toBe ( '2' ) ;
85+ } ) ;
86+
87+ test ( 'offsetBottom trigger onFixedChange and zIndex' , async ( ) => {
2388 const onFixedChangeMock = vi . fn ( ) ;
2489
2590 const { getByText } = render (
26- < Affix offsetTop = { - 1 } onFixedChange = { onFixedChangeMock } zIndex = { 2 } >
91+ < Affix offsetBottom = { 20 } onFixedChange = { onFixedChangeMock } zIndex = { 2 } >
2792 < div > 固钉</ div >
2893 </ Affix > ,
2994 ) ;
95+ // 默认
96+ expect ( onFixedChangeMock ) . toHaveBeenCalledTimes ( 0 ) ;
97+ expect ( getByText ( '固钉' ) . parentNode ) . not . toHaveClass ( 't-affix' ) ;
98+ expect ( getByText ( '固钉' ) . parentElement ?. style . zIndex ) . toBe ( '' ) ;
99+
100+ // offsetBottom
101+ const isWindow = getByText ( '固钉' ) . parentElement && window instanceof Window ;
102+ const { clientHeight } = document . documentElement ;
103+ const { innerHeight } = window ;
104+ await mockScrollTo ( ( isWindow ? innerHeight : clientHeight ) - 40 ) ;
105+ await mockScrollTo ( isWindow ? innerHeight : clientHeight ) ;
106+ await mockTimeout ( ( ) => false , 200 ) ;
107+ expect ( onFixedChangeMock ) . toHaveBeenCalledTimes ( 1 ) ;
108+
109+ expect ( getByText ( '固钉' ) . parentNode ) . toHaveClass ( 't-affix' ) ;
110+ expect ( getByText ( '固钉' ) . parentElement ?. style . zIndex ) . toBe ( '2' ) ;
111+ } ) ;
112+
113+ test ( 'offsetTop and offsetBottom trigger onFixedChange and zIndex' , async ( ) => {
114+ const onFixedChangeMock = vi . fn ( ) ;
30115
31- expect ( onFixedChangeMock ) . toBeCalledTimes ( 0 ) ;
116+ const { getByText } = render (
117+ < Affix offsetBottom = { 20 } offsetTop = { 20 } onFixedChange = { onFixedChangeMock } zIndex = { 2 } >
118+ < div > 固钉</ div >
119+ </ Affix > ,
120+ ) ;
121+ // 默认
122+ expect ( onFixedChangeMock ) . toHaveBeenCalledTimes ( 0 ) ;
32123 expect ( getByText ( '固钉' ) . parentNode ) . not . toHaveClass ( 't-affix' ) ;
33- expect ( getByText ( '固钉' ) . parentElement . style . zIndex ) . toBe ( '' ) ;
124+ expect ( getByText ( '固钉' ) . parentElement ? .style . zIndex ) . toBe ( '' ) ;
34125
35- await mockScrollTo ( - 10 ) ;
126+ // offsetTop
127+ await mockScrollTo ( 30 ) ;
128+ await mockScrollTo ( 10 ) ;
129+ await mockTimeout ( ( ) => false , 200 ) ;
130+ expect ( onFixedChangeMock ) . toHaveBeenCalledTimes ( 1 ) ;
36131
132+ // offsetBottom
133+ const isWindow = typeof window !== 'undefined' && window . innerHeight !== undefined ;
134+ const { clientHeight } = document . documentElement ;
135+ const { innerHeight } = window ;
136+ await mockScrollTo ( ( isWindow ? innerHeight : clientHeight ) - 40 ) ;
137+ await mockScrollTo ( isWindow ? innerHeight : clientHeight ) ;
37138 await mockTimeout ( ( ) => false , 200 ) ;
139+
38140 expect ( onFixedChangeMock ) . toHaveBeenCalledTimes ( 1 ) ;
141+
39142 expect ( getByText ( '固钉' ) . parentNode ) . toHaveClass ( 't-affix' ) ;
40- expect ( getByText ( '固钉' ) . parentElement . style . zIndex ) . toBe ( '2' ) ;
143+ expect ( getByText ( '固钉' ) . parentElement ? .style . zIndex ) . toBe ( '2' ) ;
41144 } ) ;
42145} ) ;
0 commit comments