Skip to content

Commit

Permalink
Test: update localized routing
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Nov 12, 2018
1 parent c6ac090 commit 9087f47
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
4 changes: 3 additions & 1 deletion tests/pipes/translate.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,15 +783,17 @@ describe('TranslatePipe', () => {
it('should translate when language changes', fakeAsync(() => {
l10nLoader.load();
tick();
router.navigate(['/en/mock']);
router.initialNavigation();
tick();

const req1: TestRequest = httpMock.expectOne('./assets/locale-en.json');
req1.flush({ "Title": "Angular localization" });

expect(location.path()).toBe('/en/mock');
expect(pipe.transform('Title', locale.getCurrentLanguage())).toEqual("Angular localization");

locale.setDefaultLocale('it');
tick();

const req2: TestRequest = httpMock.expectOne('./assets/locale-it.json');
req2.flush({ "Title": "Localizzazione in Angular" });
Expand Down
108 changes: 107 additions & 1 deletion tests/services/locale.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('LocaleService', () => {
expect(location.path()).toBe('/en-US/mock');
}));

it('should keep localized url when goes back after the language has changed', fakeAsync(() => {
it('should keep localized url when goes back after the locale has changed', fakeAsync(() => {
l10nLoader.load();
tick();
router.navigate(['/mock']);
Expand Down Expand Up @@ -138,4 +138,110 @@ describe('LocaleService', () => {

});

describe('Localized routing with default routing', () => {

let l10nLoader: L10nLoader;
let locale: LocaleService;

let router: Router;
let location: Location;

const l10nConfig: L10nConfig = {
locale: {
languages: [
{ code: 'en', dir: 'ltr' },
{ code: 'it', dir: 'ltr' }
],
defaultLocale: { languageCode: 'en', countryCode: 'US' },
storage: StorageStrategy.Disabled,
localizedRouting: [ISOCode.Language, ISOCode.Country],
localizedRoutingOptions: {
defaultRouting: true
}
},
translation: {
providers: []
}
};

const routes: Route[] = [
{ path: '', redirectTo: 'mock', pathMatch: 'full' },
{ path: 'mock', component: MockComponent },
{ path: 'otherMock', component: MockComponent },
{ path: '**', redirectTo: 'mock' }
];

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [MockComponent],
imports: [
HttpClientTestingModule,
RouterTestingModule.withRoutes(routes),
LocalizationModule.forRoot(l10nConfig)
]
}).createComponent(MockComponent);

l10nLoader = TestBed.get(L10nLoader);
locale = TestBed.get(LocaleService);

router = TestBed.get(Router);
location = TestBed.get(Location);
});

it('should not replace url when the app starts', fakeAsync(() => {
l10nLoader.load();
tick();
router.initialNavigation();
tick();

expect(location.path()).toBe('/mock');
}));

it('should not replace url when navigation ends', fakeAsync(() => {
l10nLoader.load();
tick();
router.navigate(['/otherMock']);
tick();

expect(location.path()).toBe('/otherMock');
}));

it('should replace url when default locale changes', fakeAsync(() => {
l10nLoader.load();
tick();
router.navigate(['/mock']);
tick();
locale.setDefaultLocale('it', 'IT');
tick();

expect(location.path()).toBe('/it-IT/mock');
}));

it('should remove locale from url when default locale changes', fakeAsync(() => {
l10nLoader.load();
tick();
router.navigate(['/mock']);
tick();
locale.setDefaultLocale('it', 'IT');
tick();
locale.setDefaultLocale('en', 'US');
tick();

expect(location.path()).toBe('/mock');
}));

it('should not keep localized url when goes back after the locale has changed', fakeAsync(() => {
l10nLoader.load();
tick();
router.navigate(['/mock']);
tick();
locale.setDefaultLocale('it', 'IT');
tick();
location.back();

expect(location.path()).toBe('/mock');
}));

});

});

0 comments on commit 9087f47

Please sign in to comment.