jazz-tools 0.16.3 → 0.16.5

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.
@@ -2181,84 +2181,131 @@ describe("CoMap migration", () => {
2181
2181
  expect(loaded?.friend?.name).toEqual("Charlie");
2182
2182
  expect(loaded?.friend?.version).toEqual(2);
2183
2183
  });
2184
- describe("Time", () => {
2185
- test("empty map created time", () => {
2186
- const currentTimestampInSeconds = Math.floor(Date.now() / 1000);
2187
- const emptyMap = co.map({}).create({});
2188
- const createdAtInSeconds = Math.floor(emptyMap._createdAt / 1000);
2184
+ });
2185
+
2186
+ describe("createdAt & lastUpdatedAt", () => {
2187
+ test("empty map created time", () => {
2188
+ const emptyMap = co.map({}).create({});
2189
+
2190
+ expect(emptyMap._lastUpdatedAt).toEqual(emptyMap._createdAt);
2191
+ });
2189
2192
 
2190
- expect(createdAtInSeconds).toEqual(currentTimestampInSeconds);
2191
- expect(emptyMap._lastUpdatedAt).toEqual(emptyMap._createdAt);
2193
+ test("created time and last updated time", async () => {
2194
+ const Person = co.map({
2195
+ name: z.string(),
2192
2196
  });
2193
2197
 
2194
- test("created time and last updated time", async () => {
2195
- const Person = co.map({
2196
- name: z.string(),
2197
- });
2198
+ const person = Person.create({ name: "John" });
2198
2199
 
2199
- let currentTimestampInSeconds = Math.floor(Date.now() / 1000);
2200
- const person = Person.create({ name: "John" });
2200
+ const createdAt = person._createdAt;
2201
+ expect(person._lastUpdatedAt).toEqual(createdAt);
2201
2202
 
2202
- const createdAt = person._createdAt;
2203
- const createdAtInSeconds = Math.floor(createdAt / 1000);
2204
- expect(createdAtInSeconds).toEqual(currentTimestampInSeconds);
2205
- expect(person._lastUpdatedAt).toEqual(createdAt);
2203
+ await new Promise((r) => setTimeout(r, 10));
2204
+ person.name = "Jane";
2206
2205
 
2207
- await new Promise((r) => setTimeout(r, 1000));
2208
- currentTimestampInSeconds = Math.floor(Date.now() / 1000);
2209
- person.name = "Jane";
2206
+ expect(person._createdAt).toEqual(createdAt);
2207
+ expect(person._lastUpdatedAt).not.toEqual(createdAt);
2208
+ });
2209
+ });
2210
2210
 
2211
- const lastUpdatedAtInSeconds = Math.floor(person._lastUpdatedAt / 1000);
2212
- expect(lastUpdatedAtInSeconds).toEqual(currentTimestampInSeconds);
2213
- expect(person._createdAt).toEqual(createdAt);
2214
- expect(person._lastUpdatedAt).not.toEqual(createdAt);
2211
+ describe("co.map schema", () => {
2212
+ test("can access the inner schemas of a co.map", () => {
2213
+ const Person = co.map({
2214
+ name: co.plainText(),
2215
2215
  });
2216
2216
 
2217
- test("comap with custom uniqueness", () => {
2217
+ const person = Person.create({
2218
+ name: Person.shape["name"].create("John"),
2219
+ });
2220
+
2221
+ expect(person.name.toString()).toEqual("John");
2222
+ });
2223
+
2224
+ describe("pick()", () => {
2225
+ test("creates a new CoMap schema by picking fields of another CoMap schema", () => {
2218
2226
  const Person = co.map({
2219
2227
  name: z.string(),
2228
+ age: z.number(),
2220
2229
  });
2221
2230
 
2222
- let currentTimestampInSeconds = Math.floor(Date.now() / 1000);
2223
- const person = Person.create(
2224
- { name: "John" },
2225
- { unique: "name", owner: Account.getMe() },
2226
- );
2231
+ const PersonWithName = Person.pick({
2232
+ name: true,
2233
+ });
2234
+
2235
+ const person = PersonWithName.create({
2236
+ name: "John",
2237
+ });
2227
2238
 
2228
- const createdAt = person._createdAt;
2229
- const createdAtInSeconds = Math.floor(createdAt / 1000);
2230
- expect(createdAtInSeconds).toEqual(currentTimestampInSeconds);
2239
+ expect(person.name).toEqual("John");
2231
2240
  });
2232
2241
 
2233
- test("empty comap with custom uniqueness", () => {
2234
- const Person = co.map({
2235
- name: z.optional(z.string()),
2242
+ test("the new schema does not include catchall properties", () => {
2243
+ const Person = co
2244
+ .map({
2245
+ name: z.string(),
2246
+ age: z.number(),
2247
+ })
2248
+ .catchall(z.string());
2249
+
2250
+ const PersonWithName = Person.pick({
2251
+ name: true,
2236
2252
  });
2237
2253
 
2238
- let currentTimestampInSeconds = Math.floor(Date.now() / 1000);
2239
- const person = Person.create(
2240
- {},
2241
- { unique: "name", owner: Account.getMe() },
2242
- );
2254
+ expect(PersonWithName.catchAll).toBeUndefined();
2243
2255
 
2244
- const createdAt = person._createdAt;
2245
- const createdAtInSeconds = Math.floor(createdAt / 1000);
2246
- expect(createdAtInSeconds).toEqual(currentTimestampInSeconds);
2256
+ const person = PersonWithName.create({
2257
+ name: "John",
2258
+ });
2259
+ // @ts-expect-error - property `extraField` does not exist in person
2260
+ expect(person.extraField).toBeUndefined();
2247
2261
  });
2248
2262
  });
2249
- });
2250
2263
 
2251
- describe("co.map schema", () => {
2252
- test("can access the inner schemas of a co.map", () => {
2253
- const Person = co.map({
2254
- name: co.plainText(),
2255
- });
2264
+ describe("partial()", () => {
2265
+ test("creates a new CoMap schema by making all properties optional", () => {
2266
+ const Dog = co.map({
2267
+ name: z.string(),
2268
+ breed: z.string(),
2269
+ });
2270
+ const Person = co.map({
2271
+ name: z.string(),
2272
+ age: z.number(),
2273
+ pet: Dog,
2274
+ });
2256
2275
 
2257
- const person = Person.create({
2258
- name: Person.shape["name"].create("John"),
2276
+ const DraftPerson = Person.partial();
2277
+
2278
+ const draftPerson = DraftPerson.create({});
2279
+
2280
+ expect(draftPerson.name).toBeUndefined();
2281
+ expect(draftPerson.age).toBeUndefined();
2282
+ expect(draftPerson.pet).toBeUndefined();
2283
+
2284
+ draftPerson.name = "John";
2285
+ draftPerson.age = 20;
2286
+ const rex = Dog.create({ name: "Rex", breed: "Labrador" });
2287
+ draftPerson.pet = rex;
2288
+
2289
+ expect(draftPerson.name).toEqual("John");
2290
+ expect(draftPerson.age).toEqual(20);
2291
+ expect(draftPerson.pet).toEqual(rex);
2259
2292
  });
2260
2293
 
2261
- expect(person.name.toString()).toEqual("John");
2294
+ test("the new schema includes catchall properties", () => {
2295
+ const Person = co
2296
+ .map({
2297
+ name: z.string(),
2298
+ age: z.number(),
2299
+ })
2300
+ .catchall(z.string());
2301
+
2302
+ const DraftPerson = Person.partial();
2303
+
2304
+ const draftPerson = DraftPerson.create({});
2305
+ draftPerson.extraField = "extra";
2306
+
2307
+ expect(draftPerson.extraField).toEqual("extra");
2308
+ });
2262
2309
  });
2263
2310
  });
2264
2311
 
@@ -184,6 +184,25 @@ test("returns null if the value is unavailable after retries", async () => {
184
184
  expect(john).toBeNull();
185
185
  });
186
186
 
187
+ test("load works even when the coValue access is granted after the creation", async () => {
188
+ const alice = await createJazzTestAccount();
189
+ const bob = await createJazzTestAccount();
190
+
191
+ const Person = co.map({
192
+ name: z.string(),
193
+ });
194
+
195
+ const group = Group.create(alice);
196
+ const map = Person.create({ name: "John" }, group);
197
+
198
+ group.addMember("everyone", "reader");
199
+
200
+ const mapOnBob = await Person.load(map.id, { loadAs: bob });
201
+
202
+ expect(mapOnBob).not.toBeNull();
203
+ expect(mapOnBob?.name).toBe("John");
204
+ });
205
+
187
206
  test("load a large coValue", async () => {
188
207
  const syncServer = await setupJazzTestSync({ asyncPeers: true });
189
208