11import { it , beforeEachProviders , inject } from "angular2/testing" ;
2- import { provide } from "angular2/core" ;
2+ import { provide , Injector } from "angular2/core" ;
33import {
4- BaseRequestOptions , Http , ResponseOptions , Response , HTTP_PROVIDERS , Connection ,
4+ ResponseOptions , Response , HTTP_PROVIDERS , Connection ,
55 XHRBackend
66} from "angular2/http" ;
77import { MockBackend , MockConnection } from "angular2/http/testing" ;
@@ -10,37 +10,109 @@ import {TranslateService} from '../src/translate.service';
1010export function main ( ) {
1111
1212 describe ( 'TranslateService' , ( ) => {
13- beforeEachProviders ( ( ) => [
14- BaseRequestOptions ,
15- HTTP_PROVIDERS ,
16- // Provide a mocked (fake) backend for Http
17- provide ( XHRBackend , { useClass : MockBackend } ) ,
18- TranslateService
19- ] ) ;
20-
21-
22- it ( 'is defined' , ( ) => {
23- expect ( TranslateService ) . toBeDefined ( ) ;
24- } ) ;
25-
26- // this test is async, and yet it works thanks to Zone \o/
27- it ( 'should be able to get translations for the view' , inject ( [ XHRBackend , Http , TranslateService ] , ( xhrBackend , http , translate ) => {
28- var connection : MockConnection ; //this will be set when a new connection is emitted from the backend.
29- xhrBackend . connections . subscribe ( ( c : MockConnection ) => connection = c ) ;
13+ let injector : Injector ;
14+ let backend : MockBackend ;
15+ let translate : TranslateService ;
16+ var connection : MockConnection ; // this will be set when a new connection is emitted from the backend.
3017
18+ var prepareStaticTranslate = ( ) => {
3119 // this will load translate json files from src/public/i18n
3220 translate . useStaticFilesLoader ( ) ;
3321
3422 // the lang to use, if the lang isn't available, it will use the current loader to get them
3523 translate . use ( 'en' ) ;
24+ } ;
25+
26+ var mockBackendResponse = ( response : string ) => {
27+ connection . mockRespond ( new Response ( new ResponseOptions ( { body : response } ) ) ) ;
28+ } ;
29+
30+ beforeEach ( ( ) => {
31+ injector = Injector . resolveAndCreate ( [
32+ HTTP_PROVIDERS ,
33+ // Provide a mocked (fake) backend for Http
34+ provide ( XHRBackend , { useClass : MockBackend } ) ,
35+ TranslateService
36+ ] ) ;
37+ backend = injector . get ( XHRBackend ) ;
38+ translate = injector . get ( TranslateService ) ;
39+ // sets the connection when someone tries to access the backend with an xhr request
40+ backend . connections . subscribe ( ( c : MockConnection ) => connection = c ) ;
41+ } ) ;
42+
43+ it ( 'is defined' , ( ) => {
44+ expect ( TranslateService ) . toBeDefined ( ) ;
45+ expect ( translate ) . toBeDefined ( ) ;
46+ expect ( translate instanceof TranslateService ) . toBeTruthy ( ) ;
47+ } ) ;
48+
49+ it ( 'should be able to get translations' , ( ) => {
50+ prepareStaticTranslate ( ) ;
3651
3752 // this will request the translation from the backend because we use a static files loader for TranslateService
3853 translate . get ( 'TEST' ) . subscribe ( ( res : string ) => {
3954 expect ( res ) . toEqual ( 'This is a test' ) ;
4055 } ) ;
4156
4257 // mock response after the xhr request, otherwise it will be undefined
43- connection . mockRespond ( new Response ( new ResponseOptions ( { body : '{"TEST": "This is a test"}' } ) ) ) ;
44- } ) ) ;
58+ mockBackendResponse ( '{"TEST": "This is a test", "TEST2": "This is another test"}' ) ;
59+
60+ // this will request the translation from downloaded translations without making a request to the backend
61+ translate . get ( 'TEST2' ) . subscribe ( ( res : string ) => {
62+ expect ( res ) . toEqual ( 'This is another test' ) ;
63+ } ) ;
64+ } ) ;
65+
66+ it ( "should return the key when it doesn't find a translation" , ( ) => {
67+ prepareStaticTranslate ( ) ;
68+
69+ translate . get ( 'TEST' ) . subscribe ( ( res : string ) => {
70+ expect ( res ) . toEqual ( 'TEST' ) ;
71+ } ) ;
72+
73+ mockBackendResponse ( '{}' ) ;
74+ } ) ;
75+
76+ it ( 'should be able to get translations with params' , ( ) => {
77+ prepareStaticTranslate ( ) ;
78+
79+ translate . get ( 'TEST' , { param : 'with param' } ) . subscribe ( ( res : string ) => {
80+ expect ( res ) . toEqual ( 'This is a test with param' ) ;
81+ } ) ;
82+
83+ mockBackendResponse ( '{"TEST": "This is a test {{param}}"}' ) ;
84+ } ) ;
85+
86+ it ( 'should be able to get translations with nested params' , ( ) => {
87+ prepareStaticTranslate ( ) ;
88+
89+ translate . get ( 'TEST' , { param : { value : 'with param' } } ) . subscribe ( ( res : string ) => {
90+ expect ( res ) . toEqual ( 'This is a test with param' ) ;
91+ } ) ;
92+
93+ mockBackendResponse ( '{"TEST": "This is a test {{param.value}}"}' ) ;
94+ } ) ;
95+
96+ it ( 'should throw if you forget the key' , ( ) => {
97+ prepareStaticTranslate ( ) ;
98+
99+ expect ( ( ) => {
100+ translate . get ( undefined ) ;
101+ } ) . toThrowError ( 'Parameter "key" required' ) ;
102+ } ) ;
103+
104+ it ( 'should be able to get translations with nested keys' , ( ) => {
105+ prepareStaticTranslate ( ) ;
106+
107+ translate . get ( 'TEST.TEST' ) . subscribe ( ( res : string ) => {
108+ expect ( res ) . toEqual ( 'This is a test' ) ;
109+ } ) ;
110+
111+ mockBackendResponse ( '{"TEST": {"TEST": "This is a test"}, "TEST2": {"TEST2": {"TEST2": "This is another test"}}}' ) ;
112+
113+ translate . get ( 'TEST2.TEST2.TEST2' ) . subscribe ( ( res : string ) => {
114+ expect ( res ) . toEqual ( 'This is another test' ) ;
115+ } ) ;
116+ } ) ;
45117 } ) ;
46118}
0 commit comments