]> Lady’s Gitweb - Etiquette/blobdiff - model.test.js
Enable application of activities onto tag systems
[Etiquette] / model.test.js
index 4396d37def8892db01a6f32076ca066b31773a21..254dd4cc7474c67ccc43abc2f53db61cb102dd10 100644 (file)
@@ -973,6 +973,208 @@ describe("TagSystem", () => {
     // `::[Storage.toObject]` is tested by `::persist`.
   });
 
+  describe("::apply", () => {
+    let system;
+    let Tag;
+
+    beforeEach(() => {
+      system = new TagSystem("example", "1972-12-31");
+      Tag = system.Tag;
+    });
+
+    it("[[Call]] throws if no activity is provided", () => {
+      assertThrows(() => {
+        system.apply();
+      });
+    });
+
+    it("[[Call]] throws with an invalid activity", () => {
+      assertThrows(() => {
+        system.apply({});
+      });
+    });
+
+    it("[[Call]] throws when specifying an invalid object", () => {
+      assertThrows(() => {
+        system.apply({
+          object: "",
+        });
+      });
+      assertThrows(() => {
+        system.apply({
+          object: `${system.iriSpace}000-0000`,
+        });
+      });
+    });
+
+    it("[[Call]] returns the tag being modified", () => {
+      const tag = new Tag();
+      tag.persist(true);
+      const applied = system.apply({ object: tag.iri });
+      assertStrictEquals(
+        Object.getPrototypeOf(applied),
+        system.Tag.prototype,
+      );
+      assertStrictEquals(tag.identifier, applied.identifier);
+    });
+
+    it("[[Call]] applies the changes", () => {
+      const broaderTag = new Tag();
+      const broaderActivity = broaderTag.persist();
+      const otherBroaderTag = new Tag();
+      const otherBroaderActivity = otherBroaderTag.persist();
+      const tag = new Tag("EntityTag", "my pref label");
+      tag.addHiddenLabel("label");
+      tag.addBroaderTag(broaderTag);
+      const createActivity = tag.persist();
+      tag.prefLabel = "new pref label";
+      tag.addAltLabel("alternative label");
+      tag.deleteHiddenLabel("label");
+      tag.addBroaderTag(otherBroaderTag);
+      tag.deleteBroaderTag(broaderTag);
+      const updateActivity = tag.persist();
+      const otherSystem = new TagSystem(
+        system.authorityName,
+        system.date,
+      );
+      otherSystem.apply(broaderActivity);
+      otherSystem.apply(otherBroaderActivity);
+      const appliedCreate = otherSystem.apply(createActivity);
+      assertStrictEquals(appliedCreate.kind, "EntityTag");
+      assertStrictEquals(appliedCreate.identifier, tag.identifier);
+      assertEquals(
+        { ...appliedCreate.prefLabel },
+        { "@value": "my pref label" },
+      );
+      assertEquals(
+        [...appliedCreate.hiddenLabels()].map(($) => ({ ...$ })),
+        [{ "@value": "label" }],
+      );
+      assertEquals(
+        [...appliedCreate.broaderTags()].map(($) => $.identifier),
+        [broaderTag.identifier],
+      );
+      const appliedUpdate = otherSystem.apply(updateActivity);
+      assertEquals(
+        { ...appliedUpdate.prefLabel },
+        { "@value": "new pref label" },
+      );
+      assertEquals(
+        [...appliedUpdate.altLabels()].map(($) => ({ ...$ })),
+        [{ "@value": "alternative label" }],
+      );
+      assertEquals([...appliedUpdate.hiddenLabels()], []);
+      assertEquals(
+        [...appliedUpdate.broaderTags()].map(($) => $.identifier),
+        [otherBroaderTag.identifier],
+      );
+    });
+
+    it("[[Call]] silently fails deleting preflabels", () => {
+      const tag = new system.Tag("Tag", "my pref label");
+      tag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        unstates: [{
+          predicate: "prefLabel",
+          object: "my pref label",
+        }],
+      });
+      assertEquals(
+        { ...applied.prefLabel },
+        { "@value": "my pref label" },
+      );
+    });
+
+    it("[[Call]] silently fails deleting unrecognized statements", () => {
+      const tag = new Tag();
+      tag.persist(true);
+      const otherTag = new Tag();
+      otherTag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        unstates: [{
+          predicate: "bad_statement",
+          object: otherTag.iri,
+        }],
+      });
+      assert(applied);
+    });
+
+    it("[[Call]] silently fails deleting immutable statements", () => {
+      const tag = new Tag();
+      tag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        unstates: [{ predicate: "a", object: "Tag" }],
+      });
+      assertStrictEquals(applied.kind, "Tag");
+    });
+
+    it("[[Call]] silently fails deleting inverse statements", () => {
+      const tag = new Tag();
+      tag.persist(true);
+      const otherTag = new Tag();
+      otherTag.addBroaderTag(tag);
+      otherTag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        unstates: [{ predicate: "narrower", object: otherTag.iri }],
+      });
+      assertStrictEquals(
+        [...applied.narrowerTags()][0].identifier,
+        otherTag.identifier,
+      );
+    });
+
+    it("[[Call]] sets preflabels", () => {
+      const tag = new Tag("Tag", "my pref label");
+      tag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        states: [{ predicate: "prefLabel", object: "new pref label" }],
+      });
+      assertEquals(
+        { ...applied.prefLabel },
+        { "@value": "new pref label" },
+      );
+    });
+
+    it("[[Call]] silently fails setting unrecognized statements", () => {
+      const tag = new Tag();
+      tag.persist(true);
+      const otherTag = new Tag();
+      otherTag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        states: [{ predicate: "bad_statement", object: otherTag.iri }],
+      });
+      assert(applied);
+    });
+
+    it("[[Call]] silently fails setting immutable statements", () => {
+      const tag = new Tag();
+      tag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        states: [{ predicate: "a", object: "RelationshipTag" }],
+      });
+      assertStrictEquals(applied.kind, "Tag");
+    });
+
+    it("[[Call]] silently fails setting inverse statements", () => {
+      const tag = new Tag();
+      tag.persist(true);
+      const otherTag = new Tag();
+      otherTag.persist(true);
+      const applied = system.apply({
+        object: tag.iri,
+        unstates: [{ predicate: "narrower", object: otherTag.iri }],
+      });
+      assertEquals([...applied.narrowerTags()], []);
+    });
+  });
+
   describe("::authorityName", () => {
     it("[[Get]] returns the authority name", () => {
       const system = new TagSystem("etaoin.example", "1972-12-31");
This page took 0.02846 seconds and 4 git commands to generate.