Newer
Older
import { BaseResponseParsingService } from './base-response-parsing.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { CacheableObject } from '../cache/object-cache.reducer';
import { GetRequest, RestRequest } from './request.models';
import { DSpaceObject } from '../shared/dspace-object.model';
/* tslint:disable:max-classes-per-file */
class TestService extends BaseResponseParsingService {
toCache = true;
super();
}
// Overwrite methods to make them public for testing
public process<ObjectDomain>(data: any, request: RestRequest): any {
super.process(data, request);
}
public cache<ObjectDomain>(obj, request: RestRequest, data: any) {
super.cache(obj, request, data);
}
}
describe('BaseResponseParsingService', () => {
let service: TestService;
let objectCache: ObjectCacheService;
const requestUUID = 'request-uuid';
const requestHref = 'request-href';
const request = new GetRequest(requestUUID, requestHref);
beforeEach(() => {
objectCache = jasmine.createSpyObj('objectCache', {
add: {}
});
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
});
describe('cache', () => {
describe('when the object is undefined', () => {
it('should not throw an error', () => {
expect(() => { service.cache(obj, request, {}) }).not.toThrow();
});
it('should not call objectCache add', () => {
service.cache(obj, request, {});
expect(objectCache.add).not.toHaveBeenCalled();
});
});
describe('when the object has a self link', () => {
beforeEach(() => {
obj = Object.assign(new DSpaceObject(), {
_links: {
self: { href: 'obj-selflink' }
}
});
});
it('should call objectCache add', () => {
service.cache(obj, request, {});
expect(objectCache.add).toHaveBeenCalledWith(obj, request.responseMsToLive, request.uuid);
});
});
});
describe('process', () => {
let data: any;
let result: any;
describe('when data is valid, but not a real type', () => {
beforeEach(() => {
data = {
type: 'NotARealType',
_links: {
self: { href: 'data-selflink' }
}
};
});
it('should not throw an error', () => {
expect(() => { result = service.process(data, request) }).not.toThrow();
});
it('should return undefined', () => {
result = service.process(data, request);
expect(result).toBeUndefined();
});
it('should not call objectCache add', () => {
result = service.process(data, request);
expect(objectCache.add).not.toHaveBeenCalled();
});
});
});
});
/* tslint:enable:max-classes-per-file */