Skip to content

Commit

Permalink
test(TranslatePipe): Adding tests for onLangChange event with pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Apr 15, 2016
1 parent 399a2ae commit 89a563b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
52 changes: 50 additions & 2 deletions tests/translate.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {TranslatePipe} from '../src/translate.pipe';
import {MockConnection, MockBackend} from "angular2/src/http/backends/mock_backend";
import {TRANSLATE_PROVIDERS, TranslateService} from "./../ng2-translate";
import {XHRBackend, HTTP_PROVIDERS} from "angular2/http";
import {ResponseOptions, Response, XHRBackend, HTTP_PROVIDERS} from "angular2/http";
import {provide, Injector, ChangeDetectorRef} from "angular2/core";
import {LangChangeEvent} from "../src/translate.service";

class FakeChangeDetectorRef extends ChangeDetectorRef {
markForCheck(): void {}
Expand All @@ -14,10 +15,13 @@ class FakeChangeDetectorRef extends ChangeDetectorRef {
checkNoChanges(): void {}

reattach(): void {}

}

export function main() {
const mockBackendResponse = (connection: MockConnection, response: string) => {
connection.mockRespond(new Response(new ResponseOptions({body: response})));
};

describe('TranslatePipe', () => {
let injector: Injector;
let backend: MockBackend;
Expand All @@ -42,6 +46,15 @@ export function main() {
translatePipe = new TranslatePipe(translate, ref);
});

afterEach(() => {
injector = undefined;
backend = undefined;
translate = undefined;
connection = undefined;
translatePipe = undefined;
ref = undefined;
});

it('is defined', () => {
expect(TranslatePipe).toBeDefined();
expect(translatePipe).toBeDefined();
Expand Down Expand Up @@ -104,5 +117,40 @@ export function main() {
translatePipe.transform('TEST', [param]);
}).toThrowError(`Wrong parameter in TranslatePipe. Expected a valid Object, received: ${param}`)
});

describe('should update translations on lang change', () => {
it('with static loader', (done) => {
translate.setTranslation('en', {"TEST": "This is a test"});
translate.setTranslation('fr', {"TEST": "C'est un test"});
translate.use('en');

expect(translatePipe.transform('TEST', [])).toEqual("This is a test");

// this will be resolved at the next lang change
translate.onLangChange.subscribe((res: LangChangeEvent) => {
expect(res.lang).toEqual('fr');
expect(translatePipe.transform('TEST', [])).toEqual("C'est un test");
done();
});

translate.use('fr');
});

it('with file loader', (done) => {
translate.use('en');
mockBackendResponse(connection, '{"TEST": "This is a test"}');
expect(translatePipe.transform('TEST', [])).toEqual("This is a test");

// this will be resolved at the next lang change
translate.onLangChange.subscribe((res: LangChangeEvent) => {
expect(res.lang).toEqual('fr');
expect(translatePipe.transform('TEST', [])).toEqual("C'est un test");
done();
});

translate.use('fr');
mockBackendResponse(connection, `{"TEST": "C'est un test"}`);
});
});
});
}
35 changes: 23 additions & 12 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import {it} from "angular2/testing";
import {provide, Injector} from "angular2/core";
import {
ResponseOptions, Response, HTTP_PROVIDERS, Connection,
XHRBackend
} from "angular2/http";
import {ResponseOptions, Response, HTTP_PROVIDERS, XHRBackend} from "angular2/http";
import {MockBackend, MockConnection} from "angular2/http/testing";
import {
TRANSLATE_PROVIDERS,
TranslateService, MissingTranslationHandler, TranslateLoader,
TranslateService,
MissingTranslationHandler,
TranslateLoader,
TranslateStaticLoader,
LangChangeEvent
} from './../ng2-translate';
import {Observable} from "rxjs/Observable";

export function main() {

const mockBackendResponse = (connection: MockConnection, response: string) => {
connection.mockRespond(new Response(new ResponseOptions({body: response})));
};
Expand Down Expand Up @@ -268,7 +266,7 @@ export function main() {
// Provide a mocked (fake) backend for Http
provide(XHRBackend, {useClass: MockBackend}),
TRANSLATE_PROVIDERS,
provide(MissingTranslationHandler, { useClass: handlerClass })
provide(MissingTranslationHandler, {useClass: handlerClass})
]);
backend = injector.get(XHRBackend);
translate = injector.get(TranslateService);
Expand Down Expand Up @@ -301,7 +299,8 @@ export function main() {

it('should return the key when using MissingTranslationHandler & the handler returns nothing', () => {
class MissingUndef implements MissingTranslationHandler {
handle(key: string) {}
handle(key: string) {
}
}

prepare(MissingUndef);
Expand Down Expand Up @@ -354,7 +353,11 @@ export function main() {
});

it('should wait for the MissingTranslationHandler when it returns an observable & we use get with an array', () => {
let translations = {nonExistingKey1: 'handled: nonExistingKey1', nonExistingKey2: 'handled: nonExistingKey2', nonExistingKey3: 'handled: nonExistingKey3'};
let translations = {
nonExistingKey1: 'handled: nonExistingKey1',
nonExistingKey2: 'handled: nonExistingKey2',
nonExistingKey3: 'handled: nonExistingKey3'
};

prepare(MissingObs);
translate.use('en');
Expand All @@ -381,13 +384,21 @@ export function main() {
});

it('should not wait for the MissingTranslationHandler when it returns an observable & we use instant with an array', () => {
let translations = {nonExistingKey1: 'handled: nonExistingKey1', nonExistingKey2: 'handled: nonExistingKey2', nonExistingKey3: 'handled: nonExistingKey3'};
let translations = {
nonExistingKey1: 'handled: nonExistingKey1',
nonExistingKey2: 'handled: nonExistingKey2',
nonExistingKey3: 'handled: nonExistingKey3'
};

prepare(MissingObs);
translate.use('en');
spyOn(missingTranslationHandler, 'handle').and.callThrough();

expect(translate.instant(Object.keys(translations))).toEqual({nonExistingKey1: 'nonExistingKey1', nonExistingKey2: 'nonExistingKey2', nonExistingKey3: 'nonExistingKey3'});
expect(translate.instant(Object.keys(translations))).toEqual({
nonExistingKey1: 'nonExistingKey1',
nonExistingKey2: 'nonExistingKey2',
nonExistingKey3: 'nonExistingKey3'
});

// mock response after the xhr request, otherwise it will be undefined
mockBackendResponse(connection, '{"TEST": "This is a test"}');
Expand Down Expand Up @@ -443,7 +454,7 @@ export function main() {
// Provide a mocked (fake) backend for Http
provide(XHRBackend, {useClass: MockBackend}),
TRANSLATE_PROVIDERS,
provide(TranslateLoader, { useClass: CustomLoader })
provide(TranslateLoader, {useClass: CustomLoader})
]);
prepare(injector);

Expand Down

0 comments on commit 89a563b

Please sign in to comment.