increase test coverage

This commit is contained in:
ido 2015-01-22 18:02:18 +02:00
parent d26c8ed759
commit 3b9d8579fa
6 changed files with 36 additions and 2 deletions

View File

@ -19,7 +19,7 @@ var simpleTagTemplateCreateElement = _.template('React.createElement(<%= name %>
var tagTemplateCreateElement = _.template('React.createElement.apply(this,_.flatten([<%= name %>,<%= props %><%= children %>]))'); var tagTemplateCreateElement = _.template('React.createElement.apply(this,_.flatten([<%= name %>,<%= props %><%= children %>]))');
var commentTemplate = _.template(' /* <%= data %> */ '); var commentTemplate = _.template(' /* <%= data %> */ ');
var templateAMDTemplate = _.template("define(<%= name ? '\"'+name + '\", ' : '' %>[<%= requirePaths %>], function (<%= requireNames %>) {\n'use strict';\n <%= injectedFunctions %>\nreturn function(){ return <%= body %>};\n});"); var templateAMDTemplate = _.template("define(<%= name ? '\"'+name + '\", ' : '' %>[<%= requirePaths %>], function (<%= requireNames %>) {\n'use strict';\n <%= injectedFunctions %>\nreturn function(){ return <%= body %>};\n});");
var templateCommonJSTemplate = _.template("<%= vars %>\n\n'use strict';\n <%= injectedFunctions %>\nmodule.exports = function(){ return <%= body %>};\n"); var templateCommonJSTemplate = _.template("'use strict';\n<%= vars %>\n\n<%= injectedFunctions %>\nmodule.exports = function(){ return <%= body %>};\n");
var templatePJSTemplate = _.template('var <%= name %> = function () {\n' + var templatePJSTemplate = _.template('var <%= name %> = function () {\n' +
'<%= injectedFunctions %>\n' + '<%= injectedFunctions %>\n' +
'return <%= body %>\n' + 'return <%= body %>\n' +

View File

@ -0,0 +1,6 @@
'use strict';
var React = require('react/addons');
var _ = require('lodash');
module.exports = function () {
return React.createElement('div', {});
};

View File

@ -0,0 +1 @@
<div rt-repeat="a in b in c"></div>

View File

@ -0,0 +1,3 @@
<rt-require />
<div>
</div>

View File

@ -0,0 +1,2 @@
<div></div>
<div></div>

View File

@ -19,7 +19,10 @@ test('invalid tests', function (t) {
{file: 'invalid-html.rt', issue: new reactTemplates.RTCodeError('Document should have a root element', -1, -1)}, {file: 'invalid-html.rt', issue: new reactTemplates.RTCodeError('Document should have a root element', -1, -1)},
{file: 'invalid-exp.rt', issue: new reactTemplates.RTCodeError("Failed to parse text '\n {z\n'", 5, -1)}, {file: 'invalid-exp.rt', issue: new reactTemplates.RTCodeError("Failed to parse text '\n {z\n'", 5, -1)},
{file: 'invalid-lambda.rt', issue: new reactTemplates.RTCodeError("when using 'on' events, use lambda '(p1,p2)=>body' notation or use {} to return a callback function. error: [onClick='']", -1, -1)}, {file: 'invalid-lambda.rt', issue: new reactTemplates.RTCodeError("when using 'on' events, use lambda '(p1,p2)=>body' notation or use {} to return a callback function. error: [onClick='']", -1, -1)},
{file: 'invalid-js.rt', issue: new reactTemplates.RTCodeError('Line 7: Unexpected token ILLEGAL', 187, undefined)} {file: 'invalid-js.rt', issue: new reactTemplates.RTCodeError('Line 7: Unexpected token ILLEGAL', 187, undefined)},
{file: 'invalid-single-root.rt', issue: new reactTemplates.RTCodeError('Document should have no more than a single root element', 12, 1)},
{file: 'invalid-repeat.rt', issue: new reactTemplates.RTCodeError('rt-repeat invalid \'in\' expression \'a in b in c\'', -1, -1)},
{file: 'invalid-rt-require.rt', issue: new reactTemplates.RTCodeError("rt-require needs 'dependency' and 'as' attributes", -1, -1)}
]; ];
t.plan(files.length); t.plan(files.length);
@ -143,6 +146,25 @@ test('conversion test amd with name', function (t) {
} }
}); });
test('conversion test commonjs', function (t) {
var files = ['div.rt'];
t.plan(files.length);
files.forEach(check);
function check(testFile) {
var filename = path.join(dataPath, testFile);
var html = readFileNormalized(filename);
var expected = readFileNormalized(filename + '.commonjs.js');
// var expected = fs.readFileSync(filename.replace(".html", ".js")).toString();
var actual = reactTemplates.convertTemplateToReact(html, {modules: 'commonjs', name: 'div'}).replace(/\r/g, '').trim();
t.equal(actual, expected);
if (actual !== expected) {
fs.writeFileSync(filename + '.actual.js', actual);
}
}
});
function normalizeHtml(html) { function normalizeHtml(html) {
return cheerio.load(html, {normalizeWhitespace: true}).html() return cheerio.load(html, {normalizeWhitespace: true}).html()
.replace(/\>\s+/mg, '>') .replace(/\>\s+/mg, '>')