From 5cd83228c519b276ab60e91dc716fcc7b36d6b8a Mon Sep 17 00:00:00 2001 From: Andreas Loew Date: Tue, 26 Nov 2024 14:06:01 +0100 Subject: [PATCH] Added tests to reproduce #1388 - but everything seems to be ok. --- .../src/lib/translate.pipe-standalone.spec.ts | 41 +++++++++++++++++++ .../src/lib/translate.service.spec.ts | 22 ++++++++++ 2 files changed, 63 insertions(+) diff --git a/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts b/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts index e4591d6..376386d 100644 --- a/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts +++ b/projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts @@ -40,6 +40,23 @@ class FakeChangeDetectorRef extends ChangeDetectorRef { } } +@Injectable() +@Component({ + selector: 'lib-hmx-app', + standalone: true, + imports: [TranslatePipe], + changeDetection: ChangeDetectionStrategy.OnPush, + template: `{{'default' | translate}}` +}) +class AppTranslationIdDefaultComponent { + viewContainerRef: ViewContainerRef; + + constructor(viewContainerRef: ViewContainerRef) { + this.viewContainerRef = viewContainerRef; + } +} + + @Injectable() @Component({ selector: 'lib-hmx-app', @@ -123,6 +140,15 @@ describe('TranslatePipe (standalone)', () => { expect(translatePipe.transform('TEST')).toEqual("This is a test"); }); + it("should translate 'default'", () => + { + prepare(); + translate.setTranslation("en", {"default": "This is the default message"}); + translate.use("en"); + + expect(translatePipe.transform("default")).toEqual("This is the default message"); + }); + it('should call markForChanges when it translates a string', () => { prepare(); translate.setTranslation('en', {"TEST": "This is a test"}); @@ -374,5 +400,20 @@ describe('TranslatePipe (standalone)', () => { fixture.detectChanges(); expect(fixture.debugElement.nativeElement.innerHTML).toEqual("This is a test"); }); + + + it("translate text with 'default' message id" , () => { + prepare(); + + const fixture = TestBed.createComponent(AppTranslationIdDefaultComponent); + + translate.setTranslation("en", {"default": "This is some default text"}); + + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement.innerHTML).toEqual("default"); + translate.setDefaultLang('en'); + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement.innerHTML).toEqual("This is some default text"); + }); }); }); diff --git a/projects/ngx-translate/src/lib/translate.service.spec.ts b/projects/ngx-translate/src/lib/translate.service.spec.ts index c438979..808e481 100644 --- a/projects/ngx-translate/src/lib/translate.service.spec.ts +++ b/projects/ngx-translate/src/lib/translate.service.spec.ts @@ -165,6 +165,18 @@ describe("TranslateService", () => }); }); + it("translates text using 'default' translation Id", () => + { + translations = {"default": "Default text"}; + translate.use("en"); + + // this will request the translation from the backend because we use a static files loader for TranslateService + translate.get("default").subscribe((res: Translation) => + { + expect(res).toEqual("Default text"); + }); + }); + it("should be able to get an array translations", () => { translations = {"TEST": "This is a test", "TEST2": "This is another test2"}; @@ -702,6 +714,16 @@ describe("TranslateService", () => }); translate.set("TEST", "This is a test"); }); + + + it("should translate 'default'", () => + { + translate.setTranslation("en", {"default": "This is the default message"}); + translate.use("en"); + + expect(translate.instant("default")).toEqual("This is the default message"); + }); + }); it("should trigger an event when the lang changes", () =>