-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathioSerialization.test.ts
More file actions
191 lines (158 loc) · 5.9 KB
/
Copy pathioSerialization.test.ts
File metadata and controls
191 lines (158 loc) · 5.9 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { replaceSuperJsonPayload } from "../src/v3/utils/ioSerialization.js";
describe("ioSerialization", () => {
describe("replaceSuperJsonPayload", () => {
it("should replace simple JSON payload while preserving SuperJSON metadata", async () => {
const originalData = {
name: "John",
age: 30,
date: new Date("2023-01-01"),
};
const superjson = await import("superjson");
const originalSerialized = superjson.stringify(originalData);
const newPayloadJson = JSON.stringify({
name: "Jane",
surname: "Doe",
age: 25,
date: "2023-02-01T00:00:00.000Z",
});
const result = (await replaceSuperJsonPayload(originalSerialized, newPayloadJson)) as any;
expect(result.name).toBe("Jane");
expect(result.surname).toBe("Doe");
expect(result.age).toBe(25);
expect(result.date).toBeInstanceOf(Date);
expect(result.date.toISOString()).toBe("2023-02-01T00:00:00.000Z");
});
// related to issue https://github.com/triggerdotdev/trigger.dev/issues/1968
it("should ignore original undefined type metadata for overriden fields", async () => {
const originalData = {
name: "John",
age: 30,
date: new Date("2023-01-01"),
country: undefined,
settings: {
theme: undefined,
},
};
const superjson = await import("superjson");
const originalSerialized = superjson.stringify(originalData);
const newPayloadJson = JSON.stringify({
name: "Jane",
surname: "Doe",
age: 25,
date: "2023-02-01T00:00:00.000Z",
country: "US",
settings: {
theme: "dark",
},
});
const result = (await replaceSuperJsonPayload(originalSerialized, newPayloadJson)) as any;
expect(result.name).toBe("Jane");
expect(result.surname).toBe("Doe");
expect(result.country).toBe("US");
expect(result.settings.theme).toBe("dark");
expect(result.age).toBe(25);
expect(result.date).toBeInstanceOf(Date);
expect(result.date.toISOString()).toBe("2023-02-01T00:00:00.000Z");
});
it("should preserve BigInt type metadata", async () => {
const originalData = {
id: BigInt(123456789),
count: 42,
};
const superjson = await import("superjson");
const originalSerialized = superjson.stringify(originalData);
const newPayloadJson = JSON.stringify({
id: "987654321",
count: 100,
});
const result = (await replaceSuperJsonPayload(originalSerialized, newPayloadJson)) as any;
expect(result.id).toBe(BigInt(987654321));
expect(typeof result.id).toBe("bigint");
expect(result.count).toBe(100);
});
it("should preserve nested type metadata", async () => {
const originalData = {
user: {
id: BigInt(123),
createdAt: new Date("2023-01-01"),
settings: {
theme: "dark",
updatedAt: new Date("2023-01-01"),
},
},
metadata: {
version: 1,
},
};
const superjson = await import("superjson");
const originalSerialized = superjson.stringify(originalData);
const newPayloadJson = JSON.stringify({
user: {
id: "456",
createdAt: "2023-06-01T00:00:00.000Z",
settings: {
theme: "light",
updatedAt: "2023-06-01T00:00:00.000Z",
},
},
metadata: {
version: 2,
},
});
const result = (await replaceSuperJsonPayload(originalSerialized, newPayloadJson)) as any;
expect(result.user.id).toBe(BigInt(456));
expect(result.user.createdAt).toBeInstanceOf(Date);
expect(result.user.createdAt.toISOString()).toBe("2023-06-01T00:00:00.000Z");
expect(result.user.settings.theme).toBe("light");
expect(result.user.settings.updatedAt).toBeInstanceOf(Date);
expect(result.user.settings.updatedAt.toISOString()).toBe("2023-06-01T00:00:00.000Z");
expect(result.metadata.version).toBe(2);
});
it("should preserve Set type metadata", async () => {
const originalData = {
tags: new Set(["tag1", "tag2"]),
name: "test",
};
const superjson = await import("superjson");
const originalSerialized = superjson.stringify(originalData);
const newPayloadJson = JSON.stringify({
tags: ["tag3", "tag4", "tag5"],
name: "updated",
});
const result = (await replaceSuperJsonPayload(originalSerialized, newPayloadJson)) as any;
expect(result.tags).toBeInstanceOf(Set);
expect(Array.from(result.tags)).toEqual(["tag3", "tag4", "tag5"]);
expect(result.name).toBe("updated");
});
it("should preserve Map type metadata", async () => {
const originalData = {
mapping: new Map([
["key1", "value1"],
["key2", "value2"],
]),
name: "test",
};
const superjson = await import("superjson");
const originalSerialized = superjson.stringify(originalData);
const newPayloadJson = JSON.stringify({
mapping: [
["key3", "value3"],
["key4", "value4"],
],
name: "updated",
});
const result = (await replaceSuperJsonPayload(originalSerialized, newPayloadJson)) as any;
expect(result.mapping).toBeInstanceOf(Map);
expect(result.mapping.get("key3")).toBe("value3");
expect(result.mapping.get("key4")).toBe("value4");
expect(result.name).toBe("updated");
});
it("should throw error for invalid JSON payload", async () => {
const originalData = { name: "test" };
const superjson = await import("superjson");
const originalSerialized = superjson.stringify(originalData);
const invalidPayload = "{ invalid json }";
await expect(replaceSuperJsonPayload(originalSerialized, invalidPayload)).rejects.toThrow();
});
});
});