11# jsdom-testing-mocks
2+
23A set of tools for emulating browser behavior in jsdom environment
34
45## Installation
@@ -7,39 +8,58 @@ A set of tools for emulating browser behavior in jsdom environment
78npm install --save-dev jsdom-testing-mocks
89```
910
11+ or
12+
13+ ``` sh
14+ yarn add -D jsdom-testing-mocks
15+ ```
16+
1017## Mock viewport
18+
1119Mocks ` matchMedia ` , allows testing of component's behavior depending on the viewport description (supports all of the [ Media Features] ( http://www.w3.org/TR/css3-mediaqueries/#media1 ) ). ` mockViewport ` must be called before rendering the component
1220
1321Example, using ` React Testing Library ` :
22+
1423``` jsx
15- import { mockViewport } from ' jsdom-testing-mocks"
24+ import { mockViewport } from ' jsdom-testing-mocks' ;
1625
1726it (' shows the right lines on desktop and mobile' , () => {
18- const viewport = mockViewport({ width: ' 320px' , height: ' 568px' })
27+ const viewport = mockViewport ({ width: ' 320px' , height: ' 568px' });
1928
20- const { getByText, queryByText } = render(<TestComponent />)
29+ render (< TestComponent / > );
2130
22- expect(getByText(' Content visible only on the phone' )).toBeInTheDocument()
23- expect(queryByText(' Content visible only on desktop' )).not.toBeInTheDocument()
31+ expect (
32+ screen .getByText (' Content visible only on small screens' )
33+ ).toBeInTheDocument ();
34+
35+ expect (
36+ screen .queryByText (' Content visible only on small screens' )
37+ ).not .toBeInTheDocument ();
2438
2539 act (() => {
26- viewport.set({ width: ' 1440px' , height: ' 900px' })
27- })
40+ viewport .set ({ width: ' 1440px' , height: ' 900px' });
41+ });
2842
29- expect(queryByText(' Content visible only on the phone' )).not.toBeInTheDocument()
30- expect(getByText(' Content visible only on desktop' )).toBeInTheDocument()
43+ expect (
44+ screen .queryByText (' Content visible only on small screens' )
45+ ).not .toBeInTheDocument ();
3146
32- viewport.cleanup()
33- })
47+ expect (
48+ screen .getByText (' Content visible only on small screens' )
49+ ).toBeInTheDocument ();
50+
51+ viewport .cleanup ();
52+ });
3453```
3554
3655Also, you can mock the viewport for a group of tests, using ` mockViewportForTestGroup ` :
56+
3757``` jsx
38- import { mockViewportForTestGroup } from ' jsdom-testing-mocks"
58+ import { mockViewportForTestGroup } from ' jsdom-testing-mocks'
3959
4060describe (' Desktop specific tests' , () => {
4161 mockViewportForTestGroup ({ width: ' 1440px' , height: ' 900px' })
42-
62+
4363 test (' this' , () = {
4464 // ...
4565 })
@@ -51,39 +71,42 @@ describe('Desktop specific tests', () => {
5171```
5272
5373## Mock intersection observer
74+
5475Provides a way of triggering intersection observer events
5576
5677Example, using ` React Testing Library ` :
5778
5879``` jsx
59- import { mockIntersectionObserver } from 'jsdom-testing-mocks"
80+ import { mockIntersectionObserver } from ' jsdom-testing-mocks' ;
6081
61- const intersectionObserver = mockIntersectionObserver ()
82+ const intersectionObserver = mockIntersectionObserver ();
6283
6384it (' loads the image when the component is in the viewport' , () => {
64- const { getByAltText , queryByAltText , container } = render (< TestComponent / > )
85+ const { container } = render (< TestComponent / > );
6586
66- expect (queryByAltText (' alt text' )).not .toBeInTheDocument ()
87+ expect (screen . queryByAltText (' alt text' )).not .toBeInTheDocument ();
6788
6889 // when the component's root node is in the viewport - show the image
6990 act (() => {
70- intersectionObserver .enterNode (container .firstChild )
71- })
91+ intersectionObserver .enterNode (container .firstChild );
92+ });
7293
73- expect (getByAltText (' alt text' )).toBeInTheDocument ()
74- })
94+ expect (screen . getByAltText (' alt text' )).toBeInTheDocument ();
95+ });
7596```
7697
7798### API
7899
79100` mockIntersectionObserver ` returns an object, that has several useful methods:
80101
81102#### .enterNode(node, desc) and .leaveNode(node, desc)
103+
82104Triggers the intersection observer callback with only one node
83105and ` isIntersected ` set to ` true ` (for ` enterNode ` ) or ` false ` (for ` leaveNode ` ).
84106Other ` IntersectionObserverEntry ` params can be passed as ` desc ` argument
85107
86108#### .enterAll(desc) and .leaveAll(desc)
109+
87110Triggers the intersection observer callback for all of the observed nodes
88111and ` isIntersected ` set to ` true ` (for ` enterAll ` ) or ` false ` (for ` leaveAll ` ).
89112Other ` IntersectionObserverEntry ` params can be passed as ` desc ` argument
0 commit comments