jazz-tools 0.17.4 → 0.17.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.
@@ -460,3 +460,107 @@ describe("CoMap.Record", async () => {
460
460
  }
461
461
  });
462
462
  });
463
+
464
+ describe("CoRecord unique methods", () => {
465
+ test("loadUnique returns existing record", async () => {
466
+ const ItemRecord = co.record(z.string(), z.number());
467
+ const group = Group.create();
468
+
469
+ const originalRecord = ItemRecord.create(
470
+ { item1: 1, item2: 2, item3: 3 },
471
+ { owner: group, unique: "test-record" },
472
+ );
473
+
474
+ const foundRecord = await ItemRecord.loadUnique("test-record", group.id);
475
+ expect(foundRecord).toEqual(originalRecord);
476
+ expect(foundRecord?.item1).toBe(1);
477
+ expect(foundRecord?.item2).toBe(2);
478
+ });
479
+
480
+ test("loadUnique returns null for non-existent record", async () => {
481
+ const ItemRecord = co.record(z.string(), z.number());
482
+ const group = Group.create();
483
+
484
+ const foundRecord = await ItemRecord.loadUnique("non-existent", group.id);
485
+ expect(foundRecord).toBeNull();
486
+ });
487
+
488
+ test("upsertUnique creates new record when none exists", async () => {
489
+ const ItemRecord = co.record(z.string(), z.number());
490
+ const group = Group.create();
491
+
492
+ const sourceData = { item1: 1, item2: 2, item3: 3 };
493
+
494
+ const result = await ItemRecord.upsertUnique({
495
+ value: sourceData,
496
+ unique: "new-record",
497
+ owner: group,
498
+ });
499
+
500
+ expect(result).not.toBeNull();
501
+ expect(result?.item1).toBe(1);
502
+ expect(result?.item2).toBe(2);
503
+ expect(result?.item3).toBe(3);
504
+ });
505
+
506
+ test("upsertUnique updates existing record", async () => {
507
+ const ItemRecord = co.record(z.string(), z.number());
508
+ const group = Group.create();
509
+
510
+ // Create initial record
511
+ const originalRecord = ItemRecord.create(
512
+ { original1: 1, original2: 2 },
513
+ { owner: group, unique: "update-record" },
514
+ );
515
+
516
+ // Upsert with new data
517
+ const updatedRecord = await ItemRecord.upsertUnique({
518
+ value: { updated1: 10, updated2: 20, updated3: 30 },
519
+ unique: "update-record",
520
+ owner: group,
521
+ });
522
+
523
+ expect(updatedRecord).toEqual(originalRecord); // Should be the same instance
524
+ expect(updatedRecord?.updated1).toBe(10);
525
+ expect(updatedRecord?.updated2).toBe(20);
526
+ expect(updatedRecord?.updated3).toBe(30);
527
+ });
528
+
529
+ test("upsertUnique with CoValue items", async () => {
530
+ const Item = co.map({
531
+ name: z.string(),
532
+ value: z.number(),
533
+ });
534
+ const ItemRecord = co.record(z.string(), Item);
535
+ const group = Group.create();
536
+
537
+ const items = {
538
+ first: Item.create({ name: "First", value: 1 }, group),
539
+ second: Item.create({ name: "Second", value: 2 }, group),
540
+ };
541
+
542
+ const result = await ItemRecord.upsertUnique({
543
+ value: items,
544
+ unique: "item-record",
545
+ owner: group,
546
+ resolve: { first: true, second: true },
547
+ });
548
+
549
+ expect(result).not.toBeNull();
550
+ expect(result?.first?.name).toBe("First");
551
+ expect(result?.second?.name).toBe("Second");
552
+ });
553
+
554
+ test("findUnique returns correct ID", async () => {
555
+ const ItemRecord = co.record(z.string(), z.string());
556
+ const group = Group.create();
557
+
558
+ const originalRecord = ItemRecord.create(
559
+ { test: "value" },
560
+ { owner: group, unique: "find-test" },
561
+ );
562
+
563
+ const foundId = ItemRecord.findUnique("find-test", group.id);
564
+ expect(foundId).toBe(originalRecord.id);
565
+ });
566
+ });