diff --git a/src/application/controller/papers.controller.ts b/src/application/controller/papers.controller.ts index 6d0fab4..7a18f12 100644 --- a/src/application/controller/papers.controller.ts +++ b/src/application/controller/papers.controller.ts @@ -35,7 +35,7 @@ export class PapersController { (response: SearchResultDto) => { return response.data; }, - (error: HttpException) => { + (error) => { throw error; } ); @@ -61,7 +61,7 @@ export class PapersController { (response: SearchResultDto) => { return response.data; }, - (error: HttpException) => { + (error) => { throw error; } ); diff --git a/src/test/e2e/papers.endpoint.spec.ts b/src/test/e2e/papers.controller.e2e.spec.ts similarity index 100% rename from src/test/e2e/papers.endpoint.spec.ts rename to src/test/e2e/papers.controller.e2e.spec.ts diff --git a/src/test/page.interceptor.spec.ts b/src/test/page.interceptor.spec.ts index c6fe8a7..25fafbd 100644 --- a/src/test/page.interceptor.spec.ts +++ b/src/test/page.interceptor.spec.ts @@ -1,7 +1,6 @@ import { HttpService } from "@nestjs/axios"; import { ConfigModule } from "@nestjs/config"; -import { ModuleRef } from "@nestjs/core"; -import { Test } from "@nestjs/testing"; + import { Test } from "@nestjs/testing"; import { Observable, of } from "rxjs"; import { EsTime, Order } from "src/core/domain"; import { PageDto } from "src/core/domain/dtos"; @@ -263,70 +262,57 @@ describe('Unit tests for PageInterceptor', () => { }); }); - // describe('deletePIT()', () => { - // it('Should touch HttpService.delete() method', () => { - // let httpPostMock = jest.spyOn(httpService, 'delete').mockReturnValueOnce(of({ - // data: {id: '2567'}, - // status: 0, - // statusText: '', - // headers: {}, - // config: {}, - // })); + describe('deletePIT()', () => { + it('Should touch HttpService.delete() method', () => { + let httpDeleteMock = jest.spyOn(httpService, 'delete').mockReturnValueOnce( + of({ + data: {succeeded: true}, + status: 0, + statusText: '', + headers: {}, + config: {}, + })); - // pageInter.getPIT(1); - // expect(httpPostMock).toHaveBeenCalled(); - // }); + pageInter.deletePIT(''); + expect(httpDeleteMock).toHaveBeenCalled(); + }); - // it('Should contain correct port in the URI from .env', () => { - // let httpPostMock = jest.spyOn(httpService, 'post').mockReturnValueOnce(of({ - // data: {id: '2567'}, - // status: 0, - // statusText: '', - // headers: {}, - // config: {}, - // })); + it('Should contain correct port in the URI from .env and passed PIT ID in the request body', () => { + let httpDeleteMock = jest.spyOn(httpService, 'delete').mockReturnValueOnce( + of({ + data: { succeeded: true }, + status: 0, + statusText: '', + headers: {}, + config: {}, + })); - // pageInter.getPIT(1); - // expect(httpPostMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/papers/_pit?keep_alive=1m`); - // }); + pageInter.deletePIT('thisIsIDSpecified'); + expect(httpDeleteMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/_pit`, { + data: { id: 'thisIsIDSpecified' }, + headers: { 'Content-Type': 'application/json' } + }); + }); - // it('Should touch HttpService with correct URI when time alive and time-unit are set', () => { - // let httpPostMock = jest.spyOn(httpService, 'post').mockReturnValueOnce(of({ - // data: {id: '2567'}, - // status: 0, - // statusText: '', - // headers: {}, - // config: {}, - // })); + it('Should return error exeception when HttpService fails', () => { + jest.spyOn(httpService, 'delete').mockImplementationOnce(() => { + throw HttpResponseException; + }); - // let time = 2; - // let unit = EsTime.sec; - - // pageInter.getPIT(time, unit); - // expect(httpPostMock).toHaveBeenCalledWith(`http://localhost:${process.env.ES_PORT}/papers/_pit?keep_alive=${time+unit}`); - // }); + expect(pageInter.deletePIT('')).rejects.toEqual(HttpResponseException); + }); - // it('Should return error exeception when HttpService fails', () => { - // jest.spyOn(httpService, 'post').mockImplementationOnce(() => { - // throw HttpResponseException; - // }); + it('Should return true when Elasticsearch successfully removed PIT', () => { + jest.spyOn(httpService, 'delete').mockReturnValueOnce( + of({ + data: { succeeded: true }, + status: 0, + statusText: '', + headers: {}, + config: {}, + })); - // expect(pageInter.getPIT(1)).rejects.toEqual(HttpResponseException); - // }); - - // it('Should return a non-empty string when HttpService request succeedes', () => { - // jest.spyOn(httpService, 'post').mockReturnValueOnce(of({ - // data: {id: '2567', keep_alive: '1m'}, - // status: 0, - // statusText: '', - // headers: {}, - // config: {}, - // })); - - // expect(pageInter.getPIT(1)).resolves.toEqual({ - // id: '2567', - // keep_alive: '1m', - // }); - // }); - // }); + expect(pageInter.deletePIT('')).resolves.toBe(true); + }); + }); }); \ No newline at end of file diff --git a/src/test/papers.controller.spec.ts b/src/test/papers.controller.spec.ts new file mode 100644 index 0000000..a4e4382 --- /dev/null +++ b/src/test/papers.controller.spec.ts @@ -0,0 +1,99 @@ +import { HttpModule } from "@nestjs/axios"; +import { NotFoundException } from "@nestjs/common"; +import { Test } from "@nestjs/testing"; +import { PapersController } from "src/application"; +import { SearchService } from "src/core/services/common/search.service"; + + +describe('Unit tests for PapersController', () => { + let searchService: SearchService; + let papersController: PapersController; + + beforeAll(async () => { + const moduleRef = await Test.createTestingModule({ + providers: [ + PapersController, + { + provide: SearchService, + useValue: { + findByContext: jest.fn(), + findByID: jest.fn() + } + } + ], + imports: [HttpModule] + }).compile(); + + papersController = moduleRef.get(PapersController); + searchService = moduleRef.get(SearchService); + }); + + it('Should be defined', () => { + expect(papersController).toBeDefined(); + expect(searchService).toBeDefined(); + }); + + describe('getByContext()', () => { + it('Should touch SearchService.findByContext() method', () => { + let findCtxMock = jest.spyOn(searchService, 'findByContext') + .mockResolvedValueOnce({ + data: { + took: undefined, + timed_out: undefined, + hits: undefined, + _shards: undefined, + }, + statusCode: 0 + }); + + papersController.getByContext({ query: undefined }); + expect(findCtxMock).toHaveBeenCalled(); + }); + + it('Should resolve, when searched successfully', () => { + const searchResultMock = { + took: 1, + timed_out: false, + hits: { + total: {}, + hits: [ + { + _source: { + id: 'thisIsID', + title: 'andThisIsTheTitle', + authors: ['alsoAuthors'], + topic: 'andThatIsTheTopic', + summary: 'someSummaries', + tags: ['tag1', 'tag2'], + content: 'finallyContent!' + } + } + ], + }, + _shards: undefined, + }; + + jest.spyOn(searchService, 'findByContext') + .mockResolvedValueOnce({ + data: searchResultMock, + statusCode: 200 + }); + + expect(papersController.getByContext({ query: undefined })).resolves.toEqual(searchResultMock); + }); + + it('Should throw, when search was unsuccessful', () => { + searchService.findByContext = jest.fn() + .mockRejectedValueOnce(new NotFoundException); + + expect(papersController.getByContext({ query: undefined })) + .rejects.toThrow(NotFoundException) + }); + }); + + describe('getByID()', () => { + it.todo('Should touch SearchService.findByID() method'); + it.todo('Should resolve, when searched successfully'); + it.todo('Should throw, when search was unsuccessful'); + }); +}); \ No newline at end of file