@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
102 lines (81 loc) • 2.75 kB
JavaScript
jest.mock("create-react-ref/lib/createRef", () => jest.fn());
import * as React from "react";
import * as helpers from "../helpers";
import {
ClassComponent,
StatelessComponent,
componentDidMount,
} from "./fixtures";
const {
attachLifeCycleMethods,
createReactRef,
mergeChildrenProps,
} = require("../index");
const createRef = require("create-react-ref/lib/createRef");
describe("attachLifeCycleMethods", () => {
let addLifeCycleMethodsSpy;
let wrapInClassSpy;
beforeEach(() => {
addLifeCycleMethodsSpy = jest.spyOn(helpers, "addLifeCycleMethods");
wrapInClassSpy = jest.spyOn(helpers, "wrapInClass");
});
afterEach(() => {
addLifeCycleMethodsSpy.mockRestore();
wrapInClassSpy.mockRestore();
});
describe("when component is a class", () => {
it("invokes the addLifeCycleMethods function", () => {
attachLifeCycleMethods({ componentDidMount })(ClassComponent);
expect(addLifeCycleMethodsSpy).toHaveBeenCalled();
expect(wrapInClassSpy).not.toHaveBeenCalled();
});
});
describe("when component is not a class", () => {
it("invokes the wrapInClassWithMethods function", () => {
attachLifeCycleMethods({ componentDidMount })(StatelessComponent);
expect(addLifeCycleMethodsSpy).toHaveBeenCalled();
expect(wrapInClassSpy).toHaveBeenCalled();
});
});
});
describe("createReactRef", () => {
it("calls the React.createRef function if it exists", () => {
jest.resetModules();
jest.doMock("react", () => ({
...jest.requireActual("react"),
createRef: jest.fn(),
}));
const React = require("react");
createReactRef();
expect(React.createRef).toHaveBeenCalled();
expect(createRef).not.toHaveBeenCalled();
});
it("calls the polyfill if React.createRef doesn't exist", () => {
jest.resetModules();
jest.doMock("react", () => ({
...jest.requireActual("react"),
createRef: undefined,
}));
createReactRef();
expect(createRef).toHaveBeenCalled();
});
});
describe("mergeChildrenProps", () => {
it("returns the cloned children with the merged props", () => {
const Egg = () => React.createElement("Egg");
const Ham = () => React.createElement("Ham");
const propsToMerge = { bar: "baz" };
const original = (
<Egg>
<Ham foo={1} />
<Ham foo={2} />
<Ham foo={3} />
</Egg>
).props.children;
const cloned = mergeChildrenProps(original, propsToMerge);
expect(cloned[0].type).toEqual(original[0].type);
expect(cloned[0].key).not.toEqual(original[0].key);
expect(cloned[1].props).not.toEqual(original[1].props);
expect(cloned[2].props).toEqual({ ...original[2].props, ...propsToMerge });
});
});