Skip to content
Snippets Groups Projects
detail.service.spec.ts 2.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { TestBed, inject } from '@angular/core/testing';
    import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
    
    import { DetailService } from './detail.service';
    import { AppConfigService } from 'src/app/app-config.service';
    
    describe('DetailService', () => {
        let service: DetailService;
    
        beforeEach(() => {
            TestBed.configureTestingModule({
                imports: [HttpClientTestingModule],
                providers: [
                    { provide: AppConfigService, useValue: { apiUrl: 'http://testing.com' } },
                    DetailService
                ]
            });
            service = TestBed.inject(DetailService);
        });
    
        it('#retrieveData() should return an Observable<any[]>',
            inject([HttpTestingController, DetailService],(httpMock: HttpTestingController, detailervice: DetailService) => {
                    const mockResponse = ['myData'];
    
                    detailervice.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();
        //         }
        //     )
        // );
    });