@dasch-swiss/dsp-js
Version:
JavaScript library that handles API requests to Knora
71 lines • 3.88 kB
JavaScript
import { JsonConvert, OperationMode, ValueCheckingMode } from "json2typescript";
import { PropertyMatchingRule } from "json2typescript/src/json2typescript/json-convert-enums";
import { of } from "rxjs";
import { KnoraApiConfig } from "../knora-api-config";
import { KnoraApiConnection } from "../knora-api-connection";
import { ListResponse } from "../models/admin/list-response";
import { ListAdminCache } from "./ListAdminCache";
describe("ListCache", function () {
var config = new KnoraApiConfig("http", "0.0.0.0", 3333);
var knoraApiConnection = new KnoraApiConnection(config);
var getListSpy;
var listCache;
var jsonConvert = new JsonConvert(OperationMode.ENABLE, ValueCheckingMode.DISALLOW_NULL, false, PropertyMatchingRule.CASE_STRICT);
var list = require("../../test/data/api/admin/lists/get-list-response.json");
var listResp = jsonConvert.deserializeObject(list, ListResponse);
beforeEach(function () {
jasmine.Ajax.install();
getListSpy = spyOn(knoraApiConnection.admin.listsEndpoint, "getList").and.callFake(function (listIri) {
return of({ body: listResp });
});
listCache = new ListAdminCache(knoraApiConnection.admin);
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
describe("Method getItem", function () {
it("should get a full list from the cache", function (done) {
listCache["getItem"]("http://rdfh.ch/lists/0001/treeList").subscribe(function (res) {
expect(res.list.listinfo.id).toEqual("http://rdfh.ch/lists/0001/treeList");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(listCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined();
done();
});
});
it("should get the full list from the cache twice synchronously", function (done) {
listCache["getItem"]("http://rdfh.ch/lists/0001/treeList").subscribe(function (res) {
expect(res.list.listinfo.id).toEqual("http://rdfh.ch/lists/0001/treeList");
expect(getListSpy).toHaveBeenCalledTimes(1);
listCache["getItem"]("http://rdfh.ch/lists/0001/treeList").subscribe(function (res2) {
expect(res2.list.listinfo.id).toEqual("http://rdfh.ch/lists/0001/treeList");
expect(getListSpy).toHaveBeenCalledTimes(1);
});
done();
});
});
it("should get the same full list from the cache several times asynchronously", function () {
listCache["getItem"]("http://rdfh.ch/lists/0001/treeList").subscribe(function (res) {
expect(getListSpy).toHaveBeenCalledTimes(1);
});
listCache["getItem"]("http://rdfh.ch/lists/0001/treeList").subscribe(function (res) {
expect(getListSpy).toHaveBeenCalledTimes(1);
});
listCache["getItem"]("http://rdfh.ch/lists/0001/treeList").subscribe(function (res) {
expect(getListSpy).toHaveBeenCalledTimes(1);
});
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(getListSpy).toHaveBeenCalledWith("http://rdfh.ch/lists/0001/treeList");
});
});
describe("Method reloadCachedItem", function () {
it("should reload the item in the cache", function (done) {
listCache["reloadCachedItem"]("http://rdfh.ch/lists/0001/treeList").subscribe(function (res) {
expect(res.list.listinfo.id).toEqual("http://rdfh.ch/lists/0001/treeList");
expect(getListSpy).toHaveBeenCalledTimes(1);
expect(listCache["cache"]["http://rdfh.ch/lists/0001/treeList"]).not.toBeUndefined();
done();
});
});
});
});
//# sourceMappingURL=ListAdminCache.spec.js.map