11/* This Source Code Form is subject to the terms of the Mozilla Public
22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4- // @ts -nocheck Complex DOM mock with intricate typing that would require extensive work to properly type
5-
6- /**
7- * Creating a mock intersection observer type because Flow's IntersectionObserver
8- * type is not completely correct in this version.
9- */
10- type MockIntersectionObserver = {
11- thresholds : number [ ] ;
12- root : HTMLElement | Document ;
13- rootMargin : string ;
14- observe : ( param : HTMLElement ) => void ;
15- unobserve : ( param : HTMLElement ) => void ;
16- disconnect : ( ) => void ;
17- takeRecords : ( ) => void ;
18- } ;
194
205/**
216 * Type of the item we are going to keep for tracking observers.
227 */
238type Item = {
249 callback : (
25- param : IntersectionObserverEntry [ ] ,
26- MockIntersectionObserver
10+ entries : IntersectionObserverEntry [ ] ,
11+ observer : IntersectionObserver
2712 ) => void ;
2813 elements : Set < HTMLElement > ;
2914 created : number ;
@@ -32,10 +17,7 @@ type Item = {
3217/**
3318 * Tracked observers during the testing.
3419 */
35- const observers : Map < MockIntersectionObserver , Item > = new Map <
36- unknown ,
37- unknown
38- > ( ) ;
20+ const observers : Map < IntersectionObserver , Item > = new Map ( ) ;
3921
4022/**
4123 * Call this function inside a `describe` block to automatically define the
@@ -50,16 +32,16 @@ const observers: Map<MockIntersectionObserver, Item> = new Map<
5032 * give you a better control over the intersection observer and is used mostly
5133 * when you want to test the intersection observer behavior.
5234 */
53- export function autoMockIntersectionObserver ( autoTrigger ? : boolean = true ) {
35+ export function autoMockIntersectionObserver ( autoTrigger : boolean = true ) {
5436 beforeEach ( ( ) => {
5537 ( window as any ) . IntersectionObserver = jest . fn ( ( cb , options = { } ) => {
56- const item = {
38+ const item : Item = {
5739 callback : cb ,
5840 elements : new Set ( ) ,
5941 created : Date . now ( ) ,
6042 } ;
6143
62- const instance : MockIntersectionObserver = {
44+ const instance : IntersectionObserver = {
6345 thresholds : Array . isArray ( options . threshold )
6446 ? options . threshold
6547 : [ options . threshold ?? 0 ] ,
@@ -93,7 +75,7 @@ export function autoMockIntersectionObserver(autoTrigger?: boolean = true) {
9375}
9476
9577function triggerSingleObserver (
96- observer : MockIntersectionObserver ,
78+ observer : IntersectionObserver ,
9779 item : Item ,
9880 isIntersecting : boolean = true
9981) {
@@ -105,7 +87,12 @@ function triggerSingleObserver(
10587 intersectionRatio : 1 ,
10688 intersectionRect : element . getBoundingClientRect ( ) ,
10789 isIntersecting : isIntersecting ,
108- rootBounds : observer . root ? observer . root . getBoundingClientRect ( ) : null ,
90+ // `root` is `Element | Document | null`. We can call getBoundingClientRect
91+ // only if it's an Element.
92+ rootBounds :
93+ observer . root && observer . root instanceof Element
94+ ? observer . root . getBoundingClientRect ( )
95+ : null ,
10996 target : element ,
11097 time : Date . now ( ) - item . created ,
11198 } ) ;
0 commit comments