UNPKG

@dasch-swiss/dsp-js

Version:

JavaScript library that handles API requests to Knora

90 lines 4.64 kB
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 { UserResponse } from "../models/admin/user-response"; import { UserCache } from "./UserCache"; describe("UserCache", function () { var config = new KnoraApiConfig("http", "0.0.0.0", 3333); var knoraApiConnection = new KnoraApiConnection(config); var getUserSpy; var userCache; var jsonConvert = new JsonConvert(OperationMode.ENABLE, ValueCheckingMode.DISALLOW_NULL, false, PropertyMatchingRule.CASE_STRICT); var user = require("../../test/data/api/admin/users/get-user-response.json"); var userResp = jsonConvert.deserialize(user, UserResponse); beforeEach(function () { jasmine.Ajax.install(); getUserSpy = spyOn(knoraApiConnection.admin.usersEndpoint, "getUser").and.callFake(function (prop, userId) { return of({ body: userResp }); }); userCache = new UserCache(knoraApiConnection); }); afterEach(function () { jasmine.Ajax.uninstall(); }); describe("Method getItem", function () { it("should get a user from the cache", function (done) { userCache["getItem"]("http://rdfh.ch/users/root").subscribe(function (res) { expect(res.user.id).toEqual("http://rdfh.ch/users/root"); expect(getUserSpy).toHaveBeenCalledTimes(1); expect(userCache["cache"]["http://rdfh.ch/users/root"]).not.toBeUndefined(); done(); }); }); it("should get the user from the cache twice synchronously", function (done) { userCache["getItem"]("http://rdfh.ch/users/root").subscribe(function (res) { expect(res.user.id).toEqual("http://rdfh.ch/users/root"); expect(getUserSpy).toHaveBeenCalledTimes(1); userCache["getItem"]("http://rdfh.ch/users/root").subscribe(function (res2) { expect(res2.user.id).toEqual("http://rdfh.ch/users/root"); expect(getUserSpy).toHaveBeenCalledTimes(1); done(); }); }); }); it("should get the same user from the cache several times asynchronously", function () { userCache["getItem"]("http://rdfh.ch/users/root").subscribe(function (res) { expect(getUserSpy).toHaveBeenCalledTimes(1); }); userCache["getItem"]("http://rdfh.ch/users/root").subscribe(function (res) { expect(getUserSpy).toHaveBeenCalledTimes(1); }); userCache["getItem"]("http://rdfh.ch/users/root").subscribe(function (res) { expect(getUserSpy).toHaveBeenCalledTimes(1); }); expect(getUserSpy).toHaveBeenCalledTimes(1); expect(getUserSpy).toHaveBeenCalledWith("iri", "http://rdfh.ch/users/root"); }); it("should get a user from the cache and refresh the entry", function (done) { userCache["getItem"]("http://rdfh.ch/users/root").subscribe(function (res) { expect(getUserSpy).toHaveBeenCalledTimes(1); userCache["reloadItem"]("http://rdfh.ch/users/root").subscribe(function (res2) { expect(getUserSpy).toHaveBeenCalledTimes(2); done(); }); }); }); }); describe("Method getUser", function () { it("should get a user by its Iri", function (done) { userCache.getUser("http://rdfh.ch/users/root").subscribe(function (res) { expect(res.user.id).toEqual("http://rdfh.ch/users/root"); expect(getUserSpy).toHaveBeenCalledTimes(1); expect(userCache["cache"]["http://rdfh.ch/users/root"]).not.toBeUndefined(); done(); }); }); }); describe("Method reloadCachedItem", function () { it("should get reload a user from the cache", function (done) { userCache["reloadCachedItem"]("http://rdfh.ch/users/root").subscribe(function (res) { expect(res.user.id).toEqual("http://rdfh.ch/users/root"); expect(getUserSpy).toHaveBeenCalledTimes(1); expect(userCache["cache"]["http://rdfh.ch/users/root"]).not.toBeUndefined(); done(); }); }); }); }); //# sourceMappingURL=UserCache.spec.js.map