|
| 1 | +import React from "react"; |
| 2 | +import { render, wait, fireEvent, cleanup } from "react-testing-library"; |
| 3 | +import { usePaginatedRequest } from "../usePaginatedRequest"; |
| 4 | + |
| 5 | +const ChildComponent = props => { |
| 6 | + return ( |
| 7 | + <div> |
| 8 | + {props.data.map(item => ( |
| 9 | + <div>{item}</div> |
| 10 | + ))} |
| 11 | + {props.hasMore && <div>hasMore</div>} |
| 12 | + <button data-testid="trigger" onClick={props.loadMore} /> |
| 13 | + </div> |
| 14 | + ); |
| 15 | +}; |
| 16 | + |
| 17 | +describe("usePaginatedRequest", () => { |
| 18 | + afterEach(cleanup); |
| 19 | + |
| 20 | + it("first load", async () => { |
| 21 | + const request = jest.fn(() => Promise.resolve([1, 2])); |
| 22 | + |
| 23 | + const Component = () => { |
| 24 | + const { data, hasMore, loadMore } = usePaginatedRequest(request, 2); |
| 25 | + |
| 26 | + return <ChildComponent data={data} hasMore={hasMore} loadMore={loadMore} />; |
| 27 | + }; |
| 28 | + |
| 29 | + const { container, rerender } = render(<Component />); |
| 30 | + |
| 31 | + await wait(() => { |
| 32 | + expect(container).toMatchSnapshot(); |
| 33 | + }); |
| 34 | + |
| 35 | + await wait(() => { |
| 36 | + rerender(<Component />); |
| 37 | + expect(request.mock.calls.length).toBe(1); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("first load (expect hasMore - false)", async () => { |
| 42 | + const request = jest.fn(() => Promise.resolve([1])); |
| 43 | + |
| 44 | + const Component = () => { |
| 45 | + const { data, hasMore, loadMore } = usePaginatedRequest(request, 2); |
| 46 | + |
| 47 | + return <ChildComponent data={data} hasMore={hasMore} loadMore={loadMore} />; |
| 48 | + }; |
| 49 | + |
| 50 | + const { container, rerender } = render(<Component />); |
| 51 | + |
| 52 | + await wait(() => { |
| 53 | + expect(container).toMatchSnapshot(); |
| 54 | + }); |
| 55 | + |
| 56 | + await wait(() => { |
| 57 | + rerender(<Component />); |
| 58 | + expect(request.mock.calls.length).toBe(1); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("call loadMore", async () => { |
| 63 | + const request = jest.fn(); |
| 64 | + request.mockReturnValueOnce(Promise.resolve([1, 2])).mockReturnValueOnce(Promise.resolve([3, 4])); |
| 65 | + |
| 66 | + const Component = () => { |
| 67 | + const { data, hasMore, loadMore } = usePaginatedRequest(request, 2); |
| 68 | + |
| 69 | + return <ChildComponent data={data} hasMore={hasMore} loadMore={loadMore} />; |
| 70 | + }; |
| 71 | + |
| 72 | + const { container, rerender, getByTestId } = render(<Component />); |
| 73 | + |
| 74 | + await wait(() => { |
| 75 | + fireEvent.click(getByTestId("trigger")); |
| 76 | + }); |
| 77 | + |
| 78 | + await wait(() => { |
| 79 | + rerender(<Component />); |
| 80 | + }); |
| 81 | + |
| 82 | + await wait(() => { |
| 83 | + expect(container).toMatchSnapshot(); |
| 84 | + expect(request.mock.calls.length).toBe(2); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it("call loadMore (expect hasMore - false)", async () => { |
| 89 | + const request = jest.fn(); |
| 90 | + request.mockReturnValueOnce(Promise.resolve([1, 2])).mockReturnValueOnce(Promise.resolve([3])); |
| 91 | + |
| 92 | + const Component = () => { |
| 93 | + const { data, hasMore, loadMore } = usePaginatedRequest(request, 2); |
| 94 | + |
| 95 | + return <ChildComponent data={data} hasMore={hasMore} loadMore={loadMore} />; |
| 96 | + }; |
| 97 | + |
| 98 | + const { container, rerender, getByTestId } = render(<Component />); |
| 99 | + |
| 100 | + await wait(() => { |
| 101 | + fireEvent.click(getByTestId("trigger")); |
| 102 | + }); |
| 103 | + |
| 104 | + await wait(() => { |
| 105 | + rerender(<Component />); |
| 106 | + }); |
| 107 | + |
| 108 | + await wait(() => { |
| 109 | + expect(container).toMatchSnapshot(); |
| 110 | + expect(request.mock.calls.length).toBe(2); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments