Skip to content

Commit

Permalink
added basic test for autoreload - #105
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Feb 7, 2016
1 parent b59ffe1 commit ec04ddf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -2,7 +2,7 @@ test:
mocha

cover:
istanbul cover mocha
istanbul cover ./node_modules/mocha/bin/_mocha

examples:
for example in examples/*/test.js ; do \
Expand Down
3 changes: 3 additions & 0 deletions i18n.js
Expand Up @@ -108,8 +108,11 @@ i18n.configure = function i18nConfigure(opt) {

// auto reload locale files when changed
if (autoReload) {

// watch changes of locale files (it's called twice because fs.watch is still unstable)
fs.watch(directory, function(event, filename) {

// @todo: add support for prefixed files
var re = new RegExp(extension + '$');
if (filename && filename.match(re)) {
var locale = filename.replace(re, '');
Expand Down
64 changes: 64 additions & 0 deletions test/i18n.configureAutoreload.js
@@ -0,0 +1,64 @@
/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, plusplus: true, indent: 2 */

var i18n = require('../i18n'),
should = require("should"),
fs = require('fs'),
path = require('path');

var i18nPath = 'i18n';
var i18nFilename = path.resolve(i18nPath + '.js');
var testScope = {};

function reconfigure(config) {
delete require.cache[i18nFilename];
i18n = require(i18nFilename);
i18n.configure(config);
}

describe('autoreload configuration', function() {

var directory = path.resolve(__dirname + '/../testlocalesauto');

// @todo: add test on prefixed files
it('will start with empty catalogs', function(done) {
fs.mkdirSync(directory);
fs.writeFileSync(directory + '/de.json', '{}');
fs.writeFileSync(directory + '/en.json', '{}');
reconfigure({
directory: directory,
register: testScope,
autoReload: true
});
should.deepEqual(i18n.getCatalog(), { de: {}, en: {} });
setTimeout(function(){
done();
}, 50);
});

it('reloads when a catalog is altered', function(done) {
fs.writeFileSync(directory + '/de.json', '{"Hello":"Hallo"}');
setTimeout(function(){
done();
}, 50);
});

it('has added new string to catalog and translates correctly', function(done) {
i18n.setLocale(testScope, 'de');
should.equal('Hallo', testScope.__('Hello'));
should.deepEqual(i18n.getCatalog(), { de: { Hello: 'Hallo' }, en: {} });
done();
});

it('will add new string to catalog and files from __()', function(done) {
should.equal('Hallo', testScope.__('Hello'));
should.deepEqual(i18n.getCatalog(), { de: { Hello: 'Hallo' }, en: {} });
done();
});

it('will remove testlocalesauto after tests', function(){
fs.unlinkSync(directory + '/de.json');
fs.unlinkSync(directory + '/en.json');
fs.rmdirSync(directory);
});

});

0 comments on commit ec04ddf

Please sign in to comment.