UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

119 lines (95 loc) 3.2 kB
import * as React from "react"; import { render } from "@testing-library/react-native"; import { isClassComponent, attachMethodToPrototype, wrapInClass, addLifeCycleMethods, isNilOrEmpty, } from "../helpers"; import { ClassComponent, existingMethod, ComponentWithLifeCycle, StatelessComponent, componentDidMount, COMPONENT_DID_MOUNT_METHOD, } from "./fixtures"; describe("reactUtils helpers", () => { describe("isNilOrEmpty", () => { it("should return correct output", () => { expect(isNilOrEmpty).toBeDefined(); expect(isNilOrEmpty(null)).toBeTruthy(); expect(isNilOrEmpty(undefined)).toBeTruthy(); expect(isNilOrEmpty("")).toBeTruthy(); expect(isNilOrEmpty([])).toBeTruthy(); }); }); describe("isClassComponent", () => { it("returns true if the component is a class component", () => { expect(isClassComponent(ClassComponent)).toBe(true); }); it("returns false otherwise", () => { expect(isClassComponent(StatelessComponent)).toBe(false); }); }); describe("attachMethodToPrototype", () => { const initialWrapper = render(<ClassComponent />); expect(initialWrapper.toJSON()).not.toHaveProperty( COMPONENT_DID_MOUNT_METHOD ); afterEach(() => { componentDidMount.mockClear(); }); it("attaches a lifecycle method to the component's prototype", () => { const NewComponent = attachMethodToPrototype(ClassComponent)( componentDidMount, COMPONENT_DID_MOUNT_METHOD ); const wrapper = render(<NewComponent />); expect(wrapper.toJSON(wrapper)).toMatchSnapshot(); expect(new NewComponent()).toHaveProperty(COMPONENT_DID_MOUNT_METHOD); expect(componentDidMount).toHaveBeenCalled(); }); it("preserves any existing lifecycle method", () => { const NewComponent = attachMethodToPrototype(ComponentWithLifeCycle)( componentDidMount, COMPONENT_DID_MOUNT_METHOD ); expect(componentDidMount).not.toHaveBeenCalled(); const wrapper = render(<NewComponent />); expect(wrapper.toJSON()).toMatchSnapshot(); expect(componentDidMount).toHaveBeenCalled(); expect(existingMethod).toHaveBeenCalled(); }); }); describe("wrapInClass", () => { let NewComponent; let wrapper; beforeEach(() => { NewComponent = wrapInClass(StatelessComponent); wrapper = render(<NewComponent />); }); it("wraps the stateless component in a class", () => { expect(isClassComponent(NewComponent)).toBe(true); expect(wrapper.toJSON()).toMatchSnapshot(); }); }); describe("addLifeCycleMethods", () => { let NewComponent; let wrapper; beforeEach(() => { NewComponent = addLifeCycleMethods({ componentDidMount })(ClassComponent); wrapper = render(<NewComponent />); }); afterEach(() => { componentDidMount.mockClear(); }); it("attaches the lifecycle method to the component", () => { expect(wrapper.toJSON()).toMatchSnapshot(); }); it("adds the lifecycle method to the component", () => { expect(componentDidMount).toHaveBeenCalled(); }); }); });