@@ -41,7 +41,7 @@ describe('useElementVisibility', () => {
41
41
it ( 'passes a callback to useIntersectionObserver that sets visibility to false only when isIntersecting is false' , ( ) => {
42
42
const isVisible = useElementVisibility ( el )
43
43
const callback = vi . mocked ( useIntersectionObserver ) . mock . lastCall ?. [ 1 ]
44
- const callMockCallbackWithIsIntersectingValue = ( isIntersecting : boolean ) => callback ?.( [ { isIntersecting } as IntersectionObserverEntry ] , { } as IntersectionObserver )
44
+ const callMockCallbackWithIsIntersectingValue = ( isIntersecting : boolean ) => callback ?.( [ { isIntersecting, time : 1 } as IntersectionObserverEntry ] , { } as IntersectionObserver )
45
45
46
46
// It should be false initially
47
47
expect ( isVisible . value ) . toBe ( false )
@@ -59,6 +59,34 @@ describe('useElementVisibility', () => {
59
59
expect ( isVisible . value ) . toBe ( false )
60
60
} )
61
61
62
+ it ( 'uses the latest version of isIntersecting when multiple intersection entries are given' , ( ) => {
63
+ const isVisible = useElementVisibility ( el )
64
+ const callback = vi . mocked ( useIntersectionObserver ) . mock . lastCall ?. [ 1 ]
65
+ const callMockCallbackWithIsIntersectingValues = ( ...entries : { isIntersecting : boolean ; time : number } [ ] ) => {
66
+ callback ?.( entries as IntersectionObserverEntry [ ] , { } as IntersectionObserver )
67
+ }
68
+
69
+ // It should be false initially
70
+ expect ( isVisible . value ) . toBe ( false )
71
+
72
+ // It should take the latest value of isIntersecting
73
+ callMockCallbackWithIsIntersectingValues (
74
+ { isIntersecting : false , time : 1 } ,
75
+ { isIntersecting : false , time : 2 } ,
76
+ { isIntersecting : true , time : 3 } ,
77
+ )
78
+ expect ( isVisible . value ) . toBe ( true )
79
+
80
+ // It should take the latest even when entries are out of order
81
+ callMockCallbackWithIsIntersectingValues (
82
+ { isIntersecting : true , time : 1 } ,
83
+ { isIntersecting : false , time : 3 } ,
84
+ { isIntersecting : true , time : 2 } ,
85
+ )
86
+
87
+ expect ( isVisible . value ) . toBe ( false )
88
+ } )
89
+
62
90
it ( 'passes the given window to useIntersectionObserver' , ( ) => {
63
91
const mockWindow = { } as Window
64
92
0 commit comments