import { DOCUMENT } from '@angular/common' ; import { APP_INITIALIZER , LOCALE_ID } from '@angular/core' ; import { TestBed } from '@angular/core/testing' ; import { Title } from '@angular/platform-browser' ; import * as i18n from 'i18next/index' ; import { I18NextModule , I18NextPipe , I18NEXT_SERVICE , ITranslationService , } from '../../lib' ; const i18next = i18n . default ; const i18nextOptions = { lng : 'cimode' } ; export function appInit ( service ) { return ( ) => service . init ( i18nextOptions ) ; } export function localeIdFactory ( service ) { return service . language ; } export const I18N_PROVIDERS = [ { provide : APP_INITIALIZER , useFactory : appInit , deps : [ I18NEXT_SERVICE ] , multi : true } , { provide : LOCALE_ID , deps : [ I18NEXT_SERVICE ] , useFactory : localeIdFactory } , ] ; describe ( 'I18nService' , function ( ) { beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ I18NextModule . forRoot ( ) ] , providers : [ I18N_PROVIDERS ] } ) ; } ) ; it ( 'should trigger initialize event' , function ( done ) { let service = TestBed . get ( I18NEXT_SERVICE ) ; service . events . initialized . subscribe ( ( isInited ) => { if ( isInited ) done ( ) ; } ) ; service . init ( i18nextOptions ) ; } ) ; it ( 'should init' , function ( done ) { let service = TestBed . get ( I18NEXT_SERVICE ) ; service . init ( ) . then ( ( result ) => { expect ( service . options ) . toBeTruthy ( ) ; done ( ) ; } ) ; } ) ; it ( 'should load namespace' , function ( done ) { let service = TestBed . get ( I18NEXT_SERVICE ) ; service . events . initialized . subscribe ( ( value ) => { if ( value ) { service . loadNamespaces ( [ 'somens' ] ) . then ( ( ) => { done ( ) ; } ) ; } } ) ; } ) ; it ( 'should translate' , function ( done ) { let service = TestBed . get ( I18NEXT_SERVICE ) ; let title = TestBed . get ( Title ) ; let i18nextPipe = TestBed . get ( I18NextPipe ) ; const key = 'test' ; service . events . initialized . subscribe ( ( value ) => { if ( value ) { const serviceResult = service . t ( key ) ; expect ( serviceResult ) . toEqual ( 'test' ) ; const serviceResult2 = service . t ( [ key , key + '2' ] ) ; expect ( serviceResult2 ) . toEqual ( 'test2' ) ; const pipeResult = i18nextPipe . transform ( key ) ; expect ( pipeResult ) . toEqual ( 'test' ) ; title . setTitle ( 'test' ) ; let document = TestBed . get ( DOCUMENT ) ; expect ( document . title ) . toEqual ( 'test' ) ; done ( ) ; } } ) ; } ) ; xit ( 'should translate with default value' , function ( done ) { let service = TestBed . get ( I18NEXT_SERVICE ) ; let title = TestBed . get ( Title ) ; let i18nextPipe = TestBed . get ( I18NextPipe ) ; const key = 'test2' ; const defaultValue = 'test3' ; service . events . initialized . subscribe ( ( value ) => { if ( value ) { const serviceResult = service . t ( key , defaultValue ) ; expect ( serviceResult ) . toEqual ( defaultValue ) ; const pipeResult = i18nextPipe . transform ( key , { defaultValue } ) ; expect ( pipeResult ) . toEqual ( defaultValue ) ; done ( ) ; } } ) ; } ) ; it ( 'should dir' , function ( ) { const lng = 'ru' ; let service = TestBed . get ( I18NEXT_SERVICE ) ; expect ( < any > service . dir ( lng ) ) . toEqual ( 'ltr' ) ; expect ( < any > service . dir ( lng ) ) . toEqual ( service . dir ( lng ) ) ; } ) ; } ) ;