|
| 1 | +import React from "react"; |
| 2 | +import { render } from "@testing-library/react"; |
| 3 | +import { mount } from "enzyme"; |
| 4 | +import WithKeyboardNavigation from "../WithKeyboardNavigation"; |
| 5 | +import useNavigation from "../../../hooks/useNavigation/useNavigation"; |
| 6 | + |
| 7 | +jest.mock("../../../hooks/useNavigation/useNavigation"); |
| 8 | + |
| 9 | +describe("WithKeyboardNavigation HOC", () => { |
| 10 | + describe("user behavior", () => { |
| 11 | + let getByText; |
| 12 | + |
| 13 | + it("renders wrapped component inner content", () => { |
| 14 | + const FakeComponent: React.FC = () => <div>fake component</div>; |
| 15 | + const KeyboardNavigationEnhancedComponent: React.ComponentType = WithKeyboardNavigation( |
| 16 | + FakeComponent, |
| 17 | + ); |
| 18 | + |
| 19 | + const wrapper = render(<KeyboardNavigationEnhancedComponent />); |
| 20 | + |
| 21 | + getByText = wrapper.getByText; |
| 22 | + |
| 23 | + expect(getByText(/fake component/)).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe("implementation", () => { |
| 28 | + it("uses navigation hook", () => { |
| 29 | + const FakeComponent: React.FC = () => null; |
| 30 | + const WrappedComponent: React.ComponentType = WithKeyboardNavigation(FakeComponent); |
| 31 | + |
| 32 | + mount(<WrappedComponent />); |
| 33 | + |
| 34 | + expect(useNavigation).toBeCalled(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("passes all props to wrapped component", () => { |
| 38 | + const FakeComponent: React.FC<{ a: boolean; b: string }> = ({ a, b }) => { |
| 39 | + expect(a).toEqual(true); |
| 40 | + expect(b).toEqual("fake"); |
| 41 | + |
| 42 | + return null; |
| 43 | + }; |
| 44 | + |
| 45 | + const WrappedComponent: React.ComponentType<any> = WithKeyboardNavigation(FakeComponent); |
| 46 | + |
| 47 | + mount(<WrappedComponent a b="fake" />); |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
0 commit comments