mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
refactor and add tests
This commit is contained in:
144
src/app/core/data/update-data-service.spec.ts
Normal file
144
src/app/core/data/update-data-service.spec.ts
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
import { of as observableOf } from 'rxjs';
|
||||||
|
import { TestScheduler } from 'rxjs/testing';
|
||||||
|
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
||||||
|
import { ObjectCacheService } from '../cache/object-cache.service';
|
||||||
|
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||||
|
import { RequestService } from './request.service';
|
||||||
|
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
|
||||||
|
import { HrefOnlyDataService } from './href-only-data.service';
|
||||||
|
import { getMockHrefOnlyDataService } from '../../shared/mocks/href-only-data.service.mock';
|
||||||
|
import { RestResponse } from '../cache/response.models';
|
||||||
|
import { cold, getTestScheduler, hot } from 'jasmine-marbles';
|
||||||
|
import { Item } from '../shared/item.model';
|
||||||
|
import { Version } from '../shared/version.model';
|
||||||
|
import { VersionHistory } from '../shared/version-history.model';
|
||||||
|
import { RequestEntry } from './request-entry.model';
|
||||||
|
import { testPatchDataImplementation } from './base/patch-data.spec';
|
||||||
|
import { UpdateDataServiceImpl } from './update-data-service';
|
||||||
|
import { testSearchDataImplementation } from './base/search-data.spec';
|
||||||
|
import { testDeleteDataImplementation } from './base/delete-data.spec';
|
||||||
|
import { testCreateDataImplementation } from './base/create-data.spec';
|
||||||
|
import { testFindAllDataImplementation } from './base/find-all-data.spec';
|
||||||
|
import { testPutDataImplementation } from './base/put-data.spec';
|
||||||
|
import { NotificationsService } from '../../shared/notifications/notifications.service';
|
||||||
|
|
||||||
|
describe('VersionDataService test', () => {
|
||||||
|
let scheduler: TestScheduler;
|
||||||
|
let service: UpdateDataServiceImpl<any>;
|
||||||
|
let requestService: RequestService;
|
||||||
|
let rdbService: RemoteDataBuildService;
|
||||||
|
let objectCache: ObjectCacheService;
|
||||||
|
let halService: HALEndpointService;
|
||||||
|
let hrefOnlyDataService: HrefOnlyDataService;
|
||||||
|
let responseCacheEntry: RequestEntry;
|
||||||
|
|
||||||
|
const notificationsService = {} as NotificationsService;
|
||||||
|
|
||||||
|
const item = Object.assign(new Item(), {
|
||||||
|
id: '1234-1234',
|
||||||
|
uuid: '1234-1234',
|
||||||
|
bundles: observableOf({}),
|
||||||
|
metadata: {
|
||||||
|
'dc.title': [
|
||||||
|
{
|
||||||
|
language: 'en_US',
|
||||||
|
value: 'This is just another title'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'dc.type': [
|
||||||
|
{
|
||||||
|
language: null,
|
||||||
|
value: 'Article'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'dc.contributor.author': [
|
||||||
|
{
|
||||||
|
language: 'en_US',
|
||||||
|
value: 'Smith, Donald'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'dc.date.issued': [
|
||||||
|
{
|
||||||
|
language: null,
|
||||||
|
value: '2015-06-26'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const versionHistory = Object.assign(new VersionHistory(), {
|
||||||
|
id: '1',
|
||||||
|
draftVersion: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const mockVersion: Version = Object.assign(new Version(), {
|
||||||
|
item: createSuccessfulRemoteDataObject$(item),
|
||||||
|
versionhistory: createSuccessfulRemoteDataObject$(versionHistory),
|
||||||
|
version: 1,
|
||||||
|
});
|
||||||
|
const mockVersionRD = createSuccessfulRemoteDataObject(mockVersion);
|
||||||
|
|
||||||
|
const endpointURL = `https://rest.api/rest/api/versioning/versions`;
|
||||||
|
const requestUUID = '8b3c613a-5a4b-438b-9686-be1d5b4a1c5a';
|
||||||
|
|
||||||
|
objectCache = {} as ObjectCacheService;
|
||||||
|
const comparatorEntry = {} as any;
|
||||||
|
function initTestService() {
|
||||||
|
hrefOnlyDataService = getMockHrefOnlyDataService();
|
||||||
|
return new UpdateDataServiceImpl(
|
||||||
|
'testLinkPath',
|
||||||
|
requestService,
|
||||||
|
rdbService,
|
||||||
|
objectCache,
|
||||||
|
halService,
|
||||||
|
notificationsService,
|
||||||
|
comparatorEntry,
|
||||||
|
10 * 1000
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
|
||||||
|
scheduler = getTestScheduler();
|
||||||
|
|
||||||
|
halService = jasmine.createSpyObj('halService', {
|
||||||
|
getEndpoint: cold('a', { a: endpointURL })
|
||||||
|
});
|
||||||
|
responseCacheEntry = new RequestEntry();
|
||||||
|
responseCacheEntry.request = { href: 'https://rest.api/' } as any;
|
||||||
|
responseCacheEntry.response = new RestResponse(true, 200, 'Success');
|
||||||
|
|
||||||
|
requestService = jasmine.createSpyObj('requestService', {
|
||||||
|
generateRequestId: requestUUID,
|
||||||
|
send: true,
|
||||||
|
removeByHrefSubstring: {},
|
||||||
|
getByHref: observableOf(responseCacheEntry),
|
||||||
|
getByUUID: observableOf(responseCacheEntry),
|
||||||
|
});
|
||||||
|
rdbService = jasmine.createSpyObj('rdbService', {
|
||||||
|
buildSingle: hot('(a|)', {
|
||||||
|
a: mockVersionRD
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
service = initTestService();
|
||||||
|
|
||||||
|
spyOn((service as any), 'findById').and.callThrough();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
service = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('composition', () => {
|
||||||
|
const initService = () => new UpdateDataServiceImpl(null, null, null, null, null, null, null, null);
|
||||||
|
|
||||||
|
testPatchDataImplementation(initService);
|
||||||
|
testSearchDataImplementation(initService);
|
||||||
|
testDeleteDataImplementation(initService);
|
||||||
|
testCreateDataImplementation(initService);
|
||||||
|
testFindAllDataImplementation(initService);
|
||||||
|
testPutDataImplementation(initService);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
@@ -1,5 +1,3 @@
|
|||||||
import { HttpClient } from '@angular/common/http';
|
|
||||||
import { Store } from '@ngrx/store';
|
|
||||||
import { Operation } from 'fast-json-patch';
|
import { Operation } from 'fast-json-patch';
|
||||||
import { AsyncSubject, from as observableFrom, Observable } from 'rxjs';
|
import { AsyncSubject, from as observableFrom, Observable } from 'rxjs';
|
||||||
import {
|
import {
|
||||||
@@ -29,7 +27,6 @@ import { RequestService } from './request.service';
|
|||||||
import { RestRequestMethod } from './rest-request-method';
|
import { RestRequestMethod } from './rest-request-method';
|
||||||
import { NoContent } from '../shared/NoContent.model';
|
import { NoContent } from '../shared/NoContent.model';
|
||||||
import { CacheableObject } from '../cache/cacheable-object.model';
|
import { CacheableObject } from '../cache/cacheable-object.model';
|
||||||
import { CoreState } from '../core-state.model';
|
|
||||||
import { FindListOptions } from './find-list-options.model';
|
import { FindListOptions } from './find-list-options.model';
|
||||||
import { FindAllData, FindAllDataImpl } from './base/find-all-data';
|
import { FindAllData, FindAllDataImpl } from './base/find-all-data';
|
||||||
import { SearchData, SearchDataImpl } from './base/search-data';
|
import { SearchData, SearchDataImpl } from './base/search-data';
|
||||||
@@ -70,10 +67,7 @@ export interface UpdateDataService<T> {
|
|||||||
* invalidateByHref - invalidate the href making all requests as stale
|
* invalidateByHref - invalidate the href making all requests as stale
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export abstract class UpdateDataServiceImpl<T extends CacheableObject> extends IdentifiableDataService<T> implements FindAllData<T>, SearchData<T>, CreateData<T>, PatchData<T>, PutData<T>, DeleteData<T> {
|
export class UpdateDataServiceImpl<T extends CacheableObject> extends IdentifiableDataService<T> implements FindAllData<T>, SearchData<T>, CreateData<T>, PatchData<T>, PutData<T>, DeleteData<T> {
|
||||||
protected abstract store: Store<CoreState>;
|
|
||||||
protected abstract http: HttpClient;
|
|
||||||
|
|
||||||
private findAllData: FindAllDataImpl<T>;
|
private findAllData: FindAllDataImpl<T>;
|
||||||
private searchData: SearchDataImpl<T>;
|
private searchData: SearchDataImpl<T>;
|
||||||
private createData: CreateDataImpl<T>;
|
private createData: CreateDataImpl<T>;
|
||||||
@@ -146,23 +140,6 @@ export abstract class UpdateDataServiceImpl<T extends CacheableObject> extends I
|
|||||||
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
|
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an observable of {@link RemoteData} of an object, based on its ID, with a list of
|
|
||||||
* {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
|
|
||||||
* @param id ID of object we want to retrieve
|
|
||||||
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
|
|
||||||
* no valid cached version. Defaults to true
|
|
||||||
* @param reRequestOnStale Whether or not the request should automatically be re-
|
|
||||||
* requested after the response becomes stale
|
|
||||||
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
|
|
||||||
* {@link HALLink}s should be automatically resolved
|
|
||||||
*/
|
|
||||||
findById(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<T>[]): Observable<RemoteData<T>> {
|
|
||||||
const href$ = this.getIDHrefObs(encodeURIComponent(id), ...linksToFollow);
|
|
||||||
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make a new FindListRequest with given search method
|
* Make a new FindListRequest with given search method
|
||||||
*
|
*
|
||||||
|
@@ -1,12 +1,10 @@
|
|||||||
import { SuggestionsService } from './suggestions.service';
|
import { SuggestionsService } from './suggestions.service';
|
||||||
import { AuthService } from '../core/auth/auth.service';
|
|
||||||
import { ResearcherProfileDataService } from '../core/profile/researcher-profile-data.service';
|
import { ResearcherProfileDataService } from '../core/profile/researcher-profile-data.service';
|
||||||
import {
|
import {
|
||||||
SuggestionsDataService
|
SuggestionsDataService
|
||||||
} from '../core/suggestion-notifications/suggestions-data.service';
|
} from '../core/suggestion-notifications/suggestions-data.service';
|
||||||
import {
|
|
||||||
SuggestionSourceDataService
|
|
||||||
} from '../core/suggestion-notifications/source/suggestion-source-data.service';
|
|
||||||
import {
|
import {
|
||||||
SuggestionTargetDataService
|
SuggestionTargetDataService
|
||||||
} from '../core/suggestion-notifications/target/suggestion-target-data.service';
|
} from '../core/suggestion-notifications/target/suggestion-target-data.service';
|
||||||
@@ -29,10 +27,8 @@ import {
|
|||||||
describe('SuggestionsService test', () => {
|
describe('SuggestionsService test', () => {
|
||||||
let scheduler: TestScheduler;
|
let scheduler: TestScheduler;
|
||||||
let service: SuggestionsService;
|
let service: SuggestionsService;
|
||||||
let authService: AuthService;
|
|
||||||
let researcherProfileService: ResearcherProfileDataService;
|
let researcherProfileService: ResearcherProfileDataService;
|
||||||
let suggestionsDataService: SuggestionsDataService;
|
let suggestionsDataService: SuggestionsDataService;
|
||||||
let suggestionSourceDataService: SuggestionSourceDataService;
|
|
||||||
let suggestionTargetDataService: SuggestionTargetDataService;
|
let suggestionTargetDataService: SuggestionTargetDataService;
|
||||||
let translateService: any = {
|
let translateService: any = {
|
||||||
instant: (str) => str,
|
instant: (str) => str,
|
||||||
@@ -53,10 +49,8 @@ describe('SuggestionsService test', () => {
|
|||||||
|
|
||||||
function initTestService() {
|
function initTestService() {
|
||||||
return new SuggestionsService(
|
return new SuggestionsService(
|
||||||
authService,
|
|
||||||
researcherProfileService,
|
researcherProfileService,
|
||||||
suggestionsDataService,
|
suggestionsDataService,
|
||||||
suggestionSourceDataService,
|
|
||||||
suggestionTargetDataService,
|
suggestionTargetDataService,
|
||||||
translateService
|
translateService
|
||||||
);
|
);
|
||||||
@@ -65,11 +59,6 @@ describe('SuggestionsService test', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
scheduler = getTestScheduler();
|
scheduler = getTestScheduler();
|
||||||
|
|
||||||
|
|
||||||
suggestionSourceDataService = jasmine.createSpyObj('suggestionSourcesDataService', {
|
|
||||||
getSources: observableOf(null),
|
|
||||||
});
|
|
||||||
|
|
||||||
researcherProfileService = jasmine.createSpyObj('researcherProfileService', {
|
researcherProfileService = jasmine.createSpyObj('researcherProfileService', {
|
||||||
findById: createSuccessfulRemoteDataObject$(mockResercherProfile as ResearcherProfile),
|
findById: createSuccessfulRemoteDataObject$(mockResercherProfile as ResearcherProfile),
|
||||||
findRelatedItemId: observableOf('1234'),
|
findRelatedItemId: observableOf('1234'),
|
||||||
|
Reference in New Issue
Block a user