gulp-json-angular-translate
Version:
Gulp plugin to convert json files to angular-translate js files
101 lines (99 loc) • 3.86 kB
JavaScript
var expect = require('chai').expect,
fs = require('fs'),
File = require('vinyl'),
jsonAngularTranslate = require('../');
describe('gulp-json-angular-translate', function () {
describe('jsonAngularTranslate()', function () {
var file;
beforeEach(function () {
file = new File({
path: 'test/fixtures/messages_ru.json',
cwd: 'test/',
base: 'test/fixtures',
contents: fs.readFileSync('test/fixtures/messages_ru.json')
})
});
it('should use the last two letters before last dot as language by default', function (done) {
var stream = jsonAngularTranslate();
stream.once('data', function (result) {
expect(result.isBuffer()).to.equal(true);
expect(result.contents.toString()).to.equal(fs.readFileSync('test/expected/messages_ru.default.js', 'utf8'));
done();
});
stream.write(file);
stream.end();
});
it('should change the extension to .js', function (done) {
var stream = jsonAngularTranslate();
stream.once('data', function (result) {
expect(result.path).to.match(/\.js$/g);
done();
});
stream.write(file);
stream.end();
});
describe('param moduleName', function () {
it('should set the module name', function (done) {
var stream = jsonAngularTranslate({moduleName: 'wixTranslations'});
stream.once('data', function (result) {
expect(result.isBuffer()).to.equal(true);
expect(result.contents.toString()).to.equal(fs.readFileSync('test/expected/messages_ru.moduleName.js', 'utf8'));
done();
});
stream.write(file);
stream.end();
});
});
describe('param extractLanguage', function () {
it('should override the default language', function (done) {
var stream = jsonAngularTranslate({extractLanguage: /^../});
stream.once('data', function (result) {
expect(result.isBuffer()).to.equal(true);
expect(result.contents.toString()).to.equal(fs.readFileSync('test/expected/messages_ru.regexPattern.js', 'utf8'));
done();
});
stream.write(file);
stream.end();
});
it('should accept a function to override the default language', function (done) {
var stream = jsonAngularTranslate({
extractLanguage: function (path) {
return path.slice(-2)
}
});
stream.once('data', function (result) {
expect(result.isBuffer()).to.equal(true);
expect(result.contents.toString()).to.equal(fs.readFileSync('test/expected/messages_ru.functionPattern.js', 'utf8'));
done();
});
stream.write(file);
stream.end();
});
});
describe('param hasPreferredLanguage', function () {
it('should not add the preferredLanguage if set to false', function (done) {
var stream = jsonAngularTranslate({hasPreferredLanguage: false});
stream.once('data', function (result) {
expect(result.isBuffer()).to.equal(true);
expect(result.contents.toString()).to.equal(fs.readFileSync('test/expected/messages_ru.noPreferredLanguage.js', 'utf8'));
done();
});
stream.write(file);
stream.end();
});
});
describe('param createNestedKeys', function () {
it('should make the json object not expand if set to false', function (done) {
var stream = jsonAngularTranslate({createNestedKeys: false});
stream.once('data', function (result) {
expect(result.isBuffer()).to.equal(true);
expect(result.contents.toString()).to.equal(fs.readFileSync('test/expected/messages_ru.flat.js', 'utf8'));
done();
});
stream.write(file);
stream.end();
});
});
});
});
;