Presupuestador_web/client/src/app/quotes/components/QuoteItemsInlineRowActions.test.tsx

57 lines
1.7 KiB
TypeScript

import { act } from "react";
import { createRoot } from "react-dom/client";
import type { ButtonHTMLAttributes } from "react";
import { QuoteItemsInlineRowActions } from "./QuoteItemsInlineRowActions";
declare global {
var IS_REACT_ACT_ENVIRONMENT: boolean;
}
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
jest.mock("i18next", () => ({ t: (key: string) => key }));
jest.mock("@/ui", () => ({
Button: (props: ButtonHTMLAttributes<HTMLButtonElement>) => <button {...props} />,
}));
describe("QuoteItemsInlineRowActions", () => {
let container: HTMLDivElement;
let root: ReturnType<typeof createRoot>;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
act(() => root.unmount());
container.remove();
});
it("reuses table meta actions for the active row index", () => {
const insertItem = jest.fn();
const duplicateItems = jest.fn();
const deleteItems = jest.fn();
const onDelete = jest.fn();
const row = { index: 2 } as any;
const table = { options: { meta: { insertItem, duplicateItems, deleteItems } } } as any;
act(() => {
root.render(<QuoteItemsInlineRowActions row={row} table={table} onDelete={onDelete} />);
});
const buttons = Array.from(container.querySelectorAll("button"));
act(() => buttons[0].click());
act(() => buttons[1].click());
act(() => buttons[2].click());
act(() => buttons[3].click());
expect(insertItem).toHaveBeenNthCalledWith(1, 2);
expect(insertItem).toHaveBeenNthCalledWith(2, 3);
expect(duplicateItems).toHaveBeenCalledWith(2);
expect(deleteItems).toHaveBeenCalledWith(2);
expect(onDelete).toHaveBeenCalledTimes(1);
});
});