diff --git a/client/src/app/instance/store/services/search.service.spec.ts b/client/src/app/instance/store/services/search.service.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..0e37a54148793e401d633de6f8739eaac6b8435a --- /dev/null +++ b/client/src/app/instance/store/services/search.service.spec.ts @@ -0,0 +1,58 @@ +import { TestBed, inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; + +import { SearchService } from './search.service'; +import { AppConfigService } from 'src/app/app-config.service'; + +describe('SearchService', () => { + let service: SearchService; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [ + { provide: AppConfigService, useValue: { apiUrl: 'http://testing.com' } }, + SearchService + ] + }); + service = TestBed.inject(SearchService); + }); + + it('#retrieveData() should return an Observable<any[]>', + inject([HttpTestingController, SearchService],(httpMock: HttpTestingController, searchService: SearchService) => { + const mockResponse = ['myData']; + + searchService.retrieveData('myQuery').subscribe((event: any[]) => { + expect(event).toEqual(mockResponse); + }); + + const mockRequest = httpMock.expectOne('http://testing.com/search/myQuery'); + + expect(mockRequest.cancelled).toBeFalsy(); + expect(mockRequest.request.responseType).toEqual('json'); + mockRequest.flush(mockResponse); + + httpMock.verify(); + } + ) + ); + + it('#retrieveDataLength() should return an Observable<{ nb: number }[]>', + inject([HttpTestingController, SearchService],(httpMock: HttpTestingController, searchService: SearchService) => { + const mockResponse = [{ nb: 1 }]; + + searchService.retrieveDataLength('myQuery').subscribe((event: { nb: number }[]) => { + expect(event).toEqual(mockResponse); + }); + + const mockRequest = httpMock.expectOne('http://testing.com/search/myQuery'); + + expect(mockRequest.cancelled).toBeFalsy(); + expect(mockRequest.request.responseType).toEqual('json'); + mockRequest.flush(mockResponse); + + httpMock.verify(); + } + ) + ); +}); \ No newline at end of file