-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathattribute.test.ts
More file actions
134 lines (107 loc) · 3.81 KB
/
attribute.test.ts
File metadata and controls
134 lines (107 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { beforeEach, describe, expect, test, vi } from "vitest";
import { setAttributes } from "@/lib/user/attribute";
import { UpdateQueue } from "@/lib/user/update-queue";
import { delayedResult } from "@/lib/common/utils";
export const mockAttributes = {
name: "John Doe",
email: "john@example.com",
};
// Mock the UpdateQueue
vi.mock("@/lib/user/update-queue", () => ({
UpdateQueue: {
getInstance: vi.fn(() => ({
updateAttributes: vi.fn(),
processUpdates: vi.fn(),
})),
},
}));
describe("User Attributes", () => {
const mockUpdateQueue = {
updateAttributes: vi.fn(),
processUpdates: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
const getInstanceUpdateQueue = vi.spyOn(UpdateQueue, "getInstance");
getInstanceUpdateQueue.mockReturnValue(
mockUpdateQueue as unknown as UpdateQueue
);
});
describe("setAttributes", () => {
test("successfully updates attributes and triggers processing", async () => {
const result = await setAttributes(mockAttributes);
// Verify UpdateQueue methods were called correctly
expect(mockUpdateQueue.updateAttributes).toHaveBeenCalledWith(
mockAttributes
);
expect(mockUpdateQueue.processUpdates).toHaveBeenCalled();
// Verify result is ok
expect(result.ok).toBe(true);
});
test("processes multiple attribute updates", async () => {
const firstAttributes = { name: mockAttributes.name };
const secondAttributes = { email: mockAttributes.email };
await setAttributes(firstAttributes);
await setAttributes(secondAttributes);
expect(mockUpdateQueue.updateAttributes).toHaveBeenCalledTimes(2);
expect(mockUpdateQueue.updateAttributes).toHaveBeenNthCalledWith(
1,
firstAttributes
);
expect(mockUpdateQueue.updateAttributes).toHaveBeenNthCalledWith(
2,
secondAttributes
);
expect(mockUpdateQueue.processUpdates).toHaveBeenCalledTimes(2);
});
test("processes updates asynchronously", async () => {
const attributes = { name: mockAttributes.name };
// Mock processUpdates to be async
mockUpdateQueue.processUpdates.mockImplementation(() =>
delayedResult(undefined, 100)
);
const result = await setAttributes(attributes);
expect(result.ok).toBe(true);
expect(mockUpdateQueue.processUpdates).toHaveBeenCalled();
// The function returns before processUpdates completes due to void operator
});
test("converts Date values to ISO strings", async () => {
const testDate = new Date("2026-01-15T10:30:00.000Z");
const attributes = { createdAt: testDate };
await setAttributes(attributes);
expect(mockUpdateQueue.updateAttributes).toHaveBeenCalledWith({
createdAt: "2026-01-15T10:30:00.000Z",
});
});
test("preserves number values as numbers", async () => {
const attributes = { age: 25, score: 99.5 };
await setAttributes(attributes);
expect(mockUpdateQueue.updateAttributes).toHaveBeenCalledWith({
age: 25,
score: 99.5,
});
});
test("preserves string values as strings", async () => {
const attributes = { name: "Alice", role: "admin" };
await setAttributes(attributes);
expect(mockUpdateQueue.updateAttributes).toHaveBeenCalledWith({
name: "Alice",
role: "admin",
});
});
test("normalizes mixed attribute types correctly", async () => {
const testDate = new Date("2026-06-01T00:00:00.000Z");
const attributes = {
name: "Bob",
age: 30,
joinedAt: testDate,
};
await setAttributes(attributes);
expect(mockUpdateQueue.updateAttributes).toHaveBeenCalledWith({
name: "Bob",
age: 30,
joinedAt: "2026-06-01T00:00:00.000Z",
});
});
});
});