Skip to content

Commit 51605f9

Browse files
committed
Fix unit test coverage
1 parent 7faf7c0 commit 51605f9

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

src/ScrollArea.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component, createRef } from "react";
1+
import React, { Component } from "react";
22
import { ScrollToContext } from "./ScrollTo";
33
import generateId from "./utilities/generateId";
44

@@ -13,7 +13,8 @@ export function createRefPoly() {
1313
}
1414

1515
export class ScrollArea extends Component {
16-
node = createRef ? createRef() : createRefPoly();
16+
// Using React.createRef so we can easily unit test this
17+
node = React.createRef ? React.createRef() : createRefPoly();
1718
id = this.props.id || generateId();
1819

1920
componentDidMount() {

src/ScrollTo.jsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { Component, createContext, isValidElement } from "react";
2-
import { findDOMNode } from "react-dom";
1+
import React, { Component, isValidElement } from "react";
2+
import ReactDOM from "react-dom";
33
import { relative } from "./utilities/relative";
44
import createReactContext from "create-react-context";
55

6-
export const ScrollToContext = !createContext
7-
? createReactContext({})
8-
: createContext({});
6+
/* istanbul ignore next */
7+
export const ScrollToContext = React.createContext
8+
? React.createContext({}) /* istanbul ignore next */
9+
: createReactContext({});
910

1011
/**
1112
* Component that uses render props to inject
@@ -63,9 +64,12 @@ class ScrollTo extends Component {
6364
const top = ScrollTo._parseLocation(options.y, node, true);
6465
const left = ScrollTo._parseLocation(options.x, node, false);
6566

67+
/* istanbul ignore next */
6668
if (isValidElement(node)) {
67-
const rNode = findDOMNode(node);
69+
/* istanbul ignore next */
70+
const rNode = ReactDOM.findDOMNode(node);
6871

72+
/* istanbul ignore next */
6973
if (rNode) {
7074
node = rNode;
7175
}

src/tests/ScrollArea.spec.jsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,26 @@ describe("Test Scroll Area.", () => {
7979

8080
expect(ref.current).toBeTruthy();
8181
});
82+
83+
it("Should handle a null element passed to createRef", () => {
84+
const ref = createRefPoly();
85+
86+
ref(null);
87+
88+
expect(ref.current).toBeFalsy();
89+
});
90+
91+
it("Should use createRefPoly() when createRef() doesn't exist", () => {
92+
const rCreateRefTemp = React.createRef;
93+
React.createRef = false;
94+
95+
const { container, debug } = render(
96+
<BaseScrollArea addScrollArea={() => {}} removeScrollArea={() => {}}>
97+
<h1>Test</h1>
98+
</BaseScrollArea>
99+
);
100+
101+
expect(true).toBeTruthy();
102+
React.createRef = rCreateRefTemp;
103+
});
82104
});

src/tests/ScrollTo.spec.jsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import React from "react";
22
import { render, cleanup, fireEvent } from "react-testing-library";
33
import { shallow } from "enzyme";
4-
import ScrollTo from "../ScrollTo";
54
import createReactContext from "create-react-context";
6-
7-
const { findDOMNode } = jest.requireMock("react-dom");
5+
import ScrollTo from "../ScrollTo";
86

97
afterEach(cleanup);
108

@@ -177,7 +175,7 @@ describe("Test render prop.", () => {
177175
]);
178176
});
179177

180-
it("Should scroll by ref when provided", () => {
178+
it("Should scroll by ref when a DOM node is provided", () => {
181179
const refDOM = React.createRef ? React.createRef() : createReactContext();
182180
const { getByText } = render(
183181
<ScrollTo>
@@ -202,6 +200,34 @@ describe("Test render prop.", () => {
202200
expect(refDOM.current).toMatchSnapshot();
203201
});
204202

203+
it("Should scroll by ref when a React node is provided", () => {
204+
const refDOM = {};
205+
const MyElement = ({ children, ...props }) => (
206+
<div {...props}>{children}</div>
207+
);
208+
209+
const { getByText } = render(
210+
<ScrollTo>
211+
{({ scrollTo }) => (
212+
<>
213+
<MyElement
214+
onClick={() => scrollTo({ ref: refDOM, x: 100, y: 200 })}
215+
>
216+
myBtn
217+
</MyElement>
218+
219+
<div ref={() => {}}>test</div>
220+
</>
221+
)}
222+
</ScrollTo>
223+
);
224+
225+
const buttonEl = getByText("myBtn");
226+
fireEvent.click(buttonEl);
227+
228+
expect(refDOM.current).toMatchSnapshot();
229+
});
230+
205231
it("Should handle invalid ref", () => {
206232
const refDOM = {};
207233
const { getByText } = render(
@@ -230,7 +256,7 @@ describe("Test render prop.", () => {
230256

231257
it("Should handle using the relative() function", () => {
232258
const refDOM = React.createRef ? React.createRef() : createReactContext();
233-
const { getByText, baseElement } = render(
259+
const { getByText } = render(
234260
<ScrollTo>
235261
{({ scrollTo, relative }) => (
236262
<>

src/tests/__snapshots__/ScrollTo.spec.jsx.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ Object {
2626
}
2727
`;
2828

29-
exports[`Test render prop. Should scroll by ref when provided 1`] = `
29+
exports[`Test render prop. Should scroll by ref when a DOM node is provided 1`] = `
3030
<div>
3131
test
3232
</div>
3333
`;
3434

35+
exports[`Test render prop. Should scroll by ref when a React node is provided 1`] = `undefined`;
36+
3537
exports[`Test render prop. Should update scroll position of ScrollArea's if present, rather than window 1`] = `
3638
Object {
3739
"scrollLeft": 100,

0 commit comments

Comments
 (0)