mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-14 18:54:40 -05:00
fix tests
This commit is contained in:
@@ -90,9 +90,9 @@ describe("upsertBulkContacts", () => {
|
||||
vi.mocked(prisma.contactAttribute.findMany).mockResolvedValueOnce([]);
|
||||
// Mock: all attribute keys already exist
|
||||
const mockAttributeKeys = [
|
||||
{ id: "attr-key-email", key: "email", environmentId: mockEnvironmentId },
|
||||
{ id: "attr-key-userId", key: "userId", environmentId: mockEnvironmentId },
|
||||
{ id: "attr-key-name", key: "name", environmentId: mockEnvironmentId },
|
||||
{ id: "attr-key-email", key: "email", environmentId: mockEnvironmentId, name: "Email" },
|
||||
{ id: "attr-key-userId", key: "userId", environmentId: mockEnvironmentId, name: "User ID" },
|
||||
{ id: "attr-key-name", key: "name", environmentId: mockEnvironmentId, name: "Name" },
|
||||
];
|
||||
vi.mocked(prisma.contactAttributeKey.findMany).mockResolvedValueOnce(mockAttributeKeys);
|
||||
// Mock: no existing contacts by email
|
||||
@@ -186,7 +186,9 @@ describe("upsertBulkContacts", () => {
|
||||
// Mock: no existing userIds conflict
|
||||
vi.mocked(prisma.contactAttribute.findMany).mockResolvedValueOnce([]);
|
||||
// Mock: attribute keys for email exist
|
||||
const mockAttributeKeys = [{ id: "attr-key-email", key: "email", environmentId: mockEnvironmentId }];
|
||||
const mockAttributeKeys = [
|
||||
{ id: "attr-key-email", key: "email", environmentId: mockEnvironmentId, name: "Email" },
|
||||
];
|
||||
vi.mocked(prisma.contactAttributeKey.findMany).mockResolvedValueOnce(mockAttributeKeys);
|
||||
// Mock: an existing contact with the same email
|
||||
vi.mocked(prisma.contact.findMany).mockResolvedValueOnce([
|
||||
@@ -358,7 +360,10 @@ describe("upsertBulkContacts", () => {
|
||||
// Mock: no existing user IDs
|
||||
vi.mocked(prisma.contactAttribute.findMany).mockResolvedValueOnce([]);
|
||||
// Mock: only "email" exists; new keys are missing
|
||||
const mockAttributeKeys = [{ id: "attr-key-email", key: "email", environmentId: mockEnvironmentId }];
|
||||
const mockAttributeKeys = [
|
||||
{ id: "attr-key-email", key: "email", environmentId: mockEnvironmentId, name: "Email" },
|
||||
];
|
||||
|
||||
vi.mocked(prisma.contactAttributeKey.findMany).mockResolvedValueOnce(mockAttributeKeys);
|
||||
|
||||
// Mock: creation of new attribute keys returns new keys
|
||||
@@ -413,4 +418,87 @@ describe("upsertBulkContacts", () => {
|
||||
environmentId: mockEnvironmentId,
|
||||
});
|
||||
});
|
||||
|
||||
test("should update attribute key names when they change", async () => {
|
||||
// Mock data: a contact with an attribute that has a new name for an existing key
|
||||
const mockContacts = [
|
||||
{
|
||||
attributes: [
|
||||
{ attributeKey: { key: "email", name: "Email" }, value: "john@example.com" },
|
||||
{ attributeKey: { key: "name", name: "Full Name" }, value: "John Doe" }, // Changed name from "Name" to "Full Name"
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const mockParsedEmails = ["john@example.com"];
|
||||
|
||||
// Mock: no existing userIds conflict
|
||||
vi.mocked(prisma.contactAttribute.findMany).mockResolvedValueOnce([]);
|
||||
|
||||
// Mock: attribute keys exist but with different names
|
||||
const mockAttributeKeys = [
|
||||
{ id: "attr-key-email", key: "email", environmentId: mockEnvironmentId, name: "Email" },
|
||||
{ id: "attr-key-name", key: "name", environmentId: mockEnvironmentId, name: "Name" }, // Original name
|
||||
];
|
||||
vi.mocked(prisma.contactAttributeKey.findMany).mockResolvedValueOnce(mockAttributeKeys);
|
||||
|
||||
// Mock: an existing contact
|
||||
vi.mocked(prisma.contact.findMany).mockResolvedValueOnce([
|
||||
{
|
||||
id: "existing-contact-id",
|
||||
attributes: [
|
||||
{
|
||||
id: "existing-email-attr",
|
||||
attributeKey: { key: "email", name: "Email" },
|
||||
value: "john@example.com",
|
||||
createdAt: new Date("2023-01-01"),
|
||||
},
|
||||
{
|
||||
id: "existing-name-attr",
|
||||
attributeKey: { key: "name", name: "Name" },
|
||||
value: "John Doe",
|
||||
createdAt: new Date("2023-01-01"),
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
// Mock the transaction
|
||||
const mockTransaction = {
|
||||
contactAttributeKey: {
|
||||
createManyAndReturn: vi.fn().mockResolvedValue([]),
|
||||
update: vi.fn().mockResolvedValue({ id: "attr-key-name", key: "name", name: "Full Name" }),
|
||||
},
|
||||
contact: {
|
||||
createMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
},
|
||||
$executeRaw: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
};
|
||||
|
||||
vi.mocked(prisma.$transaction).mockImplementationOnce((callback) => {
|
||||
return callback(mockTransaction as any);
|
||||
});
|
||||
|
||||
// Execute the function
|
||||
const result = await upsertBulkContacts(mockContacts, mockEnvironmentId, mockParsedEmails);
|
||||
|
||||
if (!result.ok) throw new Error("Expected result.ok to be true");
|
||||
expect(result.data).toEqual({ contactIdxWithConflictingUserIds: [] });
|
||||
|
||||
// Verify that the attribute key name was updated
|
||||
expect(mockTransaction.contactAttributeKey.update).toHaveBeenCalledWith({
|
||||
where: {
|
||||
key_environmentId: {
|
||||
key: "name",
|
||||
environmentId: mockEnvironmentId,
|
||||
},
|
||||
},
|
||||
data: { name: "Full Name" },
|
||||
});
|
||||
|
||||
// Verify that caches were revalidated
|
||||
expect(contactAttributeKeyCache.revalidate).toHaveBeenCalledWith({
|
||||
environmentId: mockEnvironmentId,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user