From 89a563bb9d1663716588f4c2dbbae8767f8b28f4 Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Fri, 15 Apr 2016 10:59:36 +0200 Subject: [PATCH] test(TranslatePipe): Adding tests for onLangChange event with pipe --- tests/translate.pipe.spec.ts | 52 +++++++++++++++++++++++++++++++-- tests/translate.service.spec.ts | 35 ++++++++++++++-------- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/tests/translate.pipe.spec.ts b/tests/translate.pipe.spec.ts index 994808a8..36d28f77 100644 --- a/tests/translate.pipe.spec.ts +++ b/tests/translate.pipe.spec.ts @@ -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 {} @@ -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; @@ -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(); @@ -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"}`); + }); + }); }); } \ No newline at end of file diff --git a/tests/translate.service.spec.ts b/tests/translate.service.spec.ts index ae79eadf..42e86aed 100644 --- a/tests/translate.service.spec.ts +++ b/tests/translate.service.spec.ts @@ -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}))); }; @@ -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); @@ -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); @@ -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'); @@ -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"}'); @@ -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);