Skip to content

Commit

Permalink
Added missing tests for the languages: all option.
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Nov 22, 2017
1 parent 60b28aa commit 4f34157
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const { EventEmitter } = require( 'events' );
module.exports = class MultipleLanguageTranslationService extends EventEmitter {
/**
* @param {Array.<String>} languages Target languages.
* @param {Boolean} compileAllLanguages Flag indicates whether the languages are specified or should be found at runtime.
* @param {Boolean} [compileAllLanguages] Flag indicates whether the languages are specified or should be found at runtime.
*/
constructor( languages, compileAllLanguages ) {
super();

this._languages = new Set( languages );

this._compileAllLanguages = compileAllLanguages;
this._compileAllLanguages = !!compileAllLanguages;

this._packagePaths = new Set();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ const proxyquire = require( 'proxyquire' );

describe( 'translations', () => {
describe( 'MultipleLanguageTranslationService', () => {
let MultipleLanguageTranslationService, stubs, files, fileContents, sandbox;
let MultipleLanguageTranslationService, stubs, filesAndDirs, fileContents, dirContents;
const sandbox = sinon.sandbox.create();

beforeEach( () => {
sandbox = sinon.sandbox.create();
filesAndDirs = [];
fileContents = {};
dirContents = {};

stubs = {
fs: {
existsSync: path => files.includes( path ),
readFileSync: path => fileContents[ path ]
existsSync: path => filesAndDirs.includes( path ),
readFileSync: path => fileContents[ path ],
readdirSync: dir => dirContents[ dir ]
}
};

Expand All @@ -42,12 +46,12 @@ describe( 'translations', () => {
} );

describe( 'loadPackage()', () => {
it( 'should load po file from the package and load translations', () => {
it( 'should load PO file from the package and load translations', () => {
const translationService = new MultipleLanguageTranslationService( [ 'pl', 'de' ] );
const pathToPlTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'pl.po' );
const pathToDeTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'de.po' );

files = [ pathToPlTranslations, pathToDeTranslations ];
filesAndDirs = [ pathToPlTranslations, pathToDeTranslations ];

fileContents = {
[ pathToPlTranslations ]: [
Expand Down Expand Up @@ -76,18 +80,18 @@ describe( 'translations', () => {
} );
} );

it( 'should do nothing if the po file does not exist', () => {
it( 'should do nothing if the PO file does not exist', () => {
const translationService = new MultipleLanguageTranslationService( [ 'pl', 'de' ] );

files = [];
filesAndDirs = [];
fileContents = {};

translationService.loadPackage( 'pathToPackage' );

expect( translationService._dictionary ).to.deep.equal( {} );
} );

it( 'should load po file from the package only once per language', () => {
it( 'should load PO file from the package only once per language', () => {
const translationService = new MultipleLanguageTranslationService( [ 'pl', 'de' ] );
const loadPoFileSpy = sandbox.stub( translationService, '_loadPoFile' );

Expand All @@ -97,6 +101,48 @@ describe( 'translations', () => {

sinon.assert.calledTwice( loadPoFileSpy );
} );

it.only( 'should load all PO files for the current package and add languages to the language list', () => {
const translationService = new MultipleLanguageTranslationService( [], true );

const pathToTranslations = path.join( 'pathToPackage', 'lang', 'translations' );
const pathToPlTranslations = path.join( pathToTranslations, 'pl.po' );
const pathToDeTranslations = path.join( pathToTranslations, 'de.po' );

filesAndDirs = [ pathToPlTranslations, pathToDeTranslations, pathToTranslations ];

fileContents = {
[ pathToPlTranslations ]: [
'msgctxt "Label for the Save button."',
'msgid "Save"',
'msgstr "Zapisz"',
''
].join( '\n' ),
[ pathToDeTranslations ]: [
'msgctxt "Label for the Save button."',
'msgid "Save"',
'msgstr "Speichern"',
''
].join( '\n' )
};

dirContents = {
[ pathToTranslations ]: [ 'pl.po', 'de.po' ]
};

translationService.loadPackage( 'pathToPackage' );

expect( translationService._dictionary ).to.deep.equal( {
pl: {
'Save': 'Zapisz'
},
de: {
'Save': 'Speichern'
}
} );

expect( Array.from( translationService._languages ) ).to.deep.equal( [ 'pl', 'de' ] );
} );
} );

describe( 'translateSource()', () => {
Expand Down Expand Up @@ -260,7 +306,7 @@ describe( 'translations', () => {

const pathToTranslations = path.join( 'custom', 'path', 'to', 'pathToPackage', 'en.po' );

files = [ pathToTranslations ];
filesAndDirs = [ pathToTranslations ];

fileContents = {
[ pathToTranslations ]: [
Expand All @@ -287,7 +333,7 @@ describe( 'translations', () => {
const pathToPlTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'pl.po' );
const pathToDeTranslations = path.join( 'pathToPackage', 'lang', 'translations', 'de.po' );

files = [ pathToPlTranslations, pathToDeTranslations ];
filesAndDirs = [ pathToPlTranslations, pathToDeTranslations ];

fileContents = {
[ pathToPlTranslations ]: [
Expand Down
17 changes: 17 additions & 0 deletions packages/ckeditor5-dev-webpack-plugin/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,22 @@ describe( 'webpack-plugin/CKEditorWebpackPlugin', () => {
expect( stubs.serveTranslations.getCall( 0 ).args[ 2 ] ).to.be.instanceof( MultipleLanguageTranslationService );
expect( stubs.serveTranslations.getCall( 0 ).args[ 3 ] ).to.equal( stubs.ckeditor5EnvUtils );
} );

it( 'should serve `MultipleLanguageTranslationService` if the `languages` is set to `all`.', () => {
const options = {
languages: 'all'
};

const compiler = {};

const ckeditorWebpackPlugin = new CKEditorWebpackPlugin( options );
ckeditorWebpackPlugin.apply( compiler );

sinon.assert.calledOnce( stubs.serveTranslations );
expect( stubs.serveTranslations.getCall( 0 ).args[ 0 ] ).to.equal( compiler );
expect( stubs.serveTranslations.getCall( 0 ).args[ 1 ] ).to.equal( options );
expect( stubs.serveTranslations.getCall( 0 ).args[ 2 ] ).to.be.instanceof( MultipleLanguageTranslationService );
expect( stubs.serveTranslations.getCall( 0 ).args[ 3 ] ).to.equal( stubs.ckeditor5EnvUtils );
} );
} );
} );

0 comments on commit 4f34157

Please sign in to comment.