react-intersection-observer
Advanced tools
Comparing version 8.33.0 to 8.33.1
{ | ||
"name": "react-intersection-observer", | ||
"version": "8.33.0", | ||
"version": "8.33.1", | ||
"description": "Monitor if a component is inside the viewport, using IntersectionObserver API", | ||
@@ -5,0 +5,0 @@ "source": "src/index.tsx", |
@@ -257,4 +257,8 @@ # react-intersection-observer | ||
Import the methods from `react-intersection-observer/test-utils`. | ||
You can use these test utilities as imports in individual files OR you can globally mock Intersection Observer for all Jest tests. If you use a library or an application with a lot of Intersection Observer usage, you may wish to globally mock it; however, the official recommendation is to be purposeful about your mocking and do so on a per-usage basis. | ||
#### Indvidual Methods | ||
Import these from `react-intersection-observer/test-utils`. | ||
**`mockAllIsIntersecting(isIntersecting:boolean | number)`** | ||
@@ -271,2 +275,27 @@ Set `isIntersecting` on all current IntersectionObserver instances. | ||
#### Global Intersection Observer Behavior | ||
##### Use Fallback | ||
You can create a [Jest setup file](https://jestjs.io/docs/configuration#setupfiles-array) that leverages the [unsupported fallback](https://github.com/thebuilder/react-intersection-observer#unsupported-fallback) option. In this case, you only mock the IntersectionObserver in test files were you actively import `react-intersection-observer/test-utils`: | ||
```js | ||
import { defaultFallbackInView } from 'react-intersection-observer'; | ||
defaultFallbackInView(true); // or 'false' - whichever consistent behavior makes the most sense for your use case. | ||
``` | ||
##### Mock Everywhere | ||
In your Jest config, add `'react-intersection-observer/test-utils'` to the array value for the [`setupFilesAfterEnv`](https://jestjs.io/docs/configuration#setupfilesafterenv-array) option. | ||
```js | ||
module.exports = { | ||
// other config lines | ||
setupFilesAfterEnv: ['react-intersection-observer/test-utils'], | ||
// other config lines | ||
}; | ||
``` | ||
### Test Example | ||
@@ -273,0 +302,0 @@ |
@@ -134,4 +134,4 @@ "use strict"; | ||
} | ||
throw new Error('Failed to find IntersectionObserver for element. Is it being observer?'); | ||
throw new Error('Failed to find IntersectionObserver for element. Is it being observed?'); | ||
} | ||
exports.intersectionMockInstance = intersectionMockInstance; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
195658
454