Преглед изворни кода

Tests: Added tests for the locale.js.

Maciej Bukowski пре 5 година
родитељ
комит
036e015bfd

+ 4 - 4
packages/ckeditor5-utils/src/translation-service.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
  */
 
 
-/* globals window */
+/* globals window, console */
 
 
 /**
 /**
  * @module utils/translation-service
  * @module utils/translation-service
@@ -84,7 +84,7 @@ export function add( language, translations, getPluralForm ) {
 export function translate( language, message, amount = 1 ) {
 export function translate( language, message, amount = 1 ) {
 	if ( typeof message === 'string' ) {
 	if ( typeof message === 'string' ) {
 		// TODO
 		// TODO
-		console.warn( 'DEPRECATED' );
+		console.warn( 'deprecated usage' );
 
 
 		message = { string: message };
 		message = { string: message };
 	}
 	}
@@ -135,8 +135,8 @@ export function _clear() {
 // Checks whether the dictionary exists and translation in that dictionary exists.
 // Checks whether the dictionary exists and translation in that dictionary exists.
 function hasTranslation( language, messageId ) {
 function hasTranslation( language, messageId ) {
 	return (
 	return (
-		window.CKEDITOR_TRANSLATIONS[ language ] &&
-		window.CKEDITOR_TRANSLATIONS[ language ].dictionary[ messageId ]
+		!!window.CKEDITOR_TRANSLATIONS[ language ] &&
+		!!window.CKEDITOR_TRANSLATIONS[ language ].dictionary[ messageId ]
 	);
 	);
 }
 }
 
 

+ 37 - 20
packages/ckeditor5-utils/tests/locale.js

@@ -6,15 +6,14 @@
 /* globals console */
 /* globals console */
 
 
 import Locale from '../src/locale';
 import Locale from '../src/locale';
+import {
+	add as addTranslations,
+	_clear as clearTranslations
+} from '../src/translation-service';
 
 
 describe( 'Locale', () => {
 describe( 'Locale', () => {
-	let locale;
-
-	beforeEach( () => {
-		locale = new Locale();
-	} );
-
 	afterEach( () => {
 	afterEach( () => {
+		clearTranslations();
 		sinon.restore();
 		sinon.restore();
 	} );
 	} );
 
 
@@ -115,35 +114,51 @@ describe( 'Locale', () => {
 	} );
 	} );
 
 
 	describe( 't', () => {
 	describe( 't', () => {
-		it( 'has the context bound', () => {
-			const t = locale.t;
+		let locale;
 
 
-			expect( t( 'Foo' ) ).to.equal( 'Foo' );
-		} );
+		beforeEach( () => {
+			// eslint-disable-next-line no-nested-ternary
+			const getPolishPluralForm = n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2;
 
 
-		it( 'interpolates 1 value', () => {
-			const t = locale.t;
+			addTranslations( 'pl', {
+				'foo': 'foo_pl',
+				'bar': [ 'bar_pl_0', '%0 bar_pl_1', '%0 bar_pl_2' ]
+			}, getPolishPluralForm );
 
 
-			expect( t( '%0 - %0', [ 'foo' ] ) ).to.equal( 'foo - foo' );
+			addTranslations( 'de', {
+				'foo': 'foo_de',
+				'bar': [ 'bar_de_0', '%0 bar_de_1', '%0 bar_de_2' ]
+			} );
+
+			locale = new Locale( {
+				uiLanguage: 'pl',
+				contentLanguage: 'de'
+			} );
 		} );
 		} );
 
 
-		it( 'interpolates 3 values', () => {
+		it( 'should translate a string to the target ui language', () => {
 			const t = locale.t;
 			const t = locale.t;
 
 
-			expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
+			expect( t( 'foo' ) ).to.equal( 'foo_pl' );
 		} );
 		} );
 
 
-		// Those test make sure that if %0 is really to be used, then it's going to work.
-		// It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
-		it( 'does not interpolate placeholders if values not passed', () => {
+		it( 'should translate a plural form to the target ui language based on the first value and interpolate the string', () => {
 			const t = locale.t;
 			const t = locale.t;
 
 
-			expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
+			expect( t( { string: 'bar', plural: '%0 bars' }, [ 1 ] ), 1 ).to.equal( 'bar_pl_0' );
+			expect( t( { string: 'bar', plural: '%0 bars' }, [ 2 ] ), 2 ).to.equal( '2 bar_pl_1' );
+			expect( t( { string: 'bar', plural: '%0 bars' }, [ 5 ] ), 3 ).to.equal( '5 bar_pl_2' );
 		} );
 		} );
 
 
-		it( 'does not interpolate those placeholders for which values has not been passed', () => {
+		it( 'should interpolate a message with provided values', () => {
 			const t = locale.t;
 			const t = locale.t;
 
 
+			expect( t( '%0 - %0', [ 'foo' ] ) ).to.equal( 'foo - foo' );
+			expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
+
+			// Those test make sure that if %0 is really to be used, then it's going to work.
+			// It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
+			expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
 			expect( t( '%1 - %0 - %2', [ 'a' ] ) ).to.equal( '%1 - a - %2' );
 			expect( t( '%1 - %0 - %2', [ 'a' ] ) ).to.equal( '%1 - a - %2' );
 		} );
 		} );
 	} );
 	} );
@@ -151,6 +166,7 @@ describe( 'Locale', () => {
 	describe( 'language()', () => {
 	describe( 'language()', () => {
 		it( 'should return #uiLanguage', () => {
 		it( 'should return #uiLanguage', () => {
 			const stub = sinon.stub( console, 'warn' );
 			const stub = sinon.stub( console, 'warn' );
+			const locale = new Locale();
 
 
 			expect( locale.language ).to.equal( locale.uiLanguage );
 			expect( locale.language ).to.equal( locale.uiLanguage );
 			sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
 			sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
@@ -158,6 +174,7 @@ describe( 'Locale', () => {
 
 
 		it( 'should warn about deprecation', () => {
 		it( 'should warn about deprecation', () => {
 			const stub = sinon.stub( console, 'warn' );
 			const stub = sinon.stub( console, 'warn' );
+			const locale = new Locale();
 
 
 			expect( locale.language ).to.equal( 'en' );
 			expect( locale.language ).to.equal( 'en' );
 			sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
 			sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );