Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoMolinaro committed Jan 9, 2024
1 parent 3149842 commit d16bf49
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/app/core/cache/builders/build-decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HALLink } from '../../shared/hal-link.model';
import { HALResource } from '../../shared/hal-resource.model';
import { ResourceType } from '../../shared/resource-type';
import { getLinkDefinition, link } from './build-decorators';
import { dataService, getLinkDefinition, link } from './build-decorators';

class TestHALResource implements HALResource {
_links: {
Expand Down Expand Up @@ -46,5 +46,11 @@ describe('build decorators', () => {
expect(result).toBeUndefined();
});
});

describe(`set data service`, () => {
it(`should throw error`, () => {
expect(dataService(null)).toThrow();
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { TestScheduler } from 'rxjs/testing';
import { SuggestionDataServiceImpl, SuggestionsDataService } from './suggestions-data.service';
import { RequestService } from '../../data/request.service';
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../../cache/object-cache.service';
import { HALEndpointService } from '../../shared/hal-endpoint.service';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { HttpClient } from '@angular/common/http';
import { DefaultChangeAnalyzer } from '../../data/default-change-analyzer.service';
import { Suggestion } from './models/suggestion.model';
import { cold, getTestScheduler } from 'jasmine-marbles';
import { RequestEntry } from '../../data/request-entry.model';
import { RestResponse } from '../../cache/response.models';
import { of as observableOf } from 'rxjs';
import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
import { RemoteData } from '../../data/remote-data';
import { RequestEntryState } from '../../data/request-entry-state.model';
import { SuggestionSource } from './models/suggestion-source.model';
import { SuggestionTarget } from './models/suggestion-target.model';
import { SuggestionSourceDataService } from './source/suggestion-source-data.service';
import { SuggestionTargetDataService } from './target/suggestion-target-data.service';
import { RequestParam } from '../../cache/models/request-param.model';

describe('SuggestionDataService test', () => {
let scheduler: TestScheduler;
let service: SuggestionsDataService;
let requestService: RequestService;
let rdbService: RemoteDataBuildService;
let objectCache: ObjectCacheService;
let halService: HALEndpointService;
let notificationsService: NotificationsService;
let http: HttpClient;
let comparatorSuggestion: DefaultChangeAnalyzer<Suggestion>;
let comparatorSuggestionSource: DefaultChangeAnalyzer<SuggestionSource>;
let comparatorSuggestionTarget: DefaultChangeAnalyzer<SuggestionTarget>;
let suggestionSourcesDataService: SuggestionSourceDataService;
let suggestionTargetsDataService: SuggestionTargetDataService;
let suggestionsDataService: SuggestionDataServiceImpl;
let responseCacheEntry: RequestEntry;


const testSource = 'test-source';
const testUserId = '1234-4321';
const endpointURL = `https://rest.api/rest/api/`;
const requestUUID = '8b3c613a-5a4b-438b-9686-be1d5b4a1c5a';
const remoteDataMocks = {
Success: new RemoteData(null, null, null, RequestEntryState.Success, null, null, 200),
};

function initTestService() {
return new SuggestionsDataService(
requestService,
rdbService,
objectCache,
halService,
notificationsService,
http,
comparatorSuggestion,
comparatorSuggestionSource,
comparatorSuggestionTarget
);
}

beforeEach(() => {
scheduler = getTestScheduler();

objectCache = {} as ObjectCacheService;
http = {} as HttpClient;
notificationsService = {} as NotificationsService;
comparatorSuggestion = {} as DefaultChangeAnalyzer<Suggestion>;
comparatorSuggestionTarget = {} as DefaultChangeAnalyzer<SuggestionTarget>;
comparatorSuggestionSource = {} as DefaultChangeAnalyzer<SuggestionSource>;
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),
setStaleByHrefSubstring: observableOf(true)
});

halService = jasmine.createSpyObj('halService', {
getEndpoint: observableOf(endpointURL)
});

rdbService = jasmine.createSpyObj('rdbService', {
buildSingle: createSuccessfulRemoteDataObject$({}, 500),
buildList: cold('a', { a: remoteDataMocks.Success })
});


suggestionSourcesDataService = jasmine.createSpyObj('suggestionSourcesDataService', {
getSources: observableOf(null),
});

suggestionTargetsDataService = jasmine.createSpyObj('suggestionTargetsDataService', {
getTargets: observableOf(null),
getTargetsByUser: observableOf(null),
findById: observableOf(null),
});

suggestionsDataService = jasmine.createSpyObj('suggestionsDataService', {
searchBy: observableOf(null),
delete: observableOf(null),
});


service = initTestService();
/* eslint-disable-next-line @typescript-eslint/dot-notation */
service['suggestionSourcesDataService'] = suggestionSourcesDataService;
/* eslint-disable-next-line @typescript-eslint/dot-notation */
service['suggestionTargetsDataService'] = suggestionTargetsDataService;
/* eslint-disable-next-line @typescript-eslint/dot-notation */
service['suggestionsDataService'] = suggestionsDataService;
});

describe('Suggestion targets service', () => {
it('should call suggestionSourcesDataService.getTargets', () => {
const options = {
searchParams: [new RequestParam('source', testSource)]
};
service.getTargets(testSource);
expect(suggestionTargetsDataService.getTargets).toHaveBeenCalledWith('findBySource', options);
});

it('should call suggestionSourcesDataService.getTargetsByUser', () => {
const options = {
searchParams: [new RequestParam('target', testUserId)]
};
service.getTargetsByUser(testUserId);
expect(suggestionTargetsDataService.getTargetsByUser).toHaveBeenCalledWith(testUserId, options);
});

it('should call suggestionSourcesDataService.getTargetById', () => {
service.getTargetById('1');
expect(suggestionTargetsDataService.findById).toHaveBeenCalledWith('1');
});
});


describe('Suggestion sources service', () => {
it('should call suggestionSourcesDataService.getSources', () => {
service.getSources();
expect(suggestionSourcesDataService.getSources).toHaveBeenCalled();
});
});

describe('Suggestion service', () => {
it('should call suggestionsDataService.searchBy', () => {
const options = {
searchParams: [new RequestParam('target', testUserId), new RequestParam('source', testSource)]
};
service.getSuggestionsByTargetAndSource(testUserId, testSource);
expect(suggestionsDataService.searchBy).toHaveBeenCalledWith('findByTargetAndSource', options, true, true);
});

it('should call suggestionsDataService.delete', () => {
service.deleteSuggestion('1');
expect(suggestionsDataService.delete).toHaveBeenCalledWith('1');
});
});

describe('Request service', () => {
it('should call requestService.setStaleByHrefSubstring', () => {
service.clearSuggestionRequests();
expect(requestService.setStaleByHrefSubstring).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class SuggestionsDataService {
}

/**
* Return the list of Suggestion Target
* Return the list of Suggestion Sources
*
* @param options
* Find list options object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { DSpaceObjectType } from '../../../core/shared/dspace-object-type.model'
import { Item } from '../../../core/shared/item.model';
import { DSOSelectorModalWrapperComponent, SelectorActionType } from './dso-selector-modal-wrapper.component';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, ActivatedRouteSnapshot } from '@angular/router';
import { DSpaceObject } from '../../../core/shared/dspace-object.model';
import { By } from '@angular/platform-browser';
import { DSOSelectorComponent } from '../dso-selector/dso-selector.component';
import { MockComponent } from 'ng-mocks';
import { MetadataValue } from '../../../core/shared/metadata.models';
import { createSuccessfulRemoteDataObject } from '../../remote-data.utils';
import { hasValue } from '../../empty.util';

describe('DSOSelectorModalWrapperComponent', () => {
let component: DSOSelectorModalWrapperComponent;
Expand Down Expand Up @@ -83,6 +84,20 @@ describe('DSOSelectorModalWrapperComponent', () => {
});
});

describe('selectObject with emit only', () => {
beforeEach(() => {
spyOn(component, 'navigate');
spyOn(component, 'close');
spyOn(component.select, 'emit');
component.emitOnly = true;
component.selectObject(item);
});
it('should call the close and navigate method on the component with the given DSO', () => {
expect(component.close).toHaveBeenCalled();
expect(component.select.emit).toHaveBeenCalledWith(item);
});
});

describe('close', () => {
beforeEach(() => {
component.close();
Expand Down Expand Up @@ -113,6 +128,19 @@ describe('DSOSelectorModalWrapperComponent', () => {
expect(component.close).toHaveBeenCalled();
});
});

describe('should find route data', () => {
beforeEach(() => {
spyOn(component, 'findRouteData');
component.ngOnInit();
});
it('should call the findRouteData method on the component', () => {
expect(component.findRouteData).toHaveBeenCalled();
});
it('should return undefined', () => {
expect(component.findRouteData((route) => hasValue(route.data), {} as unknown as ActivatedRouteSnapshot)).toEqual(undefined);
});
});
});

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,31 @@ describe('FilterInputSuggestionsComponent', () => {
expect(comp.onClickSuggestion).toHaveBeenCalledWith(suggestions[clickedIndex]);
});
});

describe('component methods', () => {
const testData = {
value: 'test-field'
} as unknown as any;

beforeEach(() => {
spyOn(comp.submitSuggestion, 'emit');
spyOn(comp.clickSuggestion, 'emit');
spyOn(comp, 'close');
});

it('should properly submit', () => {
comp.onSubmit(testData);
expect(comp.submitSuggestion.emit).toHaveBeenCalledWith(testData);
expect(comp.value).toBe(testData);
});

it('should update value on suggestion clicked', () => {
comp.onClickSuggestion(testData);
expect(comp.clickSuggestion.emit).toHaveBeenCalledWith(testData);
expect(comp.value).toBe(testData.value);
expect(comp.blockReopen).toBeTruthy();
expect(comp.close).toHaveBeenCalled();
});
});

});

0 comments on commit d16bf49

Please sign in to comment.