Skip to content

Commit

Permalink
fix: mock does not necessarily need TranslationModule to be provided (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
KimFFVII and kim.tran authored Feb 8, 2024
1 parent 90ff3b3 commit a152ece
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<TopicMessage>()

Expand All @@ -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.')
}
}
}

0 comments on commit a152ece

Please sign in to comment.