Files
calendar/packages/features/attributes/lib/getAttributes.test.ts
T
Benny JooGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f5d345b133 refactor: remove @calcom/web imports from @calcom/features and add @calcom/testing package (#26480)
* fix: remove @calcom/web imports from packages/features to eliminate circular dependency

- Migrate UserTableUser and MemberPermissions types to packages/features/users/types/user-table.ts
- Migrate useGeo hook to packages/features/geo/GeoContext.tsx
- Migrate buildLegacyRequest to packages/lib/buildLegacyCtx.ts
- Migrate Calendar component to packages/features/calendars/weeklyview/components/
- Move test utilities (bookingScenario, fixtures) to packages/features/test/
- Update all imports in packages/features to use new locations
- Add re-exports in apps/web for backward compatibility

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: delete original implementation files and fix type issues

- Delete original calendar component files in apps/web (keep only re-export stubs)
- Migrate OutOfOfficeInSlots to packages/features/bookings/components
- Convert apps/web OutOfOfficeInSlots to re-export stub
- Fix className vs class issue in Calendar.tsx
- Fix @calcom/trpc import violation in user-table.ts by using structural type

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: add missing isGroup and contains fields to UserTableUser attributes type

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update customRole type to match actual Prisma Role model

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix build

* fix

* fix

* cleanup weeklyview

* fix

* refactor to mv MemberPermissions to types package

* add types dependency to features

* fix

* fix

* fix

* fix

* fix

* fix

* rename

* rename

* migrate

* migrate

* migrate

* fix

* fix

* fix

* refactor: move test utilities from packages/features/test to tests/libs

- Move bookingScenario utilities to tests/libs/bookingScenario
- Move fixtures to tests/libs/fixtures
- Update all imports in packages/features test files to use new location
- Update all imports in apps/web test files to use new location
- Eliminates duplication of test utilities between packages/features and apps/web

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: correct relative import paths for tests/libs in test files

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: replace test utility implementations with re-exports to tests/libs

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: fix test import paths and move signup handler tests to apps/web

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: move buildLegacyCtx to packages/lib and restore handlers to packages/features

- Move buildLegacyCtx from apps/web/lib to packages/lib to break circular dependency
- Update apps/web/lib/buildLegacyCtx.ts to re-export from @calcom/lib
- Restore signup handlers and tests to packages/features/auth/signup/handlers
- Update handler imports to use @calcom/lib/buildLegacyCtx instead of @calcom/web/lib/buildLegacyCtx

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update recurring-event.test.ts imports to use tests/libs path

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: delete test re-export files and update imports to use tests/libs directly

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update remaining test imports to use tests/libs directly

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update handleRecurringEventBooking calls to match function signature (1 arg)

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* remove

* migrate tests

* migrate tests

* refactor: update test mock imports by removing  and using async  for mock creators.

* fix type errors

* fix: add type assertion for MockUser in p2002.test-suite.ts

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: restore locale import in compareReminderBodyToTemplate.test.ts using relative path

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* feat: create @calcom/testing package and migrate tests from /tests directory

- Created new @calcom/testing package in /packages/testing
- Moved all files from /tests to /packages/testing
- Updated all imports across the codebase to use @calcom/testing alias
- Removed /tests directory at root level

This allows other packages like @calcom/features and @calcom/web to import
testing utilities using the @calcom/testing alias instead of relative paths.

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* fix

* fix: add missing useBookings export to @calcom/atoms package

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: add missing useCalendarsBusyTimes and useConnectedCalendars exports to @calcom/atoms

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* chore: add @calcom/testing as explicit devDependency to packages that use it

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: move setupVitest.ts into @calcom/testing package

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* chore: add biome rules to restrict @calcom/testing imports

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* rename libs to lib

* rename libs to lib

* add rule

* add rule

* refactor: remove @calcom/features imports from @calcom/testing

- Move mockPaymentSuccessWebhookFromStripe to fresh-booking.test.ts
- Replace ProfileRepository.generateProfileUid() with uuidv4()
- Clone Tracking type into @calcom/testing/src/lib/types.ts
- Update imports in expects.ts and getMockRequestDataForBooking.ts
- Move source files into src/ folder
- Move CalendarManager mock to @calcom/features

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: add explicit exports for nested paths in @calcom/testing

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* improve

* improve

* fix

* fix

* fix type checks

* fix type checks

* fix type checks

* fix tests

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-01-08 16:59:11 +09:00

481 lines
15 KiB
TypeScript

import prismock from "@calcom/testing/lib/__mocks__/prisma";
import { describe, expect, it, beforeEach } from "vitest";
import type { Attribute } from "@calcom/app-store/routing-forms/types/types";
import type { AttributeOption } from "@calcom/prisma/client";
import { AttributeType, MembershipRole } from "@calcom/prisma/enums";
import { getAttributesForTeam, getAttributesAssignmentData, getUsersAttributes } from "./getAttributes";
// Helper functions to create test data
async function createMockAttribute({
orgId,
options,
...attributeData
}: {
orgId: number;
id: string;
type: AttributeType;
name: string;
slug: string;
options: Pick<AttributeOption, "id" | "value" | "slug" | "contains" | "isGroup">[];
}) {
const attribute = await prismock.attribute.create({
data: {
...attributeData,
teamId: orgId,
enabled: true,
usersCanEditRelation: true,
isWeightsEnabled: false,
createdAt: new Date(),
updatedAt: new Date(),
},
});
await prismock.attributeOption.createMany({
data: options.map((option) => ({
...option,
attributeId: attribute.id,
})),
});
const result = await prismock.attribute.findUnique({
where: { id: attribute.id },
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
include: { options: true },
});
if (!result) {
throw new Error("Some error in creating mock attribute");
}
return result;
}
async function createMockUserHavingMembershipWithBothTeamAndOrg({
orgId,
teamId,
}: {
orgId: number;
teamId: number;
}) {
const user = await prismock.user.create({
data: {
name: "Test User",
email: "test@test.com",
},
});
// Create org membership
const orgMembership = await prismock.membership.create({
data: {
role: MembershipRole.MEMBER,
disableImpersonation: false,
accepted: true,
teamId: orgId,
userId: user.id,
},
});
// Create team membership
const teamMembership = await prismock.membership.create({
data: {
role: MembershipRole.MEMBER,
disableImpersonation: false,
accepted: true,
teamId: teamId,
userId: user.id,
},
});
return { user, teamMembership, orgMembership };
}
async function createMockTeam({ orgId }: { orgId: number | null }) {
return await prismock.team.create({
data: {
name: "Test Team",
slug: "test-team",
parentId: orgId,
},
});
}
async function createMockAttributeAssignment({
orgMembershipId,
attributeOptionId,
}: {
orgMembershipId: number;
attributeOptionId: string;
}) {
return await prismock.attributeToUser.create({
data: {
memberId: orgMembershipId,
attributeOptionId,
},
});
}
async function expectAttributeToMatch(attribute: Attribute, expected: Attribute) {
expect(attribute).toMatchObject({
id: expected.id,
name: expected.name,
slug: expected.slug,
type: expected.type,
});
}
async function expectAttributesToMatch(attributes: Attribute[], expected: Attribute[]) {
expect(attributes).toHaveLength(expected.length);
for (let i = 0; i < attributes.length; i++) {
expectAttributeToMatch(attributes[i], expected[i]);
}
}
describe("getAttributes", () => {
const orgId = 1001;
beforeEach(async () => {
// @ts-expect-error reset is a method on Prismock
await prismock.reset();
});
describe("getAttributesForTeam", () => {
it("should return empty array for team without parent (non-org team)", async () => {
const teamWithoutParent = await createMockTeam({ orgId: null });
await createMockAttribute({
orgId,
id: "attr1",
name: "Department",
slug: "department",
type: AttributeType.SINGLE_SELECT,
options: [
{ id: "opt1", value: "Engineering", slug: "engineering", isGroup: false, contains: [] },
{ id: "opt2", value: "Sales", slug: "sales", isGroup: false, contains: [] },
],
});
const attributes = await getAttributesForTeam({ teamId: teamWithoutParent.id });
expect(attributes).toHaveLength(0);
});
it("should return attributes assigned to team members", async () => {
const team = await createMockTeam({ orgId });
const { teamMembership } = await createMockUserHavingMembershipWithBothTeamAndOrg({
orgId,
teamId: team.id,
});
const attribute = await createMockAttribute({
orgId,
id: "attr1",
name: "Department",
slug: "department",
type: AttributeType.SINGLE_SELECT,
options: [
{ id: "opt1", value: "Engineering", slug: "engineering", isGroup: false, contains: [] },
{ id: "opt2", value: "Sales", slug: "sales", isGroup: false, contains: [] },
],
});
await createMockAttributeAssignment({
orgMembershipId: teamMembership.id,
attributeOptionId: "opt1",
});
const attributes = await getAttributesForTeam({ teamId: team.id });
expect(attributes).toHaveLength(1);
expect(attributes[0]).toMatchObject({
id: attribute.id,
name: attribute.name,
type: attribute.type,
});
});
});
describe("getAttributesAssignmentData", () => {
it("should return team members with their assigned attributes", async () => {
const team = await createMockTeam({ orgId });
const { user, orgMembership } = await createMockUserHavingMembershipWithBothTeamAndOrg({
orgId,
teamId: team.id,
});
const attr1 = await createMockAttribute({
orgId,
id: "attr1",
name: "Department",
slug: "department",
type: AttributeType.MULTI_SELECT,
options: [
{ id: "engineering-id", value: "Engineering", slug: "engineering", isGroup: false, contains: [] },
{ id: "sales-id", value: "Sales", slug: "sales", isGroup: false, contains: [] },
{
id: "engineering-and-sales-id",
value: "EngineeringAndSales",
slug: "engineering-and-sales",
isGroup: true,
contains: ["engineering-id", "sales-id"],
},
],
});
const attr2 = await createMockAttribute({
orgId,
id: "attr2",
name: "Territory",
slug: "territory",
type: AttributeType.SINGLE_SELECT,
options: [
{ id: "india-id", value: "India", slug: "india", isGroup: false, contains: [] },
{ id: "china-id", value: "China", slug: "china", isGroup: false, contains: [] },
],
});
// Unassigned attribute
const attr3 = await createMockAttribute({
orgId,
id: "attr3",
name: "Attr3",
slug: "attr3",
type: AttributeType.SINGLE_SELECT,
options: [
{ id: "opt1", value: "Opt1", slug: "opt1", isGroup: false, contains: [] },
{ id: "opt2", value: "Opt2", slug: "opt2", isGroup: false, contains: [] },
],
});
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
// Assigning a group option that has its sub-options not assigned directly to the user
attributeOptionId: "engineering-and-sales-id",
});
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
attributeOptionId: "india-id",
});
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
attributeOptionId: "engineering-id",
});
const { attributesOfTheOrg, attributesAssignedToTeamMembersWithOptions } =
await getAttributesAssignmentData({ teamId: team.id, orgId });
expect(attributesAssignedToTeamMembersWithOptions).toHaveLength(1);
expect(attributesAssignedToTeamMembersWithOptions[0]).toEqual({
userId: user.id,
attributes: {
attr1: {
type: AttributeType.MULTI_SELECT,
attributeOption: [
{
isGroup: true,
value: "EngineeringAndSales",
contains: [
{
id: "engineering-id",
slug: "engineering",
value: "Engineering",
},
{ id: "sales-id", slug: "sales", value: "Sales" },
],
},
{
isGroup: false,
value: "Engineering",
contains: [],
},
],
},
attr2: {
type: AttributeType.SINGLE_SELECT,
attributeOption: { isGroup: false, value: "India", contains: [] },
},
// attr3 - unassigned isn't here
},
});
expectAttributesToMatch(attributesOfTheOrg, [attr1, attr2, attr3]);
});
it("should return contains correctly for group options that have sub-options not assigned directly to the user", async () => {
const team = await createMockTeam({ orgId });
const { user, orgMembership } = await createMockUserHavingMembershipWithBothTeamAndOrg({
orgId,
teamId: team.id,
});
const attr1 = await createMockAttribute({
orgId,
id: "attr1",
name: "Department",
slug: "department",
type: AttributeType.MULTI_SELECT,
options: [
{ id: "engineering-id", value: "Engineering", slug: "engineering", isGroup: false, contains: [] },
{ id: "sales-id", value: "Sales", slug: "sales", isGroup: false, contains: [] },
{
id: "engineering-and-sales-id",
value: "EngineeringAndSales",
slug: "engineering-and-sales",
isGroup: true,
contains: ["engineering-id", "sales-id"],
},
],
});
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
// Assigning a group option that has its sub-options not assigned directly to the user
attributeOptionId: "engineering-and-sales-id",
});
const { attributesOfTheOrg, attributesAssignedToTeamMembersWithOptions } =
await getAttributesAssignmentData({ teamId: team.id, orgId });
expect(attributesAssignedToTeamMembersWithOptions).toHaveLength(1);
expect(attributesAssignedToTeamMembersWithOptions[0]).toEqual({
userId: user.id,
attributes: {
attr1: {
type: AttributeType.MULTI_SELECT,
attributeOption: {
isGroup: true,
value: "EngineeringAndSales",
contains: [
{
id: "engineering-id",
slug: "engineering",
value: "Engineering",
},
{ id: "sales-id", slug: "sales", value: "Sales" },
],
},
},
},
});
expectAttributesToMatch(attributesOfTheOrg, [attr1]);
});
it("should return no attributes for a different org", async () => {
const team = await createMockTeam({ orgId });
const otherOrgId = 1002;
const otherOrgsTeam = await createMockTeam({ orgId: otherOrgId });
const { orgMembership } = await createMockUserHavingMembershipWithBothTeamAndOrg({
orgId,
teamId: team.id,
});
await createMockAttribute({
orgId,
id: "attr1",
name: "Department",
slug: "department",
type: AttributeType.MULTI_SELECT,
options: [
{ id: "engineering-id", value: "Engineering", slug: "engineering", isGroup: false, contains: [] },
{ id: "sales-id", value: "Sales", slug: "sales", isGroup: false, contains: [] },
{
id: "engineering-and-sales-id",
value: "EngineeringAndSales",
slug: "engineering-and-sales",
isGroup: true,
contains: ["engineering-id", "sales-id"],
},
],
});
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
// Assigning a group option that has its sub-options not assigned directly to the user
attributeOptionId: "engineering-and-sales-id",
});
const { attributesOfTheOrg, attributesAssignedToTeamMembersWithOptions } =
await getAttributesAssignmentData({ teamId: otherOrgsTeam.id, orgId: otherOrgId });
expect(attributesAssignedToTeamMembersWithOptions).toHaveLength(0);
// Because created attribute is of other org, it should not be returned
expect(attributesOfTheOrg).toHaveLength(0);
});
it("should handle multi-select attributes correctly", async () => {
const team = await createMockTeam({ orgId });
const { orgMembership } = await createMockUserHavingMembershipWithBothTeamAndOrg({
orgId,
teamId: team.id,
});
const attr1 = await createMockAttribute({
orgId,
id: "attr1",
name: "Skills",
slug: "skills",
type: AttributeType.MULTI_SELECT,
options: [
{ id: "opt1", value: "JavaScript", slug: "javascript", isGroup: false, contains: [] },
{ id: "opt2", value: "Python", slug: "python", isGroup: false, contains: [] },
],
});
// Assign multiple skills
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
attributeOptionId: "opt1",
});
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
attributeOptionId: "opt2",
});
const { attributesOfTheOrg, attributesAssignedToTeamMembersWithOptions } =
await getAttributesAssignmentData({ teamId: team.id, orgId });
expect(attributesAssignedToTeamMembersWithOptions).toHaveLength(1);
expect(attributesAssignedToTeamMembersWithOptions[0].attributes.attr1.attributeOption).toEqual([
{ value: "JavaScript", isGroup: false, contains: [] },
{ value: "Python", isGroup: false, contains: [] },
]);
expectAttributesToMatch(attributesOfTheOrg, [attr1]);
});
});
describe("getUsersAttributes", () => {
it("should return attributes assigned to specific user in team", async () => {
const team = await createMockTeam({ orgId });
const { user, orgMembership } = await createMockUserHavingMembershipWithBothTeamAndOrg({
orgId,
teamId: team.id,
});
const attribute = await createMockAttribute({
orgId,
id: "attr1",
name: "Department",
slug: "department",
type: AttributeType.SINGLE_SELECT,
options: [{ id: "opt1", value: "Engineering", slug: "engineering", isGroup: false, contains: [] }],
});
await createMockAttributeAssignment({
orgMembershipId: orgMembership.id,
attributeOptionId: "opt1",
});
const userAttributes = await getUsersAttributes({ userId: user.id, teamId: team.id });
expect(userAttributes).toHaveLength(1);
expect(userAttributes[0]).toMatchObject({
id: attribute.id,
name: attribute.name,
type: attribute.type,
});
});
});
});