|
| 1 | +import React from "react"; |
| 2 | +import { act, screen } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | +import flushPromises from "flush-promises"; |
| 6 | +import { renderWithRedux } from "../../../utils/test-utils"; |
| 7 | +import TaxTypeListPage from "../tax-type-list-page"; |
| 8 | +import { |
| 9 | + getTaxTypes, |
| 10 | + deleteTaxType, |
| 11 | + getTaxType, |
| 12 | + saveTaxType, |
| 13 | + resetTaxTypeForm, |
| 14 | + addTicketToTaxType, |
| 15 | + removeTicketFromTaxType |
| 16 | +} from "../../../actions/tax-actions"; |
| 17 | + |
| 18 | +jest.mock("../../../actions/tax-actions", () => ({ |
| 19 | + getTaxTypes: jest.fn(), |
| 20 | + deleteTaxType: jest.fn(), |
| 21 | + getTaxType: jest.fn(), |
| 22 | + saveTaxType: jest.fn(), |
| 23 | + resetTaxTypeForm: jest.fn(), |
| 24 | + addTicketToTaxType: jest.fn(), |
| 25 | + removeTicketFromTaxType: jest.fn() |
| 26 | +})); |
| 27 | + |
| 28 | +jest.mock("../../../routes/restrict", () => ({ |
| 29 | + __esModule: true, |
| 30 | + default: (WrappedComponent) => WrappedComponent |
| 31 | +})); |
| 32 | + |
| 33 | +jest.mock("react-breadcrumbs", () => ({ |
| 34 | + Breadcrumb: () => null |
| 35 | +})); |
| 36 | + |
| 37 | +jest.mock("openstack-uicore-foundation/lib/components/mui/table", () => ({ |
| 38 | + __esModule: true, |
| 39 | + default: ({ onEdit, onDelete }) => ( |
| 40 | + <div> |
| 41 | + <button |
| 42 | + type="button" |
| 43 | + onClick={() => onEdit({ id: 1, name: "VAT", rate: 20, tax_id: "V1" })} |
| 44 | + > |
| 45 | + edit-row |
| 46 | + </button> |
| 47 | + <button type="button" onClick={() => onDelete(1)}> |
| 48 | + delete-row |
| 49 | + </button> |
| 50 | + </div> |
| 51 | + ) |
| 52 | +})); |
| 53 | + |
| 54 | +jest.mock( |
| 55 | + "openstack-uicore-foundation/lib/components/mui/search-input", |
| 56 | + () => ({ |
| 57 | + __esModule: true, |
| 58 | + default: () => <input placeholder="search-tax-types" /> |
| 59 | + }) |
| 60 | +); |
| 61 | + |
| 62 | +jest.mock("../popup/tax-type-popup", () => ({ |
| 63 | + __esModule: true, |
| 64 | + default: ({ onSubmit }) => ( |
| 65 | + <div data-testid="tax-type-popup"> |
| 66 | + <button |
| 67 | + type="button" |
| 68 | + onClick={() => onSubmit({ name: "New Tax", rate: 10, tax_id: "NT1" })} |
| 69 | + > |
| 70 | + popup-save |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + ) |
| 74 | +})); |
| 75 | + |
| 76 | +jest.mock("i18n-react/dist/i18n-react", () => ({ |
| 77 | + __esModule: true, |
| 78 | + default: { translate: (key) => key } |
| 79 | +})); |
| 80 | + |
| 81 | +const initialState = { |
| 82 | + currentSummitState: { |
| 83 | + currentSummit: { id: 1 } |
| 84 | + }, |
| 85 | + currentTaxTypeListState: { |
| 86 | + taxTypes: [{ id: 1, name: "VAT", rate: 20, tax_id: "V1" }], |
| 87 | + totalTaxTypes: 1, |
| 88 | + perPage: 10, |
| 89 | + currentPage: 1, |
| 90 | + term: "", |
| 91 | + order: "name", |
| 92 | + orderDir: 1 |
| 93 | + }, |
| 94 | + currentTaxTypeState: { |
| 95 | + entity: { id: 0, name: "", rate: "", tax_id: "", ticket_types: [] }, |
| 96 | + errors: {} |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +describe("TaxTypeListPage", () => { |
| 101 | + beforeEach(() => { |
| 102 | + jest.clearAllMocks(); |
| 103 | + getTaxTypes.mockReturnValue(() => Promise.resolve()); |
| 104 | + deleteTaxType.mockReturnValue(() => Promise.resolve()); |
| 105 | + saveTaxType.mockReturnValue(() => Promise.resolve()); |
| 106 | + getTaxType.mockReturnValue(() => Promise.resolve()); |
| 107 | + resetTaxTypeForm.mockReturnValue({ type: "RESET_TAX_TYPE_FORM" }); |
| 108 | + addTicketToTaxType.mockReturnValue(() => Promise.resolve()); |
| 109 | + removeTicketFromTaxType.mockReturnValue(() => Promise.resolve()); |
| 110 | + }); |
| 111 | + |
| 112 | + it("reloads the list after a successful save", async () => { |
| 113 | + renderWithRedux(<TaxTypeListPage match={{ url: "/taxes" }} />, { |
| 114 | + initialState |
| 115 | + }); |
| 116 | + |
| 117 | + await userEvent.click( |
| 118 | + screen.getByRole("button", { name: "tax_type_list.add_tax_type" }) |
| 119 | + ); |
| 120 | + expect(screen.getByTestId("tax-type-popup")).toBeInTheDocument(); |
| 121 | + |
| 122 | + await act(async () => { |
| 123 | + await userEvent.click(screen.getByRole("button", { name: "popup-save" })); |
| 124 | + await flushPromises(); |
| 125 | + }); |
| 126 | + |
| 127 | + // Call 1: useEffect on mount; call 2: handleSave .then() |
| 128 | + expect(getTaxTypes).toHaveBeenCalledTimes(2); |
| 129 | + }); |
| 130 | + |
| 131 | + it("reloads the list after a successful delete", async () => { |
| 132 | + renderWithRedux(<TaxTypeListPage match={{ url: "/taxes" }} />, { |
| 133 | + initialState |
| 134 | + }); |
| 135 | + |
| 136 | + await act(async () => { |
| 137 | + await userEvent.click(screen.getByRole("button", { name: "delete-row" })); |
| 138 | + await flushPromises(); |
| 139 | + }); |
| 140 | + |
| 141 | + // Call 1: useEffect on mount; call 2: handleDelete .finally() |
| 142 | + expect(getTaxTypes).toHaveBeenCalledTimes(2); |
| 143 | + }); |
| 144 | + |
| 145 | + it("re-syncs the list after a failed delete", async () => { |
| 146 | + deleteTaxType.mockReturnValue(() => |
| 147 | + Promise.reject(new Error("delete failed")) |
| 148 | + ); |
| 149 | + |
| 150 | + renderWithRedux(<TaxTypeListPage match={{ url: "/taxes" }} />, { |
| 151 | + initialState |
| 152 | + }); |
| 153 | + |
| 154 | + await act(async () => { |
| 155 | + await userEvent.click(screen.getByRole("button", { name: "delete-row" })); |
| 156 | + await flushPromises(); |
| 157 | + }); |
| 158 | + |
| 159 | + // Call 1: useEffect on mount; call 2: handleDelete .finally() (fires even on rejection) |
| 160 | + expect(getTaxTypes).toHaveBeenCalledTimes(2); |
| 161 | + }); |
| 162 | +}); |
0 commit comments