Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/setup.test.ts
2500 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Organization } from "@gitpod/public-api/lib/gitpod/v1/organization_pb";
8
import { Timestamp } from "@bufbuild/protobuf";
9
import { hydrate, dehydrate } from "./setup";
10
11
test("set and get proto message", async () => {
12
const now = new Date();
13
const org = new Organization({
14
creationTime: Timestamp.fromDate(now),
15
id: "test-id",
16
name: "test-name",
17
slug: "test-slug",
18
});
19
20
expect(rehydrate(org).creationTime!.toDate()).toStrictEqual(now);
21
expect(rehydrate([org])[0].creationTime!.toDate()).toStrictEqual(now);
22
expect(rehydrate({ foo: org }).foo.creationTime!.toDate()).toStrictEqual(now);
23
});
24
25
test("set and get proto message that include | in the message value", async () => {
26
const now = new Date();
27
const org = new Organization({
28
creationTime: Timestamp.fromDate(now),
29
id: "test-id",
30
name: "test-name|more",
31
slug: "test-slug",
32
});
33
34
expect(rehydrate(org).creationTime!.toDate()).toStrictEqual(now);
35
expect(rehydrate([org])[0].creationTime!.toDate()).toStrictEqual(now);
36
expect(rehydrate({ foo: org }).foo.creationTime!.toDate()).toStrictEqual(now);
37
expect(rehydrate(org).name).toStrictEqual("test-name|more");
38
});
39
40
function rehydrate<T>(obj: T): T {
41
const dehydrated = dehydrate(obj);
42
const str = JSON.stringify(dehydrated);
43
const fromStorage = JSON.parse(str);
44
const hydrated = hydrate(fromStorage);
45
return hydrated;
46
}
47
48