resourcerer 2.0.7 → 2.1.1

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.
@@ -1,4 +1,6 @@
1
1
  import * as sync from "../lib/sync";
2
+ import CanonicalModel from "../lib/canonical-model";
3
+ import { canonicalModelCache } from "../lib/canonical-model-cache";
2
4
 
3
5
  import Collection from "../lib/collection";
4
6
  import Model from "../lib/model";
@@ -47,7 +49,7 @@ describe("Model", () => {
47
49
  parse(attrs) {
48
50
  return Object.keys(attrs).reduce(
49
51
  (memo, attr) => Object.assign(memo, { [attr]: attrs[attr] + 5 }),
50
- {}
52
+ {},
51
53
  );
52
54
  }
53
55
  }
@@ -514,4 +516,359 @@ describe("Model", () => {
514
516
  expect(model.isNew()).toBe(false);
515
517
  });
516
518
  });
519
+
520
+ describe("subscriptions", () => {
521
+ let sourceModel, targetModel;
522
+
523
+ beforeEach(() => {
524
+ sourceModel = new SubscribingSourceModel();
525
+ targetModel = new SubscribingTargetModel();
526
+ });
527
+
528
+ afterEach(() => {
529
+ sourceModel.unsubscribe();
530
+ targetModel.unsubscribe();
531
+ canonicalModelCache.clear();
532
+ });
533
+
534
+ describe("are not made", () => {
535
+ it("if the model is the empty model", async () => {
536
+ vi.spyOn(targetModel, "fetch").mockResolvedValue([{}, {}]);
537
+
538
+ targetModel.isEmptyModel = true;
539
+ await targetModel.fetch();
540
+ sourceModel.set({ _id: "1234", name: "Zorah" });
541
+ expect(targetModel.get("name")).toBeUndefined();
542
+ delete targetModel.isEmptyModel;
543
+ });
544
+
545
+ it("if the model is new", () => {
546
+ // there is no id linking them, so no subscriptions will be made
547
+ sourceModel.set({ _id: "1234", name: "Zorah" });
548
+ expect(targetModel.get("name")).toBeUndefined();
549
+ });
550
+
551
+ it("if the subscribe option is false when the model is instantiated", () => {
552
+ sourceModel = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
553
+ targetModel = new SubscribingTargetModel({ id: "1234", subscribe: false });
554
+
555
+ sourceModel.set({ _id: "1234", name: "Zorah" });
556
+ expect(targetModel.get("name")).toBeUndefined();
557
+ });
558
+
559
+ it("if the model is already subscribed", () => {
560
+ targetModel = new SubscribingTargetModel({ id: "1234" });
561
+ sourceModel = new SubscribingSourceModel();
562
+
563
+ vi.spyOn(targetModel, "set");
564
+
565
+ targetModel._subscribe();
566
+ targetModel._subscribe();
567
+ targetModel._subscribe();
568
+ targetModel._subscribe();
569
+
570
+ sourceModel.set({ _id: "1234", name: "Zorah" });
571
+ expect(targetModel.get("name")).toBe("C. Zorah");
572
+ // only called once!
573
+ expect(targetModel.set).toHaveBeenCalledTimes(1);
574
+ });
575
+ });
576
+
577
+ describe("are made", () => {
578
+ it("when the model is instantiated", () => {
579
+ targetModel = new SubscribingTargetModel({ id: "1234" });
580
+
581
+ vi.spyOn(targetModel, "set");
582
+ sourceModel = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
583
+
584
+ expect(targetModel.get("name")).toBe("C. Zorah");
585
+ expect(targetModel.set).toHaveBeenCalledTimes(1);
586
+ });
587
+
588
+ it("when the model is fetched", async () => {
589
+ // no id yet, so no subscription made
590
+ targetModel = new SubscribingTargetModel();
591
+ // id will return on the fetch
592
+ vi.spyOn(targetModel, "sync").mockResolvedValue([{ id: "1234" }, {}]);
593
+
594
+ sourceModel.set({ _id: "1234", name: "Zorah" });
595
+ expect(targetModel.get("name")).not.toBe("C. Zorah");
596
+
597
+ await targetModel.fetch();
598
+
599
+ sourceModel.set({ _id: "1234", name: "Zorah 2" });
600
+ expect(targetModel.get("name")).toBe("C. Zorah 2");
601
+ });
602
+
603
+ it("when the model is saved", async () => {
604
+ // no id yet, so no subscription made
605
+ targetModel = new SubscribingTargetModel();
606
+ // id will return on the save
607
+ vi.spyOn(targetModel, "sync").mockResolvedValue([{ id: "1234" }, {}]);
608
+
609
+ sourceModel.set({ _id: "1234", name: "Zorah" });
610
+ expect(targetModel.get("name")).not.toBe("C. Zorah");
611
+
612
+ await targetModel.save();
613
+
614
+ sourceModel.set({ _id: "1234", name: "Zorah 2" });
615
+ expect(targetModel.get("name")).toBe("C. Zorah 2");
616
+ });
617
+ });
618
+
619
+ describe("are not updated", () => {
620
+ it("if the subscribe option is false", () => {
621
+ sourceModel = new SubscribingSourceModel();
622
+ targetModel = new SubscribingTargetModel({ id: "1234" });
623
+
624
+ sourceModel.set({ _id: "1234", name: "Zorah" }, { subscribe: false });
625
+ expect(targetModel.get("name")).not.toBeDefined();
626
+ });
627
+
628
+ it("if the source options is 'subscription'", () => {
629
+ sourceModel = new SubscribingSourceModel();
630
+ targetModel = new SubscribingTargetModel({ id: "1234" });
631
+
632
+ sourceModel.set({ _id: "1234", name: "Zorah" }, { source: "subscription" });
633
+ expect(targetModel.get("name")).not.toBeDefined();
634
+ });
635
+
636
+ it("if nothing changes", () => {
637
+ vi.spyOn(SubscribingTargetModel.prototype, "_updateSubscriptions");
638
+
639
+ sourceModel = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
640
+ targetModel = new SubscribingTargetModel({ id: "1234", name: "C. Zorah" });
641
+
642
+ expect(SubscribingTargetModel.prototype._updateSubscriptions).toHaveBeenCalledTimes(1);
643
+ SubscribingTargetModel.prototype._updateSubscriptions.mockClear();
644
+
645
+ sourceModel.set({ _id: "1234", name: "Zorah" });
646
+ expect(SubscribingTargetModel.prototype._updateSubscriptions).not.toHaveBeenCalled();
647
+ });
648
+ });
649
+ });
650
+
651
+ describe("_subscribe", () => {
652
+ beforeEach(() => {
653
+ vi.spyOn(CanonicalModel.prototype, "onUpdate").mockReturnValue();
654
+ vi.spyOn(CanonicalModel.prototype, "offUpdate").mockReturnValue();
655
+ });
656
+
657
+ describe("does nothing", () => {
658
+ it("if the model is the empty model", () => {
659
+ const emptyModel = new SubscribingTargetModel();
660
+
661
+ emptyModel.isEmptyModel = true;
662
+ emptyModel._subscribe();
663
+
664
+ expect(CanonicalModel.prototype.onUpdate).not.toHaveBeenCalled();
665
+ expect(CanonicalModel.prototype.offUpdate).not.toHaveBeenCalled();
666
+ });
667
+
668
+ it("if the model has no canonical model id", () => {
669
+ const model = new SubscribingTargetModel({ name: "Zorah" });
670
+
671
+ model._subscribe();
672
+
673
+ expect(CanonicalModel.prototype.onUpdate).not.toHaveBeenCalled();
674
+ expect(CanonicalModel.prototype.offUpdate).not.toHaveBeenCalled();
675
+ });
676
+ });
677
+
678
+ describe("the id field", () => {
679
+ it("can come from attributes set on the model", () => {
680
+ const model = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
681
+
682
+ model._subscribe();
683
+
684
+ expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
685
+ expect(CanonicalModel.prototype.offUpdate).toHaveBeenCalledWith(model);
686
+ });
687
+
688
+ it("can come from the attributes passed to the _subscribe method", () => {
689
+ const model = new SubscribingSourceModel();
690
+
691
+ model._subscribe({ _id: "1234", name: "Zorah" });
692
+
693
+ expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
694
+ expect(CanonicalModel.prototype.offUpdate).toHaveBeenCalledWith(model);
695
+ });
696
+
697
+ it("can be nested with a dot-separated string", () => {
698
+ class NestedSubscribingSourceModel extends SubscribingSourceModel {
699
+ static subscriptions = [
700
+ {
701
+ Model: CanonicalTestModel,
702
+ idField: "nested._id",
703
+ },
704
+ ];
705
+ }
706
+
707
+ const model = new NestedSubscribingSourceModel({ nested: { _id: "1234", name: "Zorah" } });
708
+
709
+ model._subscribe();
710
+
711
+ expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
712
+ expect(CanonicalModel.prototype.offUpdate).toHaveBeenCalledWith(model);
713
+ });
714
+ });
715
+
716
+ describe("the onUpdate callback", () => {
717
+ describe("does nothing", () => {
718
+ it("if the context is the same as the model instance", () => {
719
+ const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
720
+ const model2 = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
721
+
722
+ vi.spyOn(model, "set");
723
+
724
+ expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
725
+ // same context
726
+ CanonicalModel.prototype.onUpdate.mock.calls[0][0]({ name: "Zorah 2" }, model);
727
+
728
+ expect(model.set).not.toHaveBeenCalled();
729
+ });
730
+
731
+ it("if there is no fromSource function", () => {
732
+ // the source model is toSource only
733
+ const model = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
734
+ const model2 = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
735
+
736
+ vi.spyOn(model, "set");
737
+
738
+ expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
739
+ // different context
740
+ CanonicalModel.prototype.onUpdate.mock.calls[0][0]({ name: "Zorah 2" }, model2);
741
+
742
+ expect(model.set).not.toHaveBeenCalled();
743
+ });
744
+ });
745
+
746
+ describe("updates the model", () => {
747
+ it("if the fromSource function is provided", () => {
748
+ const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
749
+ const model2 = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
750
+
751
+ vi.spyOn(model, "set");
752
+
753
+ expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
754
+ // different context
755
+ CanonicalModel.prototype.onUpdate.mock.calls[0][0](
756
+ { _id: "1234", name: "Zorah 2" },
757
+ model2,
758
+ );
759
+
760
+ expect(model.set).toHaveBeenCalledWith(
761
+ // mapped attributes
762
+ { id: "1234", name: "C. Zorah 2" },
763
+ { source: "subscription" },
764
+ );
765
+ });
766
+ });
767
+ });
768
+ });
769
+
770
+ describe("unsubscribe", () => {
771
+ beforeEach(() => {
772
+ vi.spyOn(CanonicalModel.prototype, "removeSubscription").mockReturnValue();
773
+ });
774
+
775
+ it("does nothing if the model has no canonical model id", () => {
776
+ const model = new SubscribingTargetModel({ name: "Zorah" });
777
+
778
+ model.unsubscribe();
779
+
780
+ expect(CanonicalModel.prototype.removeSubscription).not.toHaveBeenCalled();
781
+ });
782
+
783
+ it("removes the model from the canonical model's subscriptions", () => {
784
+ const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
785
+
786
+ model.unsubscribe();
787
+
788
+ expect(CanonicalModel.prototype.removeSubscription).toHaveBeenCalledWith(model, "1234");
789
+ });
790
+ });
791
+
792
+ describe("_updateSubscriptions", () => {
793
+ beforeEach(() => {
794
+ vi.spyOn(CanonicalModel.prototype, "set").mockReturnValue();
795
+ });
796
+
797
+ describe("does nothing", () => {
798
+ it("if the model has no canonical model id", () => {
799
+ const model = new SubscribingTargetModel({ name: "Zorah" });
800
+
801
+ model._updateSubscriptions();
802
+
803
+ expect(CanonicalModel.prototype.set).not.toHaveBeenCalled();
804
+ });
805
+
806
+ it("if the model is the empty model", () => {
807
+ const model = new SubscribingSourceModel();
808
+
809
+ model.isEmptyModel = true;
810
+ model.set({ _id: "1234", name: "Zorah" });
811
+ model._updateSubscriptions();
812
+
813
+ expect(CanonicalModel.prototype.set).not.toHaveBeenCalled();
814
+ });
815
+
816
+ it("if there is no toSource function", () => {
817
+ const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
818
+
819
+ model._updateSubscriptions({ _id: "1234", name: "Zorah" });
820
+
821
+ expect(CanonicalModel.prototype.set).not.toHaveBeenCalled();
822
+ });
823
+ });
824
+
825
+ describe("the id field", () => {
826
+ it("can come from attributes set on the model", () => {
827
+ const model = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
828
+
829
+ model._updateSubscriptions();
830
+
831
+ expect(CanonicalModel.prototype.set).toHaveBeenCalledWith(
832
+ { _id: "1234", name: "Zorah" },
833
+ model,
834
+ {},
835
+ );
836
+ });
837
+
838
+ it("can come from the attributes passed to the _updateSubscriptions method", () => {
839
+ const model = new SubscribingSourceModel();
840
+ const options = {};
841
+
842
+ model._updateSubscriptions({ _id: "1234", name: "Zorah" }, options);
843
+
844
+ expect(CanonicalModel.prototype.set).toHaveBeenCalledWith(
845
+ { _id: "1234", name: "Zorah" },
846
+ model,
847
+ options,
848
+ );
849
+ });
850
+ });
851
+ });
517
852
  });
