@dasch-swiss/dsp-js
Version:
JavaScript library that handles API requests to Knora
126 lines • 10.5 kB
JavaScript
import { of } from "rxjs";
import { MockList } from "../../test/data/api/v2/mock-list";
import { KnoraApiConfig } from "../knora-api-config";
import { KnoraApiConnection } from "../knora-api-connection";
describe("ListNodeV2Cache", function () {
var config = new KnoraApiConfig("http", "0.0.0.0", 3333, "", "", true);
var knoraApiConnection;
var getNodeSpy;
var getListSpy;
beforeEach(function () {
jasmine.Ajax.install();
knoraApiConnection = new KnoraApiConnection(config);
getNodeSpy = spyOn(knoraApiConnection.v2.list, "getNode").and.callFake(function (nodeIri) {
return of(MockList.mockNode(nodeIri));
});
getListSpy = spyOn(knoraApiConnection.v2.list, "getList").and.callFake(function (nodeIri) {
return of(MockList.mockList(nodeIri));
});
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
describe("Method getItem()", function () {
it("should get a list node from the cache", function (done) {
knoraApiConnection.v2.listNodeCache["getItem"]("http://rdfh.ch/lists/0001/treeList01").subscribe(function (node) {
expect(node.id).toEqual("http://rdfh.ch/lists/0001/treeList01");
expect(getNodeSpy).toHaveBeenCalledTimes(1);
expect(getNodeSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList01");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(getListSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList");
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList01"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined(); // root node Iri is a dependency of each list node
// since the test uses a sync Observable to retrieve the whole list, the information is present
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList02"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList03"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList10"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList11"]).not.toBeUndefined();
done();
});
});
it("should get a list node from the cache several times asynchronously", function (done) {
knoraApiConnection.v2.listNodeCache["getItem"]("http://rdfh.ch/lists/0001/treeList01").subscribe(function (node) {
expect(node.id).toEqual("http://rdfh.ch/lists/0001/treeList01");
expect(getNodeSpy).toHaveBeenCalledTimes(1);
expect(getNodeSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList01");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(getListSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList");
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList01"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined(); // root node Iri is a dependency of each list node
// since the test uses a sync Observable to retrieve the whole list, the information is present
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList02"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList03"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList10"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList11"]).not.toBeUndefined();
});
knoraApiConnection.v2.listNodeCache["getItem"]("http://rdfh.ch/lists/0001/treeList01").subscribe(function (node) {
expect(node.id).toEqual("http://rdfh.ch/lists/0001/treeList01");
expect(getNodeSpy).toHaveBeenCalledTimes(1);
expect(getNodeSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList01");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(getListSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList");
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList01"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined(); // root node Iri is a dependency of each list node
// since the test uses a sync Observable to retrieve the whole list, the information is present
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList02"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList03"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList10"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList11"]).not.toBeUndefined();
});
knoraApiConnection.v2.listNodeCache["getItem"]("http://rdfh.ch/lists/0001/treeList01").subscribe(function (node) {
expect(node.id).toEqual("http://rdfh.ch/lists/0001/treeList01");
expect(getNodeSpy).toHaveBeenCalledTimes(1);
expect(getNodeSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList01");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(getListSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList");
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList01"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined(); // root node Iri is a dependency of each list node
// since the test uses a sync Observable to retrieve the whole list, the information is present
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList02"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList03"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList10"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList11"]).not.toBeUndefined();
done();
});
});
});
describe("Method getNode()", function () {
it("should get a list node from the cache", function (done) {
knoraApiConnection.v2.listNodeCache.getNode("http://rdfh.ch/lists/0001/treeList01").subscribe(function (node) {
expect(node.id).toEqual("http://rdfh.ch/lists/0001/treeList01");
expect(getNodeSpy).toHaveBeenCalledTimes(1);
expect(getNodeSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList01");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(getListSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList");
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList01"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined(); // root node Iri is a dependency of each list node
// since the test uses a sync Observable to retrieve the whole list, the information is present
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList02"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList03"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList10"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList11"]).not.toBeUndefined();
done();
});
});
});
describe("Method reloadCachedItem", function () {
it("should reload the item in the cache", function (done) {
knoraApiConnection.v2.listNodeCache["reloadCachedItem"]("http://rdfh.ch/lists/0001/treeList01").subscribe(function (node) {
expect(node.id).toEqual("http://rdfh.ch/lists/0001/treeList01");
expect(getNodeSpy).toHaveBeenCalledTimes(1);
expect(getNodeSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList01");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(getListSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList");
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList01"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined(); // root node Iri is a dependency of each list node
// since the test uses a sync Observable to retrieve the whole list, the information is present
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList02"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList03"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList10"]).not.toBeUndefined();
expect(knoraApiConnection.v2.listNodeCache["cache"]["http://rdfh.ch/lists/0001/treeList11"]).not.toBeUndefined();
done();
});
});
});
});
//# sourceMappingURL=ListNodeV2Cache.spec.js.map