Skip to content

Commit

Permalink
Split tests for i18n-js tech into different files by core versions cr…
Browse files Browse the repository at this point in the history
…iteria
  • Loading branch information
tormozz48 committed Jun 8, 2015
1 parent 4b4f66e commit 17fb8d2
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 46 deletions.
174 changes: 174 additions & 0 deletions test/techs/i18n-js/v1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
var fs = require('fs'),
path = require('path'),
mock = require('mock-fs'),
serializeJS = require('serialize-javascript'),
TestNode = require('enb/lib/test/mocks/test-node'),
dropRequireCache = require('enb/lib/fs/drop-require-cache'),
Tech = require('../../../techs/i18n-js'),
core;

describe('i18n-js v1', function () {
before(function () {
var p = './test/fixtures/bem-core/common.blocks/i-bem/__i18n/i-bem__i18n.i18n/core.js';
core = fs.readFileSync(path.resolve(p), { encoding: 'utf-8' });
});

afterEach(function () {
mock.restore();
});

it('must throw err if i18n is not found', function () {
var keysets = {};

return build(keysets)
.fail(function (err) {
err.must.a(Error);
err.message.must.be('Core of i18n is not found!');
});
});

it('must return value', function () {
var keysets = {
all: {
'': core
},
scope: {
key: 'val'
}
};

return build(keysets)
.then(function (i18n) {
i18n('scope', 'key').must.be('val');
});
});

it('must get valid *.lang from *.keyset file', function () {
var keysets = {
all: { '': core },
scope1: { key11: 'val11', key12: 'val12' },
scope2: { key21: 'val21' }
};
return build(keysets)
.then(function (i18n) {
i18n('scope1', 'key11').must.be('val11');
i18n('scope1', 'key12').must.be('val12');
i18n('scope2', 'key21').must.be('val21');
});
});

it('must get valid *.lang from empty *.keyset file (only core)', function () {
var keysets = {
all: { '': core }
};

return build(keysets)
.then(function (i18n) {
i18n('scope', 'key').must.be('');
});
});

describe('cache', function () {
it('must get result from cache', function () {
var time = new Date(1);

mock({
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
all: { '': core },
scope: { key: 'val' }
}),
mtime: time
})
}
});

var bundle = new TestNode('bundle'),
cache = bundle.getNodeCache('bundle.lang.lang.js'),
basename = 'bundle.keysets.lang.js',
filename = path.resolve('bundle', basename);

dropRequireCache(require, filename);
require(filename);
cache.cacheFileInfo('keysets-file-' + basename, filename);

mock({
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
all: { '': core },
scope: { key: 'val2' }
}),
mtime: time
})
}
});

return bundle.runTechAndRequire(Tech, { lang: 'lang' })
.spread(function (i18n) {
i18n('scope', 'key').must.be('val');
});
});

it('must ignore outdated cache', function () {
mock({
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
all: { '': core },
scope: { key: 'val' }
}),
mtime: new Date(1)
})
}
});

var bundle = new TestNode('bundle'),
cache = bundle.getNodeCache('bundle.lang.lang.js'),
basename = 'bundle.keysets.lang.js',
filename = path.resolve('bundle', basename);

dropRequireCache(require, filename);
require(filename);
cache.cacheFileInfo('keysets-file-' + basename, filename);

mock({
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
all: { '': core },
scope: { key: 'val2' }
}),
mtime: new Date(2)
})
}
});

return bundle.runTechAndRequire(Tech, { lang: 'lang' })
.spread(function (i18n) {
i18n('scope', 'key').must.be('val2');
});
});
});
});

function build(keysets) {
mock({
bundle: {
'bundle.keysets.lang.js': serialize(keysets)
}
});

var bundle = new TestNode('bundle');

return bundle.runTechAndRequire(Tech, { lang: 'lang' })
.spread(function (i18n) {
return i18n;
});
}

function serialize(js) {
return 'module.exports = ' + serializeJS(js) + ';';
}

