Skip to content

Commit 45ef09c

Browse files
authored
Add TypeScript coverage to the intersection observer mock (#5574)
It appears that it wasn't so difficult to add type coverage to the intersection observer mock file. It was more difficult in Flow due to Flow's incorrect `IntersectionObserver` type. But TypeScript have it correct, so we can just remove the redundant type instead. Also, I removed the eslint rule that allows `@ts-nocheck` since this was the last file that used it. Ideally we shouldn't use it in the future.
2 parents fb9a4bc + b3a786c commit 45ef09c

File tree

2 files changed

+13
-42
lines changed

2 files changed

+13
-42
lines changed

eslint.config.mjs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,22 +215,6 @@ export default [
215215
// Allow require(), for example for importing JSON files.
216216
'@typescript-eslint/no-require-imports': 'off',
217217

218-
// Override the project-wide config to allow @ts-nocheck.
219-
// We really just need this for our mocks.
220-
'@typescript-eslint/ban-ts-comment': [
221-
'error',
222-
{
223-
// Allow @ts-expect-error and @ts-no-check annotations with descriptions.
224-
'ts-expect-error': 'allow-with-description',
225-
'ts-nocheck': 'allow-with-description',
226-
// Don't allow @ts-ignore because we want to be notified
227-
// when the error goes away so we can remove the annotation - use
228-
// @ts-expect-error instead
229-
'ts-ignore': true,
230-
'ts-check': false,
231-
},
232-
],
233-
234218
// Adding more errors now
235219
'testing-library/no-manual-cleanup': 'error',
236220
'testing-library/no-wait-for-snapshot': 'error',

src/test/fixtures/mocks/intersection-observer.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
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
*/
238
type 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

9577
function 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

Comments
 (0)