Sfoglia il codice sorgente

Added missing tests for `ckeditor5-utils`.

Maciej Bukowski 8 anni fa
parent
commit
7d4cbc97aa

+ 8 - 3
packages/ckeditor5-utils/src/translation-service.js

@@ -20,6 +20,12 @@ let dictionaries = {};
  *			'Cancel [context: reject]': 'Anuluj'
  *		} );
  *
+ * That function is accessible globally via `window.CKEDITOR_TRANSLATIONS.add()`. So it's possible to add translation from
+ * the other script, just after that one.
+ *
+ * 		<script src="./path/to/ckeditor.js"></script>
+ * 		<script src="./path/to/translations/en.js"></script>
+ *
  * @param {String} lang Target language.
  * @param {Object.<String, String>} translations Translations which will be added to the dictionary.
  */
@@ -42,7 +48,7 @@ export function add( lang, translations ) {
  *		translate( 'pl', 'Cancel [context: reject]' );
  *
  * @param {String} lang Target language.
- * @param {String} translationKey String which is going to be translated.
+ * @param {String} translationKey String that will be translated.
  * @returns {String} Translated sentence.
  */
 export function translate( lang, translationKey ) {
@@ -50,8 +56,7 @@ export function translate( lang, translationKey ) {
 
 	if ( numberOfLanguages === 1 ) {
 		// Override the language to the only supported one.
-		// This can't be done in the `Locale` class,
-		// because the translations comes after the `Locale` class initializes.
+		// This can't be done in the `Locale` class, because the translations comes after the `Locale` class initialization.
 		lang = Object.keys( dictionaries )[ 0 ];
 	}
 

+ 28 - 0
packages/ckeditor5-utils/tests/translation-service.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+/* globals window */
+
 import { translate, add, _clear } from '../src/translation-service';
 
 describe( 'translation-service', () => {
@@ -45,6 +47,17 @@ describe( 'translation-service', () => {
 		expect( translation ).to.be.equal( 'Bold' );
 	} );
 
+	it( 'should use provided language if only one is provided', () => {
+		add( 'pl', {
+			'OK': 'OK',
+			'Cancel [context: reject]': 'Anuluj'
+		} );
+
+		const translation = translate( 'de', 'Cancel [context: reject]' );
+
+		expect( translation ).to.be.equal( 'Anuluj' );
+	} );
+
 	it( 'should be able to merge translations', () => {
 		add( 'pl', {
 			'OK': 'OK',
@@ -62,4 +75,19 @@ describe( 'translation-service', () => {
 		expect( translationPL ).to.be.equal( 'Anuluj' );
 		expect( translationEN ).to.be.equal( 'Cancel' );
 	} );
+
+	it( 'should expose `add` function globally', () => {
+		expect( window.CKEDITOR_TRANSLATIONS ).to.be.an( 'object' );
+		expect( window.CKEDITOR_TRANSLATIONS.add ).to.be.a( 'function' );
+		expect( window.CKEDITOR_TRANSLATIONS.add ).to.equal( add );
+
+		window.CKEDITOR_TRANSLATIONS.add( 'pl', {
+			'OK': 'OK',
+			'Cancel [context: reject]': 'Anuluj'
+		} );
+
+		const translationPL = translate( 'pl', 'Cancel [context: reject]' );
+
+		expect( translationPL ).to.be.equal( 'Anuluj' );
+	} );
 } );