From 3b9d8579fa16500adffdc0476f90e47313528af2 Mon Sep 17 00:00:00 2001 From: ido Date: Thu, 22 Jan 2015 18:02:18 +0200 Subject: [PATCH] increase test coverage --- src/reactTemplates.js | 2 +- test/data/div.rt.commonjs.js | 6 ++++++ test/data/invalid-repeat.rt | 1 + test/data/invalid-rt-require.rt | 3 +++ test/data/invalid-single-root.rt | 2 ++ test/src/test.js | 24 +++++++++++++++++++++++- 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/data/div.rt.commonjs.js create mode 100644 test/data/invalid-repeat.rt create mode 100644 test/data/invalid-rt-require.rt create mode 100644 test/data/invalid-single-root.rt diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 9589704..0fed9ba 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -19,7 +19,7 @@ var simpleTagTemplateCreateElement = _.template('React.createElement(<%= name %> var tagTemplateCreateElement = _.template('React.createElement.apply(this,_.flatten([<%= name %>,<%= props %><%= children %>]))'); var commentTemplate = _.template(' /* <%= data %> */ '); 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' + '<%= injectedFunctions %>\n' + 'return <%= body %>\n' + diff --git a/test/data/div.rt.commonjs.js b/test/data/div.rt.commonjs.js new file mode 100644 index 0000000..d40bb9b --- /dev/null +++ b/test/data/div.rt.commonjs.js @@ -0,0 +1,6 @@ +'use strict'; +var React = require('react/addons'); +var _ = require('lodash'); +module.exports = function () { + return React.createElement('div', {}); +}; \ No newline at end of file diff --git a/test/data/invalid-repeat.rt b/test/data/invalid-repeat.rt new file mode 100644 index 0000000..74cfdb8 --- /dev/null +++ b/test/data/invalid-repeat.rt @@ -0,0 +1 @@ +
diff --git a/test/data/invalid-rt-require.rt b/test/data/invalid-rt-require.rt new file mode 100644 index 0000000..938c91b --- /dev/null +++ b/test/data/invalid-rt-require.rt @@ -0,0 +1,3 @@ + +
+
diff --git a/test/data/invalid-single-root.rt b/test/data/invalid-single-root.rt new file mode 100644 index 0000000..5261b93 --- /dev/null +++ b/test/data/invalid-single-root.rt @@ -0,0 +1,2 @@ +
+
diff --git a/test/src/test.js b/test/src/test.js index 4039879..7aacf31 100644 --- a/test/src/test.js +++ b/test/src/test.js @@ -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-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-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); @@ -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) { return cheerio.load(html, {normalizeWhitespace: true}).html() .replace(/\>\s+/mg, '>')