feat: allow users to generate csv tables for org members (#17458)
* add downloadAsCsv util * add sanitizeValue util * write logic * refactor * add tests
This commit is contained in:
1 parent
06dccd7a73
commit
c850af7409
3 files changed
+311
No files matched your search
@@ -0,0 +1,202 @@
|
||||
import type { Table } from "@tanstack/react-table";
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
|
||||
import type { UserTableUser } from "@calcom/features/users/components/UserTable/types";
|
||||
import { generateCsvRawForUsersTable } from "@calcom/lib/csvUtils";
|
||||
import type { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
function createMockTable(data: UserTableUser[]): Table<UserTableUser> {
|
||||
return {
|
||||
getHeaderGroups: vi.fn().mockReturnValue([
|
||||
{
|
||||
headers: [
|
||||
{
|
||||
id: "select",
|
||||
column: { columnDef: { header: "" } },
|
||||
getContext: () => ({}),
|
||||
},
|
||||
{
|
||||
id: "member",
|
||||
column: { columnDef: { header: "Members" } },
|
||||
getContext: () => ({}),
|
||||
},
|
||||
{
|
||||
id: "role",
|
||||
column: { columnDef: { header: "Role" } },
|
||||
getContext: () => ({}),
|
||||
},
|
||||
{
|
||||
id: "teams",
|
||||
column: { columnDef: { header: "Teams" } },
|
||||
getContext: () => ({}),
|
||||
},
|
||||
{
|
||||
id: "attr1",
|
||||
column: { columnDef: { header: () => "Attribute 1" } },
|
||||
getContext: () => ({}),
|
||||
},
|
||||
{
|
||||
id: "attr2",
|
||||
column: { columnDef: { header: () => "Attribute 2" } },
|
||||
getContext: () => ({}),
|
||||
},
|
||||
{
|
||||
id: "actions",
|
||||
column: { columnDef: { header: "" } },
|
||||
getContext: () => ({}),
|
||||
},
|
||||
],
|
||||
},
|
||||
]),
|
||||
getRowModel: vi.fn().mockReturnValue({ rows: data.map((item) => ({ original: item })) }),
|
||||
} as unknown as Table<UserTableUser>;
|
||||
}
|
||||
|
||||
describe("generate Csv for Org Users Table", () => {
|
||||
const mockAttributeIds = ["attr1", "attr2"];
|
||||
const HEADER_IDS_TO_EXCLUDE = ["select", "actions"];
|
||||
const mockUser: UserTableUser = {
|
||||
id: 1,
|
||||
username: "testuser",
|
||||
email: "[email protected]",
|
||||
timeZone: "UTC",
|
||||
role: "MEMBER" as MembershipRole,
|
||||
avatarUrl: null,
|
||||
accepted: true,
|
||||
disableImpersonation: false,
|
||||
completedOnboarding: true,
|
||||
teams: [],
|
||||
attributes: [],
|
||||
};
|
||||
|
||||
it("should return null if no headers", () => {
|
||||
const mockTableNoHeaders = {
|
||||
getHeaderGroups: vi.fn().mockReturnValue([]),
|
||||
getRowModel: vi.fn().mockReturnValue({ rows: [] }),
|
||||
} as unknown as Table<UserTableUser>;
|
||||
|
||||
expect(generateCsvRawForUsersTable(mockTableNoHeaders, mockAttributeIds, [])).toBeNull();
|
||||
});
|
||||
|
||||
it("should generate correct CSV headers", () => {
|
||||
const mockTable = createMockTable([]);
|
||||
const csv = generateCsvRawForUsersTable(mockTable, mockAttributeIds, HEADER_IDS_TO_EXCLUDE);
|
||||
const headers = csv?.split("\n")[0];
|
||||
expect(headers).toBe("Members,Role,Teams,Attribute 1,Attribute 2");
|
||||
});
|
||||
|
||||
it("should handle user with single attribute value", () => {
|
||||
const mockData: UserTableUser[] = [
|
||||
{
|
||||
...mockUser,
|
||||
teams: [{ id: 1, name: "Team1", slug: "team1" }],
|
||||
attributes: [{ id: "1", attributeId: "attr1", value: "value1", slug: "slug1" }],
|
||||
},
|
||||
];
|
||||
|
||||
const mockTable = createMockTable(mockData);
|
||||
const csv = generateCsvRawForUsersTable(mockTable, mockAttributeIds, HEADER_IDS_TO_EXCLUDE);
|
||||
|
||||
expect(csv).toMatchInlineSnapshot(`
|
||||
"Members,Role,Teams,Attribute 1,Attribute 2
|
||||
[email protected],MEMBER,Team1,value1,"
|
||||
`);
|
||||
});
|
||||
|
||||
it("should handle user with multiple attribute values for same attribute", () => {
|
||||
const mockData: UserTableUser[] = [
|
||||
{
|
||||
...mockUser,
|
||||
teams: [{ id: 1, name: "Team1", slug: "team1" }],
|
||||
attributes: [
|
||||
{ id: "1", attributeId: "attr1", value: "value1", slug: "slug1" },
|
||||
{ id: "2", attributeId: "attr1", value: "value2", slug: "slug1" },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const mockTable = createMockTable(mockData);
|
||||
const csv = generateCsvRawForUsersTable(mockTable, mockAttributeIds, HEADER_IDS_TO_EXCLUDE);
|
||||
|
||||
expect(csv).toMatchInlineSnapshot(`
|
||||
"Members,Role,Teams,Attribute 1,Attribute 2
|
||||
[email protected],MEMBER,Team1,"value1,value2","
|
||||
`);
|
||||
});
|
||||
|
||||
it("should handle user with multiple teams", () => {
|
||||
const mockData: UserTableUser[] = [
|
||||
{
|
||||
...mockUser,
|
||||
teams: [
|
||||
{ id: 1, name: "Team1", slug: "team1" },
|
||||
{ id: 2, name: "Team2", slug: "team2" },
|
||||
],
|
||||
attributes: [],
|
||||
},
|
||||
];
|
||||
|
||||
const mockTable = createMockTable(mockData);
|
||||
const csv = generateCsvRawForUsersTable(mockTable, mockAttributeIds, HEADER_IDS_TO_EXCLUDE);
|
||||
|
||||
expect(csv).toMatchInlineSnapshot(`
|
||||
"Members,Role,Teams,Attribute 1,Attribute 2
|
||||
[email protected],MEMBER,"Team1,Team2",,"
|
||||
`);
|
||||
});
|
||||
|
||||
it("should handle values that need sanitization", () => {
|
||||
const mockData: UserTableUser[] = [
|
||||
{
|
||||
...mockUser,
|
||||
teams: [{ id: 1, name: "Team,1", slug: "team1" }],
|
||||
attributes: [{ id: "1", attributeId: "attr1", value: "value,1", slug: "slug1" }],
|
||||
},
|
||||
];
|
||||
|
||||
const mockTable = createMockTable(mockData);
|
||||
const csv = generateCsvRawForUsersTable(mockTable, mockAttributeIds, HEADER_IDS_TO_EXCLUDE);
|
||||
|
||||
expect(csv).toMatchInlineSnapshot(`
|
||||
"Members,Role,Teams,Attribute 1,Attribute 2
|
||||
[email protected],MEMBER,"Team,1","value,1","
|
||||
`);
|
||||
});
|
||||
|
||||
it("should handle all membership roles", () => {
|
||||
const roles: MembershipRole[] = ["OWNER", "ADMIN", "MEMBER"];
|
||||
const mockData: UserTableUser[] = roles.map((role) => ({
|
||||
...mockUser,
|
||||
role,
|
||||
email: `${role.toLowerCase()}@example.com`,
|
||||
}));
|
||||
|
||||
const mockTable = createMockTable(mockData);
|
||||
const csv = generateCsvRawForUsersTable(mockTable, mockAttributeIds, HEADER_IDS_TO_EXCLUDE);
|
||||
|
||||
expect(csv).toMatchInlineSnapshot(`
|
||||
"Members,Role,Teams,Attribute 1,Attribute 2
|
||||
[email protected],OWNER,,,
|
||||
[email protected],ADMIN,,,
|
||||
[email protected],MEMBER,,,"
|
||||
`);
|
||||
});
|
||||
|
||||
it("should handle users without teams and attributes", () => {
|
||||
const mockData: UserTableUser[] = [
|
||||
{
|
||||
...mockUser,
|
||||
username: null,
|
||||
avatarUrl: null,
|
||||
},
|
||||
];
|
||||
|
||||
const mockTable = createMockTable(mockData);
|
||||
const csv = generateCsvRawForUsersTable(mockTable, mockAttributeIds, HEADER_IDS_TO_EXCLUDE);
|
||||
|
||||
expect(csv).toMatchInlineSnapshot(`
|
||||
"Members,Role,Teams,Attribute 1,Attribute 2
|
||||
[email protected],MEMBER,,,"
|
||||
`);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user