59 changes: 13 additions & 46 deletions test/techs/i18n-js.test.js → test/techs/i18n-js/v2.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
var fs = require('fs'),
path = require('path'),
var path = require('path'),
mock = require('mock-fs'),
serializeJS = require('serialize-javascript'),
TestNode = require('enb/lib/test/mocks/test-node'),
dropRequireCache = require('enb/lib/fs/drop-require-cache'),
Tech = require('../../techs/i18n-js'),
corev1,
corev2 = require('../fixtures/bem-core-v3/common.blocks/i18n/i18n.i18n.js').i18n.i18n;

describe('i18n-js', function () {
before(function () {
var p = './test/fixtures/bem-core/common.blocks/i-bem/__i18n/i-bem__i18n.i18n/core.js';
corev1 = fs.readFileSync(path.resolve(p), { encoding: 'utf-8' });
});
Tech = require('../../../techs/i18n-js'),
core = require('../../fixtures/bem-core-v3/common.blocks/i18n/i18n.i18n.js').i18n.i18n;

describe('i18n-js v2', function () {
afterEach(function () {
mock.restore();
});
Expand Down Expand Up @@ -45,7 +38,7 @@ describe('i18n-js', function () {
it('must return value', function () {
var keysets = {
i18n: {
i18n: corev2
i18n: core
},
scope: {
key: 'val'
Expand All @@ -61,7 +54,7 @@ describe('i18n-js', function () {
it('must build fake key if keysets is empty', function () {
var keysets = {
i18n: {
i18n: corev2
i18n: core
}
};

Expand All @@ -74,7 +67,7 @@ describe('i18n-js', function () {
it('must build key by params', function () {
var keysets = {
i18n: {
i18n: corev2
i18n: core
},
scope: {
key: function (params) {
Expand All @@ -92,7 +85,7 @@ describe('i18n-js', function () {
it('must provide i18n instance to function', function () {
var keysets = {
i18n: {
i18n: corev2
i18n: core
},
'scope-1': {
key: 'val'
Expand All @@ -110,33 +103,6 @@ describe('i18n-js', function () {
});
});

describe('corev1', function () {
it('must get valid *.lang from *.keyset file', function () {
var keysets = {
all: { '': corev1 },
scope1: { key11: 'val11', key12: 'val12' },
scope2: { key21: 'val21' }
};
return build(keysets)
.then(function (i18n) {
i18n('scope1', 'key11').must.be('val11');
i18n('scope1', 'key12').must.be('val12');
i18n('scope2', 'key21').must.be('val21');
});
});

it('must get valid *.lang from empty *.keyset file (only core)', function () {
var keysets = {
all: { '': corev1 }
};

return build(keysets)
.then(function (i18n) {
i18n('scope', 'key').must.be('');
});
});
});

describe('cache', function () {
it('must get result from cache', function () {
var time = new Date(1);
Expand All @@ -145,7 +111,7 @@ describe('i18n-js', function () {
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
i18n: { i18n: corev2 },
i18n: { i18n: core },
scope: { key: 'val' }
}),
mtime: time
Expand All @@ -166,7 +132,7 @@ describe('i18n-js', function () {
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
i18n: { i18n: corev2 },
i18n: { i18n: core },
scope: { key: 'val2' }
}),
mtime: time
Expand All @@ -185,7 +151,7 @@ describe('i18n-js', function () {
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
i18n: { i18n: corev2 },
i18n: { i18n: core },
scope: { key: 'val' }
}),
mtime: new Date(1)
Expand All @@ -206,7 +172,7 @@ describe('i18n-js', function () {
bundle: {
'bundle.keysets.lang.js': mock.file({
content: serialize({
i18n: { i18n: corev2 },
i18n: { i18n: core },
scope: { key: 'val2' }
}),
mtime: new Date(2)
Expand Down Expand Up @@ -240,3 +206,4 @@ function build(keysets) {
function serialize(js) {
return 'module.exports = ' + serializeJS(js) + ';';
}

0 comments on commit 17fb8d2

Please sign in to comment.