diff --git a/package-lock.json b/package-lock.json index e0125c59b50a6635fd71d61adc2f432cb537e7ea..626a195d1fafef66311b93d02fc10cd8dfb7cb27 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,6 +32,7 @@ "dotenv": "^16.0.3", "ejs": "^3.1.8", "form-data": "^4.0.0", + "lodash": "^4.17.21", "luxon": "^1.25.0", "migrate": "^1.8.0", "mongoose": "^6.7.3", diff --git a/package.json b/package.json index 0222d89ecb791d49e72d85153b48eb457365e189..84f7f9543658026cfdb3165a5489fca7600f86fa 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "dotenv": "^16.0.3", "ejs": "^3.1.8", "form-data": "^4.0.0", + "lodash": "^4.17.21", "luxon": "^1.25.0", "migrate": "^1.8.0", "mongoose": "^6.7.3", diff --git a/src/mailer/mailer.service.spec.ts b/src/mailer/mailer.service.spec.ts index c6798f184c998edc210040b14965b2bb01820f24..8ff808c839b9b02a340c73fd70bd32872d06e946 100644 --- a/src/mailer/mailer.service.spec.ts +++ b/src/mailer/mailer.service.spec.ts @@ -9,16 +9,25 @@ import { MailerService } from './mailer.service'; describe('MailerService', () => { let mailerService: MailerService; - let httpService: HttpService; + const httpServiceMock = { + get: jest.fn(), + post: jest.fn(), + }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [HttpModule], - providers: [MailerService, ConfigurationService], + providers: [ + MailerService, + ConfigurationService, + { + provide: HttpService, + useValue: httpServiceMock, + }, + ], }).compile(); mailerService = module.get<MailerService>(MailerService); - httpService = module.get<HttpService>(HttpService); }); it('should be defined', () => { @@ -40,7 +49,7 @@ describe('MailerService', () => { headers: {}, config: {}, }; - jest.spyOn(httpService, 'post').mockImplementationOnce(() => of(result)); + httpServiceMock.post.mockImplementationOnce(() => of(result)); expect(await mailerService.send('a@a.com', 'test', '<p>This is a test</p>')).toBe(result.data); }); @@ -58,7 +67,7 @@ describe('MailerService', () => { headers: {}, config: {}, }; - jest.spyOn(httpService, 'post').mockImplementationOnce(() => throwError(result)); + httpServiceMock.post.mockImplementationOnce(() => throwError(result)); try { await mailerService.send('a@a.com', 'test', '<p>This is a test</p>'); } catch (e) { diff --git a/src/posts/posts.service.ts b/src/posts/posts.service.ts index 34e32b5764002064aefb37494986e5e856df9995..e0480d7212fb79c1c78513e6b8139ce8aebd7fa5 100644 --- a/src/posts/posts.service.ts +++ b/src/posts/posts.service.ts @@ -26,7 +26,7 @@ export class PostsService { /** * Get all tags with admin endpoint, including unused ones */ - public getTags(): Promise<Tag[]> { + public async getTags(): Promise<Tag[]> { return this.api.tags.browse({ limit: 'all' }); } diff --git a/src/structures/services/structures-import.service.spec.ts b/src/structures/services/structures-import.service.spec.ts index 60d5d6e736730d45e3fbc61bcde5506aa914cfd7..f910e46d17f478a0522c7156e494c443a4e4673e 100644 --- a/src/structures/services/structures-import.service.spec.ts +++ b/src/structures/services/structures-import.service.spec.ts @@ -12,11 +12,14 @@ import { StructuresImportService } from './structures-import.service'; describe('StructuresImportService', () => { let structuresImportService: StructuresImportService; - let httpService: HttpService; let structuresSearchService: StructuresSearchService; let structuresService: StructuresService; let mailerService: MailerService; + const httpServiceMock = { + get: jest.fn(), + }; + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ @@ -58,11 +61,14 @@ describe('StructuresImportService', () => { save: jest.fn(), }, }, + { + provide: HttpService, + useValue: httpServiceMock, + }, ], }).compile(); structuresImportService = module.get<StructuresImportService>(StructuresImportService); - httpService = module.get<HttpService>(HttpService); structuresSearchService = module.get<StructuresSearchService>(StructuresSearchService); structuresService = module.get<StructuresService>(StructuresService); mailerService = module.get<MailerService>(MailerService); @@ -90,7 +96,7 @@ describe('StructuresImportService', () => { const doesAlreadyExistSpy = jest .spyOn(structuresImportService as any, 'doesAlreadyExist') .mockResolvedValueOnce(false); - jest.spyOn(httpService, 'get').mockImplementationOnce(() => of(mockedAxiosResponse)); + httpServiceMock.get.mockImplementationOnce(() => of(mockedAxiosResponse)); jest.spyOn(structuresSearchService, 'indexStructure').mockResolvedValueOnce(undefined); @@ -98,7 +104,7 @@ describe('StructuresImportService', () => { await structuresImportService.importDataGouvStructures(); - expect(httpService.get).toBeCalled(); + expect(httpServiceMock.get).toBeCalled(); expect(formatToResinSchemaSpy).toBeCalled(); expect(doesAlreadyExistSpy).toBeCalled(); expect(saveSpy).toBeCalled(); diff --git a/src/structures/services/structures-search.service.spec.ts b/src/structures/services/structures-search.service.spec.ts index 670e459fc68f95cd20b2606b38d6b5b24526d24e..813c602fd7fe42fcd3f053040912b439c7a61524 100644 --- a/src/structures/services/structures-search.service.spec.ts +++ b/src/structures/services/structures-search.service.spec.ts @@ -43,7 +43,6 @@ describe('StructuresSearchService', () => { it('should be defined', () => { expect(structureSearchService).toBeDefined(); - expect(structureSearchService).toBeDefined(); }); describe('search method', () => { diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index acc58a041d3f8170a061ac30e84f744c61764f6e..aa381e2b40bc8410db56c7e8e038ae1ffda09b89 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -1099,9 +1099,9 @@ export class StructuresService { */ public getCNFSStructures(): Promise<Array<CNFSStructure>> { this.logger.debug(`get CNFS structures`); - const obs = this.httpService.get('https://api.conseiller-numerique.gouv.fr/permanences').pipe( - map((response: AxiosResponse<Array<CNFSStructure>>) => - response.data.filter((structure: CNFSStructure) => { + const obs = this.httpService.get<Array<CNFSStructure>>('https://api.conseiller-numerique.gouv.fr/permanences').pipe( + map((response) => + response.data.filter((structure) => { if (structure.code_postal.substring(0, 2) === '69' && (structure.courriel || structure.telephone)) return structure; })