Comments (9)
Hi @ihor-zinchenko ,
you have to provide the library modules such as L10nTranslationModule
. Here an example: https://github.com/robisim74/angular-l10n/blob/master/projects/angular-l10n-app/src/app/app.component.spec.ts
from angular-l10n.
@robisim74 great! thanks a lot!)
Could you please also give me advice about next case: i want to write test to my function:
public setLocale(locale: L10nLocale): Promise<void> {
return this.translation.setLocale(locale);
}
i wrote next code:
it('should setLocale() set language to english', fakeAsync(() => {
const mockLocale = {language: 'en', currency: 'USD', timeZone: 'America/Los_Angeles'};
component.setLocale(mockLocale).then(res => {
expect(component.locale).toEqual(jasmine.objectContaining({
language: 'en', currency: 'USD', timeZone: 'America/Los_Angeles'
}));
});
}));
but i've got next error
Expected $.language = '' to equal 'en'.
Expected $.currency = undefined to equal 'USD'.
Expected $.timeZone = undefined to equal 'America/Los_Angeles'.
Error: Expected $.language = '' to equal 'en'.
Expected $.currency = undefined to equal 'USD'.
Expected $.timeZone = undefined to equal 'America/Los_Angeles'.
After investigation i found next that locale default value is {language: ''}
and i can't find solution how to solve that
from angular-l10n.
if add in in the sample app test (link above) this:
it('should set the locale', () => {
app.setLocale({ language: 'en-US', currency: 'USD', timeZone: 'America/Los_Angeles' });
fixture.detectChanges();
expect(app.locale).toEqual(jasmine.objectContaining({
language: 'en-US', currency: 'USD', timeZone: 'America/Los_Angeles'
}));
});
it works without problems.
In your case, the locale has the default value (while it should already be initialized). So you should check that the app and library are ready when you run that test, and that the library initialization doesn't fail for some reason. Try to add to you component the following subscriber:
this.translation.onError().subscribe({
next: (error: any) => {
if (error) console.log(error);
}
});
and try to check if there is any error.
from angular-l10n.
@robisim74 yes it works but incorrect, it looks like app.setLocale({ language: 'en-US', currency: 'USD', timeZone: 'America/Los_Angeles' }); is never called because app.local always {langugage: ''}
from angular-l10n.
from angular-l10n.
So when the test starts, translation data are not loaded, and the locale is not initialized.
If you are loading real json files, you have to change the settings in karma config, otherwise it is better to use mock data in tests (as in the example app).
from angular-l10n.
@robisim74 thank you for your answer, yes i loading real json files, but i can't find where you use mock data in tests
in your example only one test file https://github.com/robisim74/angular-l10n/blob/master/projects/angular-l10n-app/src/app/app.component.spec.ts
from angular-l10n.
And you can notice that in the test I don't pass the HttpTranslationLoader
of the json files.
from angular-l10n.
this is works
const config: L10nConfig = {
format: 'language-region',
providers: [
{
name: 'app',
asset: {
en,
ru
},
options: { version: '11.0.0' }
}
],
fallback: false,
cache: true,
keySeparator: '.',
defaultLocale: { language: 'en', currency: 'USD', timeZone: 'America/Los_Angeles' },
schema: [
{ locale: { language: 'en', currency: 'USD', timeZone: 'America/Los_Angeles' }, dir: 'ltr', text: 'United States' },
{ locale: { language: 'ru', currency: 'RUB', timeZone: 'Europe/Rome' }, dir: 'ltr', text: 'Русский' }
],
defaultRouting: true
};
describe('HeaderComponent', () => {
let component: HeaderComponent;
let fixture: ComponentFixture<HeaderComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule,
L10nTranslationModule.forRoot(config),
L10nIntlModule
],
declarations: [
HeaderComponent
]
}).compileComponents();
const loader = TestBed.inject(L10nLoader);
await loader.init();
});
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should setLocale() set language to english', fakeAsync(() => {
const mockLocale = {language: 'en', currency: 'USD', timeZone: 'America/Los_Angeles'};
component.setLocale(mockLocale).then(res => {
expect(component.locale).toEqual(jasmine.objectContaining({
language: 'en', currency: 'USD', timeZone: 'America/Los_Angeles'
}));
});
}));
it('should setLocale() set language to russian', fakeAsync(() => {
const mockLocale = { language: 'ru', currency: 'RUB', timeZone: 'Europe/Rome' };
component.setLocale(mockLocale).then(res => {
expect(component.locale).toEqual(jasmine.objectContaining({
language: 'ru', currency: 'RUB', timeZone: 'Europe/Rome'
}));
});
}));
});
thank you! )
from angular-l10n.
Related Issues (20)
- How i can translate the word HOT 1
- feature: easily register and bundle translation data with a lazy loaded module HOT 3
- Number format options HOT 7
- Release a version with peerDependencies compatible with RxJS v7 HOT 3
- split big files HOT 2
- Namespace 'Intl' has no exported member 'RelativeTimeFormatOptions' HOT 3
- [Question] Differences / Benefits vs ngx-translate? HOT 3
- Setting available languages from Database HOT 5
- Problems after Upgrading HOT 2
- locale.language is empty, Error: angular-l10n (L10nDefaultTranslationLoader): Asset not found HOT 2
- Incorrect date format HOT 2
- Namespace 'Intl' has no exported member 'ListFormatOptions' HOT 1
- possibility to configure PARSE_DATE_STYLE const. Configure full, long,medium,short options HOT 2
- Refreshing page redirects to fallback HOT 5
- Opening subpages in tabs works - sometimes HOT 1
- I encountered this problem: Unexpected value 'TranslateModule' imported by the module 'SharedModule'. Please add an @NgModule annotation. HOT 2
- angular-l10n used in angular component being imported from react parent application as library doesnot work HOT 8
- Is it possible to use angular-l10n to support custom translations in library1 while the config for translations stays in the main app that uses library1? HOT 10
- Json file convertion to js chunk creates a sublevel: [lang]_default HOT 2
- Allow for template elements inside of params HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-l10n.