Skip to content

Commit d6e15df

Browse files
lunaleapsfacebook-github-bot
authored andcommitted
Add errors for unavailable properties
Summary: Changelog: [Internal] - Throw error when accessing unsupported properties for IntersectionObserver Reviewed By: rubennorte Differential Revision: D85976617
1 parent 9287786 commit d6e15df

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ export default class IntersectionObserver {
9494
);
9595
}
9696

97+
if (options != null && 'delay' in options) {
98+
throw new Error(
99+
"Failed to construct 'IntersectionObserver': The 'delay' option is not supported.",
100+
);
101+
}
102+
103+
if (options != null && 'scrollMargin' in options) {
104+
throw new Error(
105+
"Failed to construct 'IntersectionObserver': The 'scrollMargin' option is not supported.",
106+
);
107+
}
108+
109+
if (options != null && 'trackVisibility' in options) {
110+
throw new Error(
111+
"Failed to construct 'IntersectionObserver': The 'trackVisibility' option is not supported.",
112+
);
113+
}
114+
97115
this._callback = callback;
98116

99117
this._rootThresholds = normalizeRootThreshold(options?.rnRootThreshold);
@@ -152,6 +170,36 @@ export default class IntersectionObserver {
152170
return this._rootThresholds;
153171
}
154172

173+
/**
174+
* The `delay` option is not supported.
175+
* @throws {Error} Always throws an error indicating this property is not supported.
176+
*/
177+
get delay(): number {
178+
throw new Error(
179+
"Failed to read the 'delay' property from 'IntersectionObserver': This property is not supported.",
180+
);
181+
}
182+
183+
/**
184+
* The `scrollMargin` option is not supported.
185+
* @throws {Error} Always throws an error indicating this property is not supported.
186+
*/
187+
get scrollMargin(): string {
188+
throw new Error(
189+
"Failed to read the 'scrollMargin' property from 'IntersectionObserver': This property is not supported.",
190+
);
191+
}
192+
193+
/**
194+
* The `trackVisibility` option is not supported.
195+
* @throws {Error} Always throws an error indicating this property is not supported.
196+
*/
197+
get trackVisibility(): boolean {
198+
throw new Error(
199+
"Failed to read the 'trackVisibility' property from 'IntersectionObserver': This property is not supported.",
200+
);
201+
}
202+
155203
/**
156204
* Adds an element to the set of target elements being watched by the
157205
* `IntersectionObserver`.

packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,103 @@ describe('IntersectionObserver', () => {
473473
new IntersectionObserver(() => {}, {rnRootThreshold: []}).thresholds,
474474
).toEqual([0]);
475475
});
476+
477+
describe('unsupported options', () => {
478+
it('should throw if `delay` option is specified', () => {
479+
expect(() => {
480+
// $FlowExpectedError[prop-missing] delay is not defined in Flow.
481+
return new IntersectionObserver(() => {}, {delay: 100});
482+
}).toThrow(
483+
"Failed to construct 'IntersectionObserver': The 'delay' option is not supported.",
484+
);
485+
});
486+
487+
it('should throw if `scrollMargin` option is specified', () => {
488+
expect(() => {
489+
// $FlowExpectedError[prop-missing] scrollMargin is not defined in Flow.
490+
return new IntersectionObserver(() => {}, {scrollMargin: '10px'});
491+
}).toThrow(
492+
"Failed to construct 'IntersectionObserver': The 'scrollMargin' option is not supported.",
493+
);
494+
});
495+
496+
it('should throw if `trackVisibility` option is specified', () => {
497+
expect(() => {
498+
// $FlowExpectedError[prop-missing] trackVisibility is not defined in Flow.
499+
return new IntersectionObserver(() => {}, {trackVisibility: true});
500+
}).toThrow(
501+
"Failed to construct 'IntersectionObserver': The 'trackVisibility' option is not supported.",
502+
);
503+
});
504+
505+
it('should throw if `delay` option is set to undefined explicitly', () => {
506+
expect(() => {
507+
// $FlowExpectedError[prop-missing] delay is not defined in Flow.
508+
return new IntersectionObserver(() => {}, {delay: undefined});
509+
}).toThrow(
510+
"Failed to construct 'IntersectionObserver': The 'delay' option is not supported.",
511+
);
512+
});
513+
514+
it('should throw if `scrollMargin` option is set to null explicitly', () => {
515+
expect(() => {
516+
// $FlowExpectedError[prop-missing] scrollMargin is not defined in Flow.
517+
return new IntersectionObserver(() => {}, {scrollMargin: null});
518+
}).toThrow(
519+
"Failed to construct 'IntersectionObserver': The 'scrollMargin' option is not supported.",
520+
);
521+
});
522+
523+
it('should not throw when unsupported options are not specified', () => {
524+
expect(() => {
525+
return new IntersectionObserver(() => {}, {
526+
threshold: 0.5,
527+
rootMargin: '10px',
528+
});
529+
}).not.toThrow();
530+
});
531+
532+
it('should throw if multiple unsupported options are specified', () => {
533+
expect(() => {
534+
// $FlowExpectedError[prop-missing] delay and trackVisibility are not defined in Flow.
535+
return new IntersectionObserver(() => {}, {
536+
delay: 100,
537+
trackVisibility: true,
538+
});
539+
}).toThrow(
540+
"Failed to construct 'IntersectionObserver': The 'delay' option is not supported.",
541+
);
542+
});
543+
});
544+
545+
describe('unsupported property getters', () => {
546+
it('should throw when accessing delay getter', () => {
547+
observer = new IntersectionObserver(() => {});
548+
expect(() => {
549+
return observer.delay;
550+
}).toThrow(
551+
"Failed to read the 'delay' property from 'IntersectionObserver': This property is not supported.",
552+
);
553+
});
554+
555+
it('should throw when accessing scrollMargin getter', () => {
556+
observer = new IntersectionObserver(() => {});
557+
expect(() => {
558+
return observer.scrollMargin;
559+
}).toThrow(
560+
"Failed to read the 'scrollMargin' property from 'IntersectionObserver': This property is not supported.",
561+
);
562+
});
563+
564+
it('should throw when accessing trackVisibility getter', () => {
565+
observer = new IntersectionObserver(() => {});
566+
expect(() => {
567+
return observer.trackVisibility;
568+
}).toThrow(
569+
"Failed to read the 'trackVisibility' property from 'IntersectionObserver': This property is not supported.",
570+
);
571+
});
572+
});
476573
});
477574

478575
describe('observe(target)', () => {

0 commit comments

Comments
 (0)