From a152ecef209b9d7f073491581d1ee6a225618bf3 Mon Sep 17 00:00:00 2001 From: Kim Tran Date: Thu, 8 Feb 2024 10:03:41 +0100 Subject: [PATCH] fix: mock does not necessarily need TranslationModule to be provided (#119) * feat: mock for message service * fix: imports in mocks * fix: ignore eslint rule next line nx/enforce-module-boundaries * fix: mock does not necessarily need TranslationModule to be provided --------- Co-authored-by: kim.tran --- .../mocks/portal-message-service-mock.ts | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/libs/portal-integration-angular/mocks/portal-message-service-mock.ts b/libs/portal-integration-angular/mocks/portal-message-service-mock.ts index 563d1220..1630c1f3 100644 --- a/libs/portal-integration-angular/mocks/portal-message-service-mock.ts +++ b/libs/portal-integration-angular/mocks/portal-message-service-mock.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core' +import { Injectable, Optional } from '@angular/core' import { TranslateService } from '@ngx-translate/core' import { combineLatest, of } from 'rxjs' // eslint-disable-next-line @@ -15,7 +15,7 @@ export function providePortalMessageServiceMock() { @Injectable() export class PortalMessageServiceMock { - constructor(private translateService: TranslateService) {} + constructor(@Optional() private translateService?: TranslateService) {} lastMessages: { type: 'success' | 'info' | 'error' | 'warning'; value: Message }[] = [] message$ = new FakeTopic() @@ -40,16 +40,20 @@ export class PortalMessageServiceMock { } private addTranslated(severity: string, msg: Message) { - combineLatest([ - msg.summaryKey ? this.translateService.get(msg.summaryKey || '', msg.summaryParameters) : of(undefined), - msg.detailKey ? this.translateService.get(msg.detailKey, msg.detailParameters) : of(undefined), - ]).subscribe(([summaryTranslation, detailTranslation]: string[]) => { - this.message$.publish({ - ...msg, - severity: severity, - summary: summaryTranslation, - detail: detailTranslation, + if (this.translateService) { + combineLatest([ + msg.summaryKey ? this.translateService.get(msg.summaryKey || '', msg.summaryParameters) : of(undefined), + msg.detailKey ? this.translateService.get(msg.detailKey, msg.detailParameters) : of(undefined), + ]).subscribe(([summaryTranslation, detailTranslation]: string[]) => { + this.message$.publish({ + ...msg, + severity: severity, + summary: summaryTranslation, + detail: detailTranslation, + }) }) - }) + } else { + console.log('TranslationModule not imported in TestBed. Therefore message$ is not updated.') + } } }