Skip to content

Commit de961f6

Browse files
committed
Fix tests by mocking invariant
1 parent 49e43cc commit de961f6

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"lint": "eslint {src,stories,tests}/. ",
2727
"precommit": "flow && lint-staged",
2828
"postcommit": "git reset",
29-
"prepublish": "npm run build",
29+
"prepare": "npm run build",
3030
"pretty": "prettier '**/*.{js,md,json}' --write",
3131
"storybook": "start-storybook -p 9000",
3232
"test": "eslint {src,stories,tests}/. && jest"

tests/Observer.test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import React from 'react'
22
import { mount } from 'enzyme'
33
import Observer from '../src/index.js'
4+
import invariant from 'invariant'
45

56
jest.mock('../src/intersection')
7+
jest.mock('invariant')
68

79
it('Should render <Observer />', () => {
810
const callback = jest.fn()
@@ -60,11 +62,15 @@ it('Should render <Observer /> render when in view', () => {
6062
})
6163

6264
it('Should throw error when not passing ref', () => {
63-
expect(() =>
64-
mount(
65-
<Observer
66-
render={({ inView, ref }) => <div>Inview: {inView.toString()}</div>}
67-
/>,
68-
),
69-
).toThrow()
65+
invariant.mockReset()
66+
67+
mount(
68+
<Observer
69+
render={({ inView, ref }) => <div>Inview: {inView.toString()}</div>}
70+
/>,
71+
)
72+
expect(invariant).toHaveBeenLastCalledWith(
73+
null,
74+
'react-intersection-observer: No DOM node found. Make sure you forward "ref" to the root DOM element you want to observe, when using render prop.',
75+
)
7076
})

tests/__snapshots__/Observer.test.js.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,3 @@ exports[`Should render <Observer /> with children outside view 1`] = `
6161
</div>
6262
</Observer>
6363
`;
64-
65-
exports[`Should throw error when not passing ref 1`] = `"An error was thrown inside one of your components, but React doesn't know what it was. This is likely due to browser flakiness. React does its best to preserve the \\"Pause on exceptions\\" behavior of the DevTools, which requires some DEV-mode only tricks. It's possible that these don't work in your browser. Try triggering the error in production mode, or switching to a modern browser. If you suspect that this is actually an issue with React, please file an issue."`;

0 commit comments

Comments
 (0)