Skip to content

Commit

Permalink
Added tests to reproduce #1388 - but everything seems to be ok.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeAndWeb committed Nov 26, 2024
1 parent 7b72914 commit 5cd8322
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
41 changes: 41 additions & 0 deletions projects/ngx-translate/src/lib/translate.pipe-standalone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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"});
Expand Down Expand Up @@ -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");
});
});
});
22 changes: 22 additions & 0 deletions projects/ngx-translate/src/lib/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand Down Expand Up @@ -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", () =>
Expand Down

0 comments on commit 5cd8322

Please sign in to comment.