File tree Expand file tree Collapse file tree 3 files changed +12
-15
lines changed
Expand file tree Collapse file tree 3 files changed +12
-15
lines changed Original file line number Diff line number Diff line change 11import { observe , unobserve , destroy } from '../intersection'
2-
3- global . IntersectionObserver = jest . fn ( ( cb , options ) => ( {
4- thresholds : Array . isArray ( options . threshold )
5- ? options . threshold
6- : [ options . threshold ] ,
7- root : options . root ,
8- rootMargin : options . rootMargin ,
9- observe : jest . fn ( ) ,
10- unobserve : jest . fn ( ) ,
11- disconnect : jest . fn ( ) ,
12- } ) )
2+ import '../test-utils'
133
144afterEach ( ( ) => destroy ( ) )
155
@@ -22,6 +12,7 @@ it('should observe', () => {
2212 expect ( instance ) . toMatchObject ( {
2313 observerId : '0' ,
2414 inView : false ,
15+ thresholds : [ 0 ] ,
2516 observer : {
2617 thresholds : [ 0 ] ,
2718 } ,
@@ -41,6 +32,7 @@ it('should observe with options', () => {
4132 expect ( instance ) . toMatchObject ( {
4233 observerId : '0' ,
4334 inView : false ,
35+ thresholds : [ 0 ] ,
4436 observer : {
4537 thresholds : [ 0 ] ,
4638 } ,
@@ -54,6 +46,7 @@ it('should observe with threshold', () => {
5446 expect ( instance ) . toMatchObject ( {
5547 observerId : '1' ,
5648 inView : false ,
49+ thresholds : [ 1 ] ,
5750 observer : {
5851 thresholds : [ 1 ] ,
5952 } ,
@@ -67,6 +60,7 @@ it('should observe with Array threshold', () => {
6760 expect ( instance ) . toMatchObject ( {
6861 observerId : '0.3,0.6' ,
6962 inView : false ,
63+ thresholds : [ 0.3 , 0.6 ] ,
7064 observer : {
7165 thresholds : [ 0.3 , 0.6 ] ,
7266 } ,
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ export type ObserverInstance = {
1111 inView : boolean
1212 observerId : string
1313 observer : IntersectionObserver
14+ thresholds : number [ ]
1415}
1516
1617const INSTANCE_MAP : Map < Element , ObserverInstance > = new Map ( )
@@ -78,6 +79,10 @@ export function observe(
7879 inView : false ,
7980 observerId,
8081 observer : observerInstance ,
82+ // Make sure we have the thresholds value. It's undefined on a browser like Chrome 51.
83+ thresholds :
84+ observerInstance . thresholds ||
85+ ( Array . isArray ( threshold ) ? threshold : [ threshold ] ) ,
8186 }
8287
8388 INSTANCE_MAP . set ( element , instance )
@@ -150,10 +155,8 @@ function onChange(changes: IntersectionObserverEntry[]) {
150155 // Firefox can report a negative intersectionRatio when scrolling.
151156 /* istanbul ignore else */
152157 if ( instance && intersectionRatio >= 0 ) {
153- const thresholds = instance . observer . thresholds
154-
155158 // If threshold is an array, check if any of them intersects. This just triggers the onChange event multiple times.
156- let inView = thresholds . some ( threshold => {
159+ let inView = instance . thresholds . some ( threshold => {
157160 return instance . inView
158161 ? intersectionRatio > threshold
159162 : intersectionRatio >= threshold
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ beforeAll(() => {
2828
2929afterEach ( ( ) => {
3030 // @ts -ignore
31- global . IntersectionObserver . mockReset ( )
31+ global . IntersectionObserver . mockClear ( )
3232 instanceMap . clear ( )
3333 observerMap . clear ( )
3434} )
You can’t perform that action at this time.
0 commit comments