mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-10 11:33:04 +00:00
switched to self links as keys in the object cache
This commit is contained in:
57
src/app/core/cache/object-cache.service.spec.ts
vendored
57
src/app/core/cache/object-cache.service.spec.ts
vendored
@@ -8,12 +8,12 @@ import { CoreState } from '../core.reducers';
|
||||
|
||||
class TestClass implements CacheableObject {
|
||||
constructor(
|
||||
public uuid: string,
|
||||
public self: string,
|
||||
public foo: string
|
||||
) { }
|
||||
|
||||
test(): string {
|
||||
return this.foo + this.uuid;
|
||||
return this.foo + this.self;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,11 @@ describe('ObjectCacheService', () => {
|
||||
let service: ObjectCacheService;
|
||||
let store: Store<CoreState>;
|
||||
|
||||
const uuid = '1698f1d3-be98-4c51-9fd8-6bfedcbd59b7';
|
||||
const requestHref = 'https://rest.api/endpoint/1698f1d3-be98-4c51-9fd8-6bfedcbd59b7';
|
||||
const selfLink = 'https://rest.api/endpoint/1698f1d3-be98-4c51-9fd8-6bfedcbd59b7';
|
||||
const timestamp = new Date().getTime();
|
||||
const msToLive = 900000;
|
||||
const objectToCache = {
|
||||
uuid: uuid,
|
||||
self: selfLink,
|
||||
foo: 'bar'
|
||||
};
|
||||
const cacheEntry = {
|
||||
@@ -48,73 +47,73 @@ describe('ObjectCacheService', () => {
|
||||
|
||||
describe('add', () => {
|
||||
it('should dispatch an ADD action with the object to add, the time to live, and the current timestamp', () => {
|
||||
service.add(objectToCache, msToLive, requestHref);
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new AddToObjectCacheAction(objectToCache, timestamp, msToLive, requestHref));
|
||||
service.add(objectToCache, msToLive, selfLink);
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new AddToObjectCacheAction(objectToCache, timestamp, msToLive, selfLink));
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', () => {
|
||||
it('should dispatch a REMOVE action with the UUID of the object to remove', () => {
|
||||
service.remove(uuid);
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new RemoveFromObjectCacheAction(uuid));
|
||||
it('should dispatch a REMOVE action with the self link of the object to remove', () => {
|
||||
service.remove(selfLink);
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new RemoveFromObjectCacheAction(selfLink));
|
||||
});
|
||||
});
|
||||
|
||||
describe('get', () => {
|
||||
it('should return an observable of the cached object with the specified UUID and type', () => {
|
||||
describe('getBySelfLink', () => {
|
||||
it('should return an observable of the cached object with the specified self link and type', () => {
|
||||
spyOn(store, 'select').and.returnValue(Observable.of(cacheEntry));
|
||||
|
||||
let testObj: any;
|
||||
// due to the implementation of spyOn above, this subscribe will be synchronous
|
||||
service.get(uuid, TestClass).take(1).subscribe((o) => testObj = o);
|
||||
expect(testObj.uuid).toBe(uuid);
|
||||
service.getBySelfLink(selfLink, TestClass).take(1).subscribe((o) => testObj = o);
|
||||
expect(testObj.self).toBe(selfLink);
|
||||
expect(testObj.foo).toBe('bar');
|
||||
// this only works if testObj is an instance of TestClass
|
||||
expect(testObj.test()).toBe('bar' + uuid);
|
||||
expect(testObj.test()).toBe('bar' + selfLink);
|
||||
});
|
||||
|
||||
it('should not return a cached object that has exceeded its time to live', () => {
|
||||
spyOn(store, 'select').and.returnValue(Observable.of(invalidCacheEntry));
|
||||
|
||||
let getObsHasFired = false;
|
||||
const subscription = service.get(uuid, TestClass).subscribe((o) => getObsHasFired = true);
|
||||
const subscription = service.getBySelfLink(selfLink, TestClass).subscribe((o) => getObsHasFired = true);
|
||||
expect(getObsHasFired).toBe(false);
|
||||
subscription.unsubscribe();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getList', () => {
|
||||
it('should return an observable of the array of cached objects with the specified UUID and type', () => {
|
||||
spyOn(service, 'get').and.returnValue(Observable.of(new TestClass(uuid, 'bar')));
|
||||
it('should return an observable of the array of cached objects with the specified self link and type', () => {
|
||||
spyOn(service, 'getBySelfLink').and.returnValue(Observable.of(new TestClass(selfLink, 'bar')));
|
||||
|
||||
let testObjs: any[];
|
||||
service.getList([uuid, uuid], TestClass).take(1).subscribe((arr) => testObjs = arr);
|
||||
expect(testObjs[0].uuid).toBe(uuid);
|
||||
service.getList([selfLink, selfLink], TestClass).take(1).subscribe((arr) => testObjs = arr);
|
||||
expect(testObjs[0].self).toBe(selfLink);
|
||||
expect(testObjs[0].foo).toBe('bar');
|
||||
expect(testObjs[0].test()).toBe('bar' + uuid);
|
||||
expect(testObjs[1].uuid).toBe(uuid);
|
||||
expect(testObjs[0].test()).toBe('bar' + selfLink);
|
||||
expect(testObjs[1].self).toBe(selfLink);
|
||||
expect(testObjs[1].foo).toBe('bar');
|
||||
expect(testObjs[1].test()).toBe('bar' + uuid);
|
||||
expect(testObjs[1].test()).toBe('bar' + selfLink);
|
||||
});
|
||||
});
|
||||
|
||||
describe('has', () => {
|
||||
it('should return true if the object with the supplied UUID is cached and still valid', () => {
|
||||
it('should return true if the object with the supplied self link is cached and still valid', () => {
|
||||
spyOn(store, 'select').and.returnValue(Observable.of(cacheEntry));
|
||||
|
||||
expect(service.has(uuid)).toBe(true);
|
||||
expect(service.hasBySelfLink(selfLink)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if the object with the supplied UUID isn't cached", () => {
|
||||
it("should return false if the object with the supplied self link isn't cached", () => {
|
||||
spyOn(store, 'select').and.returnValue(Observable.of(undefined));
|
||||
|
||||
expect(service.has(uuid)).toBe(false);
|
||||
expect(service.hasBySelfLink(selfLink)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false if the object with the supplied UUID is cached but has exceeded its time to live', () => {
|
||||
it('should return false if the object with the supplied self link is cached but has exceeded its time to live', () => {
|
||||
spyOn(store, 'select').and.returnValue(Observable.of(invalidCacheEntry));
|
||||
|
||||
expect(service.has(uuid)).toBe(false);
|
||||
expect(service.hasBySelfLink(selfLink)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user