jazz-tools 0.15.9 → 0.15.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +38 -38
- package/CHANGELOG.md +20 -0
- package/dist/{chunk-5PFEKHX5.js → chunk-MLLG3GBR.js} +422 -303
- package/dist/chunk-MLLG3GBR.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/testing.js +2 -0
- package/dist/react/testing.js.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/coValues/inbox.d.ts.map +1 -1
- package/dist/tools/coValues/schemaUnion.d.ts +27 -1
- package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts +1 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +6 -3
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +6 -25
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +30 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -3
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -3
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +11 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -3
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +5 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +5 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +5 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +7 -12
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts +6 -6
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts +8 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts +18 -13
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
- package/dist/tools/internal.d.ts +1 -0
- package/dist/tools/internal.d.ts.map +1 -1
- package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts +2 -0
- package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts.map +1 -0
- package/dist/tools/tests/coDiscriminatedUnion.test.d.ts +2 -0
- package/dist/tools/tests/coDiscriminatedUnion.test.d.ts.map +1 -0
- package/dist/tools/tests/coOptional.test.d.ts +2 -0
- package/dist/tools/tests/coOptional.test.d.ts.map +1 -0
- package/dist/tools/tests/zod.test-d.d.ts +2 -0
- package/dist/tools/tests/zod.test-d.d.ts.map +1 -0
- package/package.json +5 -4
- package/src/react-core/tests/useAccount.test.ts +1 -11
- package/src/tools/coValues/CoValueBase.ts +2 -2
- package/src/tools/coValues/inbox.ts +0 -2
- package/src/tools/coValues/schemaUnion.ts +62 -0
- package/src/tools/implementation/zodSchema/coExport.ts +2 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +29 -12
- package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +93 -63
- package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +62 -3
- package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +90 -0
- package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +27 -2
- package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +27 -5
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +60 -5
- package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +33 -0
- package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +3 -5
- package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +32 -2
- package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +30 -2
- package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +26 -2
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +10 -13
- package/src/tools/implementation/zodSchema/unionUtils.ts +22 -14
- package/src/tools/implementation/zodSchema/zodCo.ts +68 -256
- package/src/tools/implementation/zodSchema/zodReExport.ts +35 -5
- package/src/tools/implementation/zodSchema/zodSchema.ts +78 -19
- package/src/tools/internal.ts +1 -0
- package/src/tools/subscribe/SubscriptionScope.ts +1 -2
- package/src/tools/tests/ContextManager.test.ts +5 -6
- package/src/tools/tests/coDiscriminatedUnion.test-d.ts +57 -0
- package/src/tools/tests/coDiscriminatedUnion.test.ts +93 -0
- package/src/tools/tests/coList.test.ts +4 -12
- package/src/tools/tests/coMap.record.test.ts +4 -4
- package/src/tools/tests/coMap.test-d.ts +3 -3
- package/src/tools/tests/coMap.test.ts +10 -10
- package/src/tools/tests/coOptional.test.ts +39 -0
- package/src/tools/tests/createContext.test.ts +5 -4
- package/src/tools/tests/deepLoading.test.ts +9 -9
- package/src/tools/tests/inbox.test.ts +3 -11
- package/src/tools/tests/patterns/requestToJoin.test.ts +1 -1
- package/src/tools/tests/schemaUnion.test.ts +11 -22
- package/src/tools/tests/subscribe.test.ts +25 -39
- package/src/tools/tests/zod.test-d.ts +70 -0
- package/src/tools/tests/zod.test.ts +64 -5
- package/dist/chunk-5PFEKHX5.js.map +0 -1
@@ -0,0 +1,57 @@
|
|
1
|
+
import { beforeEach, describe, test } from "vitest";
|
2
|
+
import { Loaded, co, z } from "../exports.js";
|
3
|
+
import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
|
4
|
+
|
5
|
+
describe("co.discriminatedUnion", () => {
|
6
|
+
beforeEach(async () => {
|
7
|
+
await setupJazzTestSync();
|
8
|
+
|
9
|
+
await createJazzTestAccount({
|
10
|
+
isCurrentActiveAccount: true,
|
11
|
+
creationProps: { name: "Hermes Puggington" },
|
12
|
+
});
|
13
|
+
});
|
14
|
+
|
15
|
+
test("can use co.discriminatedUnion with CoValue schemas as values", () => {
|
16
|
+
const Dog = co.map({
|
17
|
+
type: z.literal("dog"),
|
18
|
+
});
|
19
|
+
const Cat = co.map({
|
20
|
+
type: z.literal("cat"),
|
21
|
+
});
|
22
|
+
const Person = co.map({
|
23
|
+
pet: co.discriminatedUnion("type", [Dog, Cat]),
|
24
|
+
});
|
25
|
+
|
26
|
+
const person = Person.create({
|
27
|
+
pet: Dog.create({
|
28
|
+
type: "dog",
|
29
|
+
}),
|
30
|
+
});
|
31
|
+
|
32
|
+
person.pet = Cat.create({
|
33
|
+
type: "cat",
|
34
|
+
});
|
35
|
+
|
36
|
+
type ExpectedType = {
|
37
|
+
pet: Loaded<typeof Dog> | Loaded<typeof Cat>;
|
38
|
+
};
|
39
|
+
|
40
|
+
function matches(value: ExpectedType) {
|
41
|
+
return value;
|
42
|
+
}
|
43
|
+
|
44
|
+
matches(person);
|
45
|
+
});
|
46
|
+
|
47
|
+
test("cannot use co.discriminatedUnion with zod schemas as values", () => {
|
48
|
+
const Person = co.map({
|
49
|
+
pet: co.discriminatedUnion("type", [
|
50
|
+
// @ts-expect-error: cannot use co.discriminatedUnion with a Zod schema
|
51
|
+
z.object({
|
52
|
+
type: z.string(),
|
53
|
+
}),
|
54
|
+
]),
|
55
|
+
});
|
56
|
+
});
|
57
|
+
});
|
@@ -0,0 +1,93 @@
|
|
1
|
+
import { beforeEach, describe, expect, test, vi } from "vitest";
|
2
|
+
import { Loaded, co, z } from "../exports.js";
|
3
|
+
import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
|
4
|
+
import { waitFor } from "./utils.js";
|
5
|
+
|
6
|
+
describe("co.discriminatedUnion", () => {
|
7
|
+
beforeEach(async () => {
|
8
|
+
await setupJazzTestSync();
|
9
|
+
|
10
|
+
await createJazzTestAccount({
|
11
|
+
isCurrentActiveAccount: true,
|
12
|
+
creationProps: { name: "Hermes Puggington" },
|
13
|
+
});
|
14
|
+
});
|
15
|
+
|
16
|
+
test("use co.discriminatedUnion with CoValue schemas as values", () => {
|
17
|
+
const Dog = co.map({
|
18
|
+
type: z.literal("dog"),
|
19
|
+
});
|
20
|
+
const Cat = co.map({
|
21
|
+
type: z.literal("cat"),
|
22
|
+
});
|
23
|
+
const Person = co.map({
|
24
|
+
pet: co.discriminatedUnion("type", [Dog, Cat]),
|
25
|
+
});
|
26
|
+
|
27
|
+
const person = Person.create({
|
28
|
+
pet: Dog.create({
|
29
|
+
type: "dog",
|
30
|
+
}),
|
31
|
+
});
|
32
|
+
|
33
|
+
expect(person.pet.type).toEqual("dog");
|
34
|
+
|
35
|
+
person.pet = Cat.create({
|
36
|
+
type: "cat",
|
37
|
+
});
|
38
|
+
|
39
|
+
expect(person.pet.type).toEqual("cat");
|
40
|
+
});
|
41
|
+
|
42
|
+
test("load CoValue instances using the DiscriminatedUnion schema", async () => {
|
43
|
+
const Dog = co.map({
|
44
|
+
type: z.literal("dog"),
|
45
|
+
});
|
46
|
+
const Cat = co.map({
|
47
|
+
type: z.literal("cat"),
|
48
|
+
});
|
49
|
+
const Pet = co.discriminatedUnion("type", [Dog, Cat]);
|
50
|
+
|
51
|
+
const dog = Dog.create({ type: "dog" });
|
52
|
+
const loadedPet = await Pet.load(dog.id);
|
53
|
+
expect(loadedPet?.type).toEqual("dog");
|
54
|
+
});
|
55
|
+
|
56
|
+
test("subscribe to CoValue instances using the DiscriminatedUnion schema", async () => {
|
57
|
+
const Person = co.map({
|
58
|
+
name: z.string(),
|
59
|
+
});
|
60
|
+
const Dog = co.map({
|
61
|
+
type: z.literal("dog"),
|
62
|
+
name: z.string(),
|
63
|
+
owner: Person,
|
64
|
+
});
|
65
|
+
const Cat = co.map({
|
66
|
+
type: z.literal("cat"),
|
67
|
+
name: z.string(),
|
68
|
+
});
|
69
|
+
const Pet = co.discriminatedUnion("type", [Dog, Cat]);
|
70
|
+
|
71
|
+
const dog = Dog.create({
|
72
|
+
type: "dog",
|
73
|
+
name: "Rex",
|
74
|
+
owner: Person.create({ name: "John Doe" }),
|
75
|
+
});
|
76
|
+
|
77
|
+
const updates: Loaded<typeof Pet>[] = [];
|
78
|
+
const spy = vi.fn((pet) => updates.push(pet));
|
79
|
+
|
80
|
+
Pet.subscribe(dog.id, {}, (pet) => {
|
81
|
+
expect(pet.type).toEqual("dog");
|
82
|
+
spy(pet);
|
83
|
+
});
|
84
|
+
|
85
|
+
expect(spy).not.toHaveBeenCalled();
|
86
|
+
|
87
|
+
await waitFor(() => expect(spy).toHaveBeenCalled());
|
88
|
+
|
89
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
90
|
+
|
91
|
+
expect(updates[0]?.name).toEqual("Rex");
|
92
|
+
});
|
93
|
+
});
|
@@ -1,15 +1,7 @@
|
|
1
1
|
import { WasmCrypto } from "cojson/crypto/WasmCrypto";
|
2
2
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
3
|
-
import {
|
4
|
-
|
5
|
-
CoList,
|
6
|
-
CoMap,
|
7
|
-
Group,
|
8
|
-
coField,
|
9
|
-
subscribeToCoValue,
|
10
|
-
z,
|
11
|
-
} from "../index.js";
|
12
|
-
import { Loaded, co, zodSchemaToCoSchema } from "../internal.js";
|
3
|
+
import { Account, Group, subscribeToCoValue, z } from "../index.js";
|
4
|
+
import { Loaded, anySchemaToCoSchema, co } from "../internal.js";
|
13
5
|
import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
|
14
6
|
import { waitFor } from "./utils.js";
|
15
7
|
|
@@ -127,7 +119,7 @@ describe("Simple CoList operations", async () => {
|
|
127
119
|
name: z.string(),
|
128
120
|
});
|
129
121
|
|
130
|
-
const Recipe = co.list(
|
122
|
+
const Recipe = co.list(co.optional(Ingredient));
|
131
123
|
|
132
124
|
const recipe = Recipe.create(
|
133
125
|
[
|
@@ -588,7 +580,7 @@ describe("CoList subscription", async () => {
|
|
588
580
|
const spy = vi.fn((list) => updates.push(list));
|
589
581
|
|
590
582
|
subscribeToCoValue(
|
591
|
-
|
583
|
+
anySchemaToCoSchema(TestList),
|
592
584
|
list.id,
|
593
585
|
{
|
594
586
|
syncResolution: true,
|
@@ -281,7 +281,7 @@ describe("CoMap.Record", async () => {
|
|
281
281
|
});
|
282
282
|
|
283
283
|
test("Is ok to omit optional fields", () => {
|
284
|
-
const TestRecord = co.record(z.string(),
|
284
|
+
const TestRecord = co.record(z.string(), co.optional(NestedRecord));
|
285
285
|
|
286
286
|
expectTypeOf<typeof TestRecord.create>().toBeCallableWith(
|
287
287
|
{
|
@@ -315,7 +315,7 @@ describe("CoMap.Record", async () => {
|
|
315
315
|
|
316
316
|
const PersonRecord = co.record(
|
317
317
|
z.string(),
|
318
|
-
|
318
|
+
co.discriminatedUnion("type", [Base, IssueRepro]),
|
319
319
|
);
|
320
320
|
|
321
321
|
const person = IssueRepro.create({
|
@@ -350,7 +350,7 @@ describe("CoMap.Record", async () => {
|
|
350
350
|
|
351
351
|
const PersonRecord = co.record(
|
352
352
|
z.string(),
|
353
|
-
|
353
|
+
co.discriminatedUnion("type", [Base, IssueRepro]),
|
354
354
|
);
|
355
355
|
|
356
356
|
const person = IssueRepro.create({
|
@@ -385,7 +385,7 @@ describe("CoMap.Record", async () => {
|
|
385
385
|
|
386
386
|
const PersonRecord = co.record(
|
387
387
|
z.string(),
|
388
|
-
|
388
|
+
co.discriminatedUnion("type", [Base, IssueRepro]),
|
389
389
|
);
|
390
390
|
|
391
391
|
const person = IssueRepro.create({
|
@@ -159,7 +159,7 @@ describe("CoMap", async () => {
|
|
159
159
|
test("Comap with recursive optional reference", () => {
|
160
160
|
const Recursive = co.map({
|
161
161
|
get child(): z.ZodOptional<typeof Recursive> {
|
162
|
-
return
|
162
|
+
return co.optional(Recursive);
|
163
163
|
},
|
164
164
|
});
|
165
165
|
|
@@ -185,7 +185,7 @@ describe("CoMap", async () => {
|
|
185
185
|
age: z.number(),
|
186
186
|
// TODO: would be nice if this didn't need a type annotation
|
187
187
|
get friend(): z.ZodOptional<typeof Person> {
|
188
|
-
return
|
188
|
+
return co.optional(Person);
|
189
189
|
},
|
190
190
|
});
|
191
191
|
|
@@ -280,7 +280,7 @@ describe("CoMap", async () => {
|
|
280
280
|
|
281
281
|
const MapWithEnumOfMaps = co.map({
|
282
282
|
name: z.string(),
|
283
|
-
child:
|
283
|
+
child: co.discriminatedUnion("type", [ChildA, ChildB]),
|
284
284
|
});
|
285
285
|
|
286
286
|
const mapWithEnum = MapWithEnumOfMaps.create({
|
@@ -12,7 +12,7 @@ import {
|
|
12
12
|
} from "vitest";
|
13
13
|
import { Group, co, subscribeToCoValue, z } from "../exports.js";
|
14
14
|
import { Account } from "../index.js";
|
15
|
-
import {
|
15
|
+
import { Loaded, anySchemaToCoSchema } from "../internal.js";
|
16
16
|
import {
|
17
17
|
createJazzTestAccount,
|
18
18
|
getPeerConnectedToTestSyncServer,
|
@@ -145,7 +145,7 @@ describe("CoMap", async () => {
|
|
145
145
|
age: z.number(),
|
146
146
|
// TODO: would be nice if this didn't need a type annotation
|
147
147
|
get friend(): z.ZodOptional<typeof Person> {
|
148
|
-
return
|
148
|
+
return co.optional(Person);
|
149
149
|
},
|
150
150
|
});
|
151
151
|
|
@@ -182,7 +182,7 @@ describe("CoMap", async () => {
|
|
182
182
|
name: z.string(),
|
183
183
|
age: z.number(),
|
184
184
|
get friend(): z.ZodOptional<typeof Person> {
|
185
|
-
return
|
185
|
+
return co.optional(Person);
|
186
186
|
},
|
187
187
|
});
|
188
188
|
|
@@ -211,7 +211,7 @@ describe("CoMap", async () => {
|
|
211
211
|
name: z.string(),
|
212
212
|
age: z.number(),
|
213
213
|
get friend(): z.ZodOptional<typeof Person> {
|
214
|
-
return
|
214
|
+
return co.optional(Person);
|
215
215
|
},
|
216
216
|
});
|
217
217
|
|
@@ -400,7 +400,7 @@ describe("CoMap", async () => {
|
|
400
400
|
|
401
401
|
const MapWithEnumOfMaps = co.map({
|
402
402
|
name: z.string(),
|
403
|
-
child:
|
403
|
+
child: co.discriminatedUnion("type", [ChildA, ChildB]),
|
404
404
|
});
|
405
405
|
|
406
406
|
const mapWithEnum = MapWithEnumOfMaps.create({
|
@@ -801,7 +801,7 @@ describe("CoMap resolution", async () => {
|
|
801
801
|
const spy = vi.fn((person) => updates.push(person));
|
802
802
|
|
803
803
|
subscribeToCoValue(
|
804
|
-
|
804
|
+
anySchemaToCoSchema(Person), // TODO: we should get rid of the conversion in the future
|
805
805
|
person.id,
|
806
806
|
{
|
807
807
|
syncResolution: true,
|
@@ -1003,7 +1003,7 @@ describe("CoMap applyDiff", async () => {
|
|
1003
1003
|
birthday: z.date(),
|
1004
1004
|
nested: NestedMap,
|
1005
1005
|
optionalField: z.string().optional(),
|
1006
|
-
optionalNested:
|
1006
|
+
optionalNested: co.optional(NestedMap),
|
1007
1007
|
});
|
1008
1008
|
|
1009
1009
|
test("Basic applyDiff", () => {
|
@@ -1698,7 +1698,7 @@ describe("Creating and finding unique CoMaps", async () => {
|
|
1698
1698
|
dateValue: z.date(),
|
1699
1699
|
});
|
1700
1700
|
|
1701
|
-
const AttributeValue =
|
1701
|
+
const AttributeValue = co.discriminatedUnion("type", [
|
1702
1702
|
StringAttributeValue,
|
1703
1703
|
NumberAttributeValue,
|
1704
1704
|
DateAttributeValue,
|
@@ -1714,7 +1714,7 @@ describe("Creating and finding unique CoMaps", async () => {
|
|
1714
1714
|
attributeValue: AttributeValue,
|
1715
1715
|
});
|
1716
1716
|
|
1717
|
-
const Tag =
|
1717
|
+
const Tag = co.discriminatedUnion("type", [
|
1718
1718
|
AttributeTag,
|
1719
1719
|
StringTag,
|
1720
1720
|
DateTag,
|
@@ -1764,7 +1764,7 @@ describe("Creating and finding unique CoMaps", async () => {
|
|
1764
1764
|
error: HttpError,
|
1765
1765
|
});
|
1766
1766
|
|
1767
|
-
const ErrorResponse =
|
1767
|
+
const ErrorResponse = co.discriminatedUnion("type", [
|
1768
1768
|
ClientError,
|
1769
1769
|
ServerError,
|
1770
1770
|
NetworkError,
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { beforeEach, describe, test } from "vitest";
|
2
|
+
import { CoPlainText, co, z } from "../exports.js";
|
3
|
+
import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
|
4
|
+
|
5
|
+
describe("co.optional", () => {
|
6
|
+
beforeEach(async () => {
|
7
|
+
await setupJazzTestSync();
|
8
|
+
|
9
|
+
await createJazzTestAccount({
|
10
|
+
isCurrentActiveAccount: true,
|
11
|
+
creationProps: { name: "Hermes Puggington" },
|
12
|
+
});
|
13
|
+
});
|
14
|
+
|
15
|
+
test("can use co.optional with CoValue schemas as values", () => {
|
16
|
+
const Person = co.map({
|
17
|
+
preferredName: co.optional(co.plainText()),
|
18
|
+
});
|
19
|
+
|
20
|
+
const person = Person.create({});
|
21
|
+
|
22
|
+
type ExpectedType = {
|
23
|
+
preferredName?: CoPlainText;
|
24
|
+
};
|
25
|
+
|
26
|
+
function matches(value: ExpectedType) {
|
27
|
+
return value;
|
28
|
+
}
|
29
|
+
|
30
|
+
matches(person);
|
31
|
+
});
|
32
|
+
|
33
|
+
test("cannot use co.optional with zod schemas as values", () => {
|
34
|
+
const Person = co.map({
|
35
|
+
// @ts-expect-error: cannot use co.optional with a Zod schema
|
36
|
+
preferredName: co.optional(z.string()),
|
37
|
+
});
|
38
|
+
});
|
39
|
+
});
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { AgentSecret
|
1
|
+
import { AgentSecret } from "cojson";
|
2
2
|
import { WasmCrypto } from "cojson/crypto/WasmCrypto";
|
3
3
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
4
4
|
import {
|
@@ -9,6 +9,7 @@ import {
|
|
9
9
|
ID,
|
10
10
|
InMemoryKVStore,
|
11
11
|
KvStoreContext,
|
12
|
+
anySchemaToCoSchema,
|
12
13
|
co,
|
13
14
|
createAnonymousJazzContext,
|
14
15
|
createJazzContext,
|
@@ -90,7 +91,7 @@ describe("createContext methods", () => {
|
|
90
91
|
});
|
91
92
|
|
92
93
|
expect(context.account).toBeInstanceOf(
|
93
|
-
|
94
|
+
anySchemaToCoSchema(CustomAccount),
|
94
95
|
);
|
95
96
|
});
|
96
97
|
|
@@ -199,7 +200,7 @@ describe("createContext methods", () => {
|
|
199
200
|
});
|
200
201
|
|
201
202
|
expect(context.account).toBeInstanceOf(
|
202
|
-
|
203
|
+
anySchemaToCoSchema(CustomAccount),
|
203
204
|
);
|
204
205
|
});
|
205
206
|
|
@@ -350,7 +351,7 @@ describe("createContext methods", () => {
|
|
350
351
|
});
|
351
352
|
|
352
353
|
expect(context.account).toBeInstanceOf(
|
353
|
-
|
354
|
+
anySchemaToCoSchema(CustomAccount),
|
354
355
|
);
|
355
356
|
});
|
356
357
|
});
|
@@ -36,7 +36,7 @@ const TestList = co.list(InnerMap);
|
|
36
36
|
|
37
37
|
const TestMap = co.map({
|
38
38
|
list: TestList,
|
39
|
-
optionalRef:
|
39
|
+
optionalRef: co.optional(InnermostMap),
|
40
40
|
});
|
41
41
|
|
42
42
|
describe("Deep loading with depth arg", async () => {
|
@@ -409,7 +409,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
409
409
|
expect(mapOnAlice).toBe(null);
|
410
410
|
|
411
411
|
expect(errorSpy).toHaveBeenCalledWith(
|
412
|
-
`The current user is not authorized to access this value from ${map.id}`,
|
412
|
+
`The current user (${alice.id}) is not authorized to access this value from ${map.id}`,
|
413
413
|
);
|
414
414
|
|
415
415
|
errorSpy.mockReset();
|
@@ -431,7 +431,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
431
431
|
expect(mapWithListOnAlice).toBe(null);
|
432
432
|
|
433
433
|
expect(errorSpy).toHaveBeenCalledWith(
|
434
|
-
`The current user is not authorized to access this value from ${map.id} on path list`,
|
434
|
+
`The current user (${alice.id}) is not authorized to access this value from ${map.id} on path list`,
|
435
435
|
);
|
436
436
|
|
437
437
|
errorSpy.mockReset();
|
@@ -465,7 +465,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
465
465
|
expect(mapOnAlice).toBe(null);
|
466
466
|
|
467
467
|
expect(errorSpy).toHaveBeenCalledWith(
|
468
|
-
`The current user is not authorized to access this value from ${map.id} on path list.0`,
|
468
|
+
`The current user (${alice.id}) is not authorized to access this value from ${map.id} on path list.0`,
|
469
469
|
);
|
470
470
|
|
471
471
|
errorSpy.mockReset();
|
@@ -492,7 +492,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
492
492
|
expect(mapOnAlice?.optionalRef?.value).toBe(undefined);
|
493
493
|
|
494
494
|
expect(errorSpy).toHaveBeenCalledWith(
|
495
|
-
`The current user is not authorized to access this value from ${map.id} on path optionalRef`,
|
495
|
+
`The current user (${alice.id}) is not authorized to access this value from ${map.id} on path optionalRef`,
|
496
496
|
);
|
497
497
|
|
498
498
|
errorSpy.mockReset();
|
@@ -551,7 +551,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
551
551
|
expect(mapOnAlice).toBe(null);
|
552
552
|
|
553
553
|
expect(errorSpy).toHaveBeenCalledWith(
|
554
|
-
`The current user is not authorized to access this value from ${map.id} on path list.0.stream`,
|
554
|
+
`The current user (${alice.id}) is not authorized to access this value from ${map.id} on path list.0.stream`,
|
555
555
|
);
|
556
556
|
|
557
557
|
errorSpy.mockReset();
|
@@ -587,7 +587,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
587
587
|
expect(mapOnAlice).toBe(null);
|
588
588
|
|
589
589
|
expect(errorSpy).toHaveBeenCalledWith(
|
590
|
-
`The current user is not authorized to access this value from ${map.id} on path list.0.stream.${value.id}`,
|
590
|
+
`The current user (${alice.id}) is not authorized to access this value from ${map.id} on path list.0.stream.${value.id}`,
|
591
591
|
);
|
592
592
|
|
593
593
|
errorSpy.mockReset();
|
@@ -599,7 +599,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
599
599
|
});
|
600
600
|
|
601
601
|
const Lv2 = co.map({
|
602
|
-
lv3:
|
602
|
+
lv3: co.optional(Lv3),
|
603
603
|
});
|
604
604
|
|
605
605
|
const Lv1 = co.map({
|
@@ -737,7 +737,7 @@ describe("Deep loading with unauthorized account", async () => {
|
|
737
737
|
const Person = co.map({
|
738
738
|
name: z.string(),
|
739
739
|
get friends(): z.ZodOptional<typeof Friends> {
|
740
|
-
return
|
740
|
+
return co.optional(Friends);
|
741
741
|
},
|
742
742
|
});
|
743
743
|
const Friends: CoListSchema<typeof Person> = co.list(Person); // TODO: annoying that we have to annotate
|
@@ -1,14 +1,6 @@
|
|
1
1
|
import { describe, expect, it, vi } from "vitest";
|
2
|
-
import {
|
3
|
-
|
4
|
-
CoMap,
|
5
|
-
Group,
|
6
|
-
Inbox,
|
7
|
-
InboxSender,
|
8
|
-
Profile,
|
9
|
-
z,
|
10
|
-
} from "../exports";
|
11
|
-
import { Loaded, co, coField, zodSchemaToCoSchema } from "../internal";
|
2
|
+
import { Group, Inbox, InboxSender, z } from "../exports";
|
3
|
+
import { Loaded, anySchemaToCoSchema, co } from "../internal";
|
12
4
|
import { setupTwoNodes, waitFor } from "./utils";
|
13
5
|
|
14
6
|
const Message = co.map({
|
@@ -26,7 +18,7 @@ describe("Inbox", () => {
|
|
26
18
|
|
27
19
|
const { clientAccount: sender, serverAccount: receiver } =
|
28
20
|
await setupTwoNodes({
|
29
|
-
ServerAccountSchema:
|
21
|
+
ServerAccountSchema: anySchemaToCoSchema(WorkerAccount),
|
30
22
|
});
|
31
23
|
|
32
24
|
await expect(() => InboxSender.load(receiver.id, sender)).rejects.toThrow(
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { assert, describe, expect, test } from "vitest";
|
2
|
-
import { Account,
|
2
|
+
import { Account, Group, co, z } from "../../exports";
|
3
3
|
import { createJazzTestAccount, linkAccounts } from "../../testing";
|
4
4
|
|
5
5
|
const RequestToJoin = co.map({
|
@@ -6,7 +6,6 @@ import {
|
|
6
6
|
Loaded,
|
7
7
|
anySchemaToCoSchema,
|
8
8
|
co,
|
9
|
-
loadCoValue,
|
10
9
|
subscribeToCoValue,
|
11
10
|
z,
|
12
11
|
} from "../exports.js";
|
@@ -24,7 +23,7 @@ const BlueButtonWidget = co.map({
|
|
24
23
|
blueness: z.number(),
|
25
24
|
});
|
26
25
|
|
27
|
-
const ButtonWidget =
|
26
|
+
const ButtonWidget = co.discriminatedUnion("type", [
|
28
27
|
RedButtonWidget,
|
29
28
|
BlueButtonWidget,
|
30
29
|
]);
|
@@ -40,7 +39,7 @@ const CheckboxWidget = co.map({
|
|
40
39
|
checked: z.boolean(),
|
41
40
|
});
|
42
41
|
|
43
|
-
const WidgetUnion =
|
42
|
+
const WidgetUnion = co.discriminatedUnion("type", [
|
44
43
|
ButtonWidget,
|
45
44
|
SliderWidget,
|
46
45
|
CheckboxWidget,
|
@@ -72,25 +71,15 @@ describe("SchemaUnion", () => {
|
|
72
71
|
{ owner: me },
|
73
72
|
);
|
74
73
|
|
75
|
-
const loadedButtonWidget = await
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
sliderWidget.id,
|
85
|
-
{
|
86
|
-
loadAs: me,
|
87
|
-
},
|
88
|
-
);
|
89
|
-
const loadedCheckboxWidget = await loadCoValue(
|
90
|
-
anySchemaToCoSchema(WidgetUnion),
|
91
|
-
checkboxWidget.id,
|
92
|
-
{ loadAs: me },
|
93
|
-
);
|
74
|
+
const loadedButtonWidget = await WidgetUnion.load(buttonWidget.id, {
|
75
|
+
loadAs: me,
|
76
|
+
});
|
77
|
+
const loadedSliderWidget = await WidgetUnion.load(sliderWidget.id, {
|
78
|
+
loadAs: me,
|
79
|
+
});
|
80
|
+
const loadedCheckboxWidget = await WidgetUnion.load(checkboxWidget.id, {
|
81
|
+
loadAs: me,
|
82
|
+
});
|
94
83
|
|
95
84
|
expect(loadedButtonWidget?.type).toBe("button");
|
96
85
|
expect(loadedSliderWidget?.type).toBe("slider");
|