853
+
854
+ class CanonicalTestModel extends CanonicalModel {}
855
+ // this model has its own idattribute of id, but the canonical model uses the value of the _id property
856
+ class SubscribingSourceModel extends Model {
857
+ static subscriptions = [
858
+ {
859
+ Model: CanonicalTestModel,
860
+ idField: "_id",
861
+ toSource: (attrs) => ({ _id: attrs._id, name: attrs.name }),
862
+ },
863
+ ];
864
+ }
865
+
866
+ class SubscribingTargetModel extends Model {
867
+ static subscriptions = [
868
+ {
869
+ Model: CanonicalTestModel,
870
+ // default idField to id
871
+ fromSource: (attrs) => ({ id: attrs._id, name: "C. " + attrs.name }),
872
+ },
873
+ ];
874
+ }
@@ -1,5 +1,6 @@
1
1
  import { Utils } from "../index";
2
2
  import {
3
+ getNestedValue,
3
4
  hasErrored,
4
5
  hasLoaded,
5
6
  isDeepEqual,
@@ -186,7 +187,7 @@ describe("Utils", () => {
186
187
 
187
188
  it("if the objects have equal values, deeply", () => {
188
189
  expect(
189
- isDeepEqual({ one: "one", two: 2, three: null }, { one: "one", two: 2, three: null })
190
+ isDeepEqual({ one: "one", two: 2, three: null }, { one: "one", two: 2, three: null }),
190
191
  ).toBe(true);
191
192
  // nested
192
193
  expect(
@@ -198,8 +199,8 @@ describe("Utils", () => {
198
199
  {
199
200
  one: "one",
200
201
  two: { three: "three" },
201
- }
202
- )
202
+ },
203
+ ),
203
204
  ).toBe(true);
204
205
  });
205
206
  });
@@ -236,8 +237,8 @@ describe("Utils", () => {
236
237
  { name: "zorah" },
237
238
  { name: "alex" },
238
239
  ],
239
- ({ name }) => name
240
- )
240
+ ({ name }) => name,
241
+ ),
241
242
  ).toEqual([
242
243
  { name: "alex" },
243
244
  { name: "alex" },
@@ -252,4 +253,20 @@ describe("Utils", () => {
252
253
  expect(sortBy()).toEqual([]);
253
254
  });
254
255
  });
256
+
257
+ describe("getNestedValue", () => {
258
+ it("gets a nested value from an object", () => {
259
+ expect(getNestedValue({ foo: { bar: "baz" } }, "foo.bar")).toEqual("baz");
260
+ expect(getNestedValue({ foo: "bar" }, "foo")).toEqual("bar");
261
+ });
262
+
263
+ it("returns undefined if the path is not found", () => {
264
+ expect(getNestedValue({ foo: { bar: "baz" } }, "foo.baz")).toBeUndefined();
265
+ });
266
+
267
+ it("returns undefined if the object is not an object", () => {
268
+ // this wouldn't pass types...
269
+ expect(getNestedValue(null, "foo.bar")).toBeUndefined();
270
+ });
271
+ });
255
272
  });