jazz-tools 0.7.0-alpha.27 → 0.7.0-alpha.29

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ import { expect, describe, test, beforeEach } from "vitest";
2
+
3
+ import { webcrypto } from "node:crypto";
4
+ import { Account, jazzReady, CoMap, co, Group } from "..";
5
+
6
+ if (!("crypto" in globalThis)) {
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ (globalThis as any).crypto = webcrypto;
9
+ }
10
+
11
+ beforeEach(async () => {
12
+ await jazzReady;
13
+ });
14
+
15
+ describe("Custom accounts and groups", async () => {
16
+ class CustomProfile extends CoMap<CustomProfile> {
17
+ name = co.string;
18
+ color = co.string;
19
+ }
20
+
21
+ class CustomAccount extends Account<CustomAccount> {
22
+ profile = co.ref(CustomProfile);
23
+ root = co.ref(CoMap);
24
+
25
+ migrate(creationProps?: { name: string }) {
26
+ if (creationProps) {
27
+ const profileGroup = new Group({ owner: this });
28
+ profileGroup.addMember("everyone", "reader");
29
+ this.profile = new CustomProfile(
30
+ { name: creationProps.name, color: "blue" },
31
+ { owner: this }
32
+ );
33
+ }
34
+ }
35
+ }
36
+
37
+ class CustomGroup extends Group<CustomGroup> {
38
+ profile = co.null;
39
+ root = co.null;
40
+ [co.members] = co.ref(CustomAccount);
41
+ }
42
+
43
+ type T = CustomGroup[typeof co.members];
44
+
45
+ test("Custom account and group", async () => {
46
+ const me = await CustomAccount.create({
47
+ creationProps: { name: "Hermes Puggington" },
48
+ });
49
+
50
+ expect(me.profile).toBeDefined();
51
+ expect(me.profile?.name).toBe("Hermes Puggington");
52
+ expect(me.profile?.color).toBe("blue");
53
+
54
+ const group = new CustomGroup({ owner: me });
55
+ group.addMember("everyone", "reader");
56
+
57
+ expect(group.members).toMatchObject([
58
+ { id: me.id, role: "admin" },
59
+ { id: "everyone", role: "reader" },
60
+ ]);
61
+
62
+ await new Promise<void>((resolve) => {
63
+ group.subscribe((update) => {
64
+ const meAsMember = update.members.find((member) => {
65
+ return member.id === me.id && member.account?.profile;
66
+ });
67
+ if (meAsMember) {
68
+ expect(meAsMember.account?.profile?.name).toBe(
69
+ "Hermes Puggington"
70
+ );
71
+ expect(meAsMember.account?.profile?.color).toBe("blue");
72
+ resolve();
73
+ }
74
+ });
75
+ });
76
+ });
77
+ });