UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

66 lines (53 loc) 1.92 kB
import * as React from "react"; import * as helpers from "../helpers"; import { ClassComponent, StatelessComponent, componentDidMount, } from "./fixtures"; import { attachLifeCycleMethods, mergeChildrenProps } from ".."; 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("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 }); }); });