Fix bulk invite template download button

This commit is contained in:
Sambhram1 2026-05-07 23:22:52 +05:30
parent fee5900acc
commit fe19e3f0bc
2 changed files with 54 additions and 2 deletions

View file

@ -1,4 +1,4 @@
import { render } from "@testing-library/react";
import { fireEvent, render } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import BulkCreateUsersButton from "./bulk_create_users_button";
@ -25,4 +25,50 @@ describe("BulkCreateUsersButton", () => {
const { getByText } = render(<BulkCreateUsersButton accessToken="test-token" teams={[]} possibleUIRoles={null} />);
expect(getByText("+ Bulk Invite Users")).toBeInTheDocument();
});
it("downloads the CSV template when clicked", async () => {
const originalCreateObjectURL = window.URL.createObjectURL;
const originalRevokeObjectURL = window.URL.revokeObjectURL;
const createObjectURLSpy = vi.fn(() => "blob:test");
const revokeObjectURLSpy = vi.fn();
const appendChildSpy = vi.spyOn(document.body, "appendChild");
const removeChildSpy = vi.spyOn(document.body, "removeChild");
const clickSpy = vi.fn();
const originalCreateElement = document.createElement.bind(document);
let createdAnchor: HTMLAnchorElement | null = null;
window.URL.createObjectURL = createObjectURLSpy;
window.URL.revokeObjectURL = revokeObjectURLSpy;
const createElementSpy = vi.spyOn(document, "createElement").mockImplementation((tagName: string) => {
if (tagName === "a") {
const anchor = originalCreateElement("a");
anchor.click = clickSpy;
createdAnchor = anchor;
return anchor;
}
return originalCreateElement(tagName);
});
const { getByText, findByText } = render(
<BulkCreateUsersButton accessToken="test-token" teams={[]} possibleUIRoles={null} />
);
fireEvent.click(getByText("+ Bulk Invite Users"));
fireEvent.click(await findByText("Download CSV Template"));
expect(createObjectURLSpy).toHaveBeenCalledOnce();
expect(clickSpy).toHaveBeenCalledOnce();
expect(createdAnchor).not.toBeNull();
expect(appendChildSpy).toHaveBeenCalledWith(createdAnchor);
expect(removeChildSpy).toHaveBeenCalledWith(createdAnchor);
expect(revokeObjectURLSpy).toHaveBeenCalledWith("blob:test");
appendChildSpy.mockRestore();
removeChildSpy.mockRestore();
createElementSpy.mockRestore();
window.URL.createObjectURL = originalCreateObjectURL;
window.URL.revokeObjectURL = originalRevokeObjectURL;
});
});

View file

@ -629,7 +629,13 @@ const BulkCreateUsersButton: React.FC<BulkCreateUsersProps> = ({
</div>
</div>
<Button type="primary" size="large" className="w-full md:w-auto" icon={<DownloadOutlined />}>
<Button
type="primary"
size="large"
className="w-full md:w-auto"
icon={<DownloadOutlined />}
onClick={downloadTemplate}
>
Download CSV Template
</Button>
</div>