Skip to content

Commit 5cd8322

Browse files
committed
Added tests to reproduce #1388 - but everything seems to be ok.
1 parent 7b72914 commit 5cd8322

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ class FakeChangeDetectorRef extends ChangeDetectorRef {
4040
}
4141
}
4242

43+
@Injectable()
44+
@Component({
45+
selector: 'lib-hmx-app',
46+
standalone: true,
47+
imports: [TranslatePipe],
48+
changeDetection: ChangeDetectionStrategy.OnPush,
49+
template: `{{'default' | translate}}`
50+
})
51+
class AppTranslationIdDefaultComponent {
52+
viewContainerRef: ViewContainerRef;
53+
54+
constructor(viewContainerRef: ViewContainerRef) {
55+
this.viewContainerRef = viewContainerRef;
56+
}
57+
}
58+
59+
4360
@Injectable()
4461
@Component({
4562
selector: 'lib-hmx-app',
@@ -123,6 +140,15 @@ describe('TranslatePipe (standalone)', () => {
123140
expect(translatePipe.transform('TEST')).toEqual("This is a test");
124141
});
125142

143+
it("should translate 'default'", () =>
144+
{
145+
prepare();
146+
translate.setTranslation("en", {"default": "This is the default message"});
147+
translate.use("en");
148+
149+
expect(translatePipe.transform("default")).toEqual("This is the default message");
150+
});
151+
126152
it('should call markForChanges when it translates a string', () => {
127153
prepare();
128154
translate.setTranslation('en', {"TEST": "This is a test"});
@@ -374,5 +400,20 @@ describe('TranslatePipe (standalone)', () => {
374400
fixture.detectChanges();
375401
expect(fixture.debugElement.nativeElement.innerHTML).toEqual("This is a test");
376402
});
403+
404+
405+
it("translate text with 'default' message id" , () => {
406+
prepare();
407+
408+
const fixture = TestBed.createComponent(AppTranslationIdDefaultComponent);
409+
410+
translate.setTranslation("en", {"default": "This is some default text"});
411+
412+
fixture.detectChanges();
413+
expect(fixture.debugElement.nativeElement.innerHTML).toEqual("default");
414+
translate.setDefaultLang('en');
415+
fixture.detectChanges();
416+
expect(fixture.debugElement.nativeElement.innerHTML).toEqual("This is some default text");
417+
});
377418
});
378419
});

projects/ngx-translate/src/lib/translate.service.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ describe("TranslateService", () =>
165165
});
166166
});
167167

168+
it("translates text using 'default' translation Id", () =>
169+
{
170+
translations = {"default": "Default text"};
171+
translate.use("en");
172+
173+
// this will request the translation from the backend because we use a static files loader for TranslateService
174+
translate.get("default").subscribe((res: Translation) =>
175+
{
176+
expect(res).toEqual("Default text");
177+
});
178+
});
179+
168180
it("should be able to get an array translations", () =>
169181
{
170182
translations = {"TEST": "This is a test", "TEST2": "This is another test2"};
@@ -702,6 +714,16 @@ describe("TranslateService", () =>
702714
});
703715
translate.set("TEST", "This is a test");
704716
});
717+
718+
719+
it("should translate 'default'", () =>
720+
{
721+
translate.setTranslation("en", {"default": "This is the default message"});
722+
translate.use("en");
723+
724+
expect(translate.instant("default")).toEqual("This is the default message");
725+
});
726+
705727
});
706728

707729
it("should trigger an event when the lang changes", () =>

0 commit comments

Comments
 (0)