mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 02:24:11 +00:00
finished tests and docs
This commit is contained in:
61
src/app/core/cache/object-cache.service.spec.ts
vendored
61
src/app/core/cache/object-cache.service.spec.ts
vendored
@@ -2,12 +2,20 @@ import { Store } from '@ngrx/store';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
|
||||
import { ObjectCacheService } from './object-cache.service';
|
||||
import { AddToObjectCacheAction, RemoveFromObjectCacheAction } from './object-cache.actions';
|
||||
import {
|
||||
AddPatchObjectCacheAction,
|
||||
AddToObjectCacheAction, ApplyPatchObjectCacheAction,
|
||||
RemoveFromObjectCacheAction
|
||||
} from './object-cache.actions';
|
||||
import { CoreState } from '../core.reducers';
|
||||
import { ResourceType } from '../shared/resource-type';
|
||||
import { NormalizedItem } from './models/normalized-item.model';
|
||||
import { first } from 'rxjs/operators';
|
||||
import * as ngrx from '@ngrx/store';
|
||||
import { Operation } from '../../../../node_modules/fast-json-patch';
|
||||
import { RestRequestMethod } from '../data/rest-request-method';
|
||||
import { AddToSSBAction } from './server-sync-buffer.actions';
|
||||
import { Patch } from './object-cache.reducer';
|
||||
|
||||
describe('ObjectCacheService', () => {
|
||||
let service: ObjectCacheService;
|
||||
@@ -16,18 +24,29 @@ describe('ObjectCacheService', () => {
|
||||
const selfLink = 'https://rest.api/endpoint/1698f1d3-be98-4c51-9fd8-6bfedcbd59b7';
|
||||
const timestamp = new Date().getTime();
|
||||
const msToLive = 900000;
|
||||
const objectToCache = {
|
||||
let objectToCache = {
|
||||
self: selfLink,
|
||||
type: ResourceType.Item
|
||||
};
|
||||
const cacheEntry = {
|
||||
data: objectToCache,
|
||||
timeAdded: timestamp,
|
||||
msToLive: msToLive
|
||||
};
|
||||
const invalidCacheEntry = Object.assign({}, cacheEntry, { msToLive: -1 });
|
||||
let cacheEntry;
|
||||
let invalidCacheEntry;
|
||||
const operations = [{ op: 'replace', path: '/name', value: 'random string' } as Operation];
|
||||
|
||||
function init() {
|
||||
objectToCache = {
|
||||
self: selfLink,
|
||||
type: ResourceType.Item
|
||||
};
|
||||
cacheEntry = {
|
||||
data: objectToCache,
|
||||
timeAdded: timestamp,
|
||||
msToLive: msToLive
|
||||
};
|
||||
invalidCacheEntry = Object.assign({}, cacheEntry, { msToLive: -1 })
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
init();
|
||||
store = new Store<CoreState>(undefined, undefined, undefined);
|
||||
spyOn(store, 'dispatch');
|
||||
service = new ObjectCacheService(store);
|
||||
@@ -127,4 +146,30 @@ describe('ObjectCacheService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('patch methods', () => {
|
||||
it('should dispatch the correct actions when addPatch is called', () => {
|
||||
service.addPatch(selfLink, operations);
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new AddPatchObjectCacheAction(selfLink, operations));
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new AddToSSBAction(selfLink, RestRequestMethod.PATCH));
|
||||
});
|
||||
|
||||
it('isDirty should return true when the patches list in the cache entry is not empty', () => {
|
||||
cacheEntry.patches = [
|
||||
{
|
||||
operations: operations
|
||||
} as Patch];
|
||||
const result = (service as any).isDirty(cacheEntry);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('isDirty should return false when the patches list in the cache entry is empty', () => {
|
||||
cacheEntry.patches = [];
|
||||
const result = (service as any).isDirty(cacheEntry);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
it('should dispatch the correct actions when applyPatchesToCachedObject is called', () => {
|
||||
(service as any).applyPatchesToCachedObject(selfLink);
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new ApplyPatchObjectCacheAction(selfLink));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user