Skip to content

Commit

Permalink
Adding test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
toddself committed Dec 12, 2013
1 parent 29633cd commit 0d1e5eb
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*node_modules*
*.jsx
bundle.js
test1
test-2
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.8"
- "0.10"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![build status](https://secure.travis-ci.org/CondeNast/JSXHint.png)](http://travis-ci.org/CondeNast/JSXHint)

#JSXHint
A wrapper around JSHint to allow linting of files containg JSX syntax

Expand Down
72 changes: 41 additions & 31 deletions jsxhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var react = require('react-tools');
var docblock = require('react-tools/vendor/fbtransform/lib/docblock');
var runnel = require('runnel');

var prjRoot = path.dirname(require.main.filename);
var currFile = require.main ? require.main.filename : undefined;
var prjRoot = path.dirname(currFile || process.cwd());


/**
Expand All @@ -34,6 +35,7 @@ var prjRoot = path.dirname(require.main.filename);
*/
function makeIgnores(ignoreList, ignoreFile, prjRoot, cb){
var _ignores = [];
var resolvedFn;

/**
* Transform an array of strings into an array of regular
Expand Down Expand Up @@ -65,22 +67,26 @@ function makeIgnores(ignoreList, ignoreFile, prjRoot, cb){
_ignores = _ignores.concat(_makeRegex(ignoreList));
}

var resolvedFn = path.resolve(prjRoot, ignoreFile);
if(typeof ignoreFile === 'string'){
resolvedFn = path.resolve(prjRoot, ignoreFile);

fs.exists(resolvedFn, function(exists){
if(exists){
fs.readFile(resolvedFn, 'utf8', function(err, ignoreData){
if(err){
cb(err);
} else {
_ignores = _ignores.concat(_makeRegex(ignoreData.trim().split(/\n/)));
cb(null, _ignores);
}
});
} else {
cb(null, _ignores);
}
});
fs.exists(resolvedFn, function(exists){
if(exists){
fs.readFile(resolvedFn, 'utf8', function(err, ignoreData){
if(err){
cb(err);
} else {
_ignores = _ignores.concat(_makeRegex(ignoreData.trim().split(/\n/)));
cb(null, _ignores);
}
});
} else {
cb(null, _ignores);
}
});
} else {
cb(null, _ignores);
}
}

/**
Expand All @@ -92,21 +98,25 @@ function makeIgnores(ignoreList, ignoreFile, prjRoot, cb){
* @return {Object} Undefined
*/
function getJSHintRc(jshintfile, prjRoot, cb){
var resolvedFn = path.resolve(prjRoot, jshintfile);
fs.exists(resolvedFn, function(exist){
if(exist){
fs.readFile(resolvedFn, 'utf8', function(err, data){
if(err){
cb(err);
} else {
var rc = JSON.parse(data);
cb(null, rc);
}
});
} else {
cb(null, {});
}
});
if(typeof prjRoot === 'string' && typeof jshintfile === 'string'){
var resolvedFn = path.resolve(prjRoot, jshintfile);
fs.exists(resolvedFn, function(exist){
if(exist){
fs.readFile(resolvedFn, 'utf8', function(err, data){
if(err){
cb(err);
} else {
var rc = JSON.parse(data);
cb(null, rc);
}
});
} else {
cb(null, {});
}
});
} else {
cb(null, {});
}
}

/**
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"main": "jsxhint.js",
"bin": "./cli.js",
"scripts": {
"test": "./node_modules/.bin/mocha",
"lint": "./node_modules/.bin/jshint"
"test": "tap test/test.js",
"lint": "jshint",
"postinstall": "if [[ -d .git ]]; then cp pre-commit.sh .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit; fi",
"precommit": "npm run lint && npm test"
},
"repository": {
"type": "git",
Expand All @@ -32,7 +34,6 @@
"colors": "~0.6.2"
},
"devDependencies": {
"mocha": "~1.13.0",
"chai": "~1.8.0"
"tap": "~0.4.6"
}
}
2 changes: 2 additions & 0 deletions pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
npm run precommit
19 changes: 0 additions & 19 deletions test.js

This file was deleted.

61 changes: 61 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

var fs = require('fs');
var test = require('tap').test;
var jsxhint = require('../jsxhint');

test('Load and enumerate regexps from .gitignore', function(t){
t.plan(4);
var ignoreString = 'test;'
var ignoreData = fs.readFileSync('../.gitignore', 'utf-8').trim().split('\n');

jsxhint.makeIgnores([ignoreString], null, null, function(err, ignores){
t.equal(ignores.length, 1, 'More than one ignore');
var regex = new RegExp('^'+ignoreString+'$','g');
t.equal(ignores[0].toString(), regex.toString(), 'RegExp processed incorrectly');
});

jsxhint.makeIgnores(null, '.gitignore', '../', function(err, ignores){
t.equal(ignores.length, ignoreData.length, 'Should be '+ignoreData.length);
});

jsxhint.makeIgnores([ignoreString], '.gitignore', '../', function(err, ignores){
t.equal(ignores.length, ignoreData.length+1, 'Should have combined');
});
});

test('Load .jshintrc, if it exists', function(t){
t.plan(4);
var jshintRC = JSON.parse(fs.readFileSync('../.jshintrc', 'utf-8'));
jsxhint.getJSHintRc(null, null, function(err, jshintdata){
t.ifError(err);
t.deepEqual(jshintdata, {}, 'Should be an empty object');
});

jsxhint.getJSHintRc('.jshintrc', '../', function(err, jshintdata){
t.ifError(err);
t.deepEqual(jshintdata, jshintRC, 'Should match');
});
});

test('Convert JSX to JS', function(t){
t.plan(4);
jsxhint.transformJSX('./test_article.js', function(err, data){
t.ifError(err);
var containsFormElement = data.match(/<form/);
t.equal(data.match(/<form/), null, 'JS was not properly converted');
});

jsxhint.transformJSX('./test_article.jsx', function(err, data){
t.ifError(err);
t.equal(data.match(/<form/), null, 'JS was not properly converted');
});
});

test('Generate tests', function(t){
var tasks = jsxhint.generateTasks(['./test.js'], [], '', '', process.cwd());
console.log(tasks);
t.equal(tasks.length, 2, 'Should be a single task');
t.equal(typeof tasks[0], 'function', 'First task should a function');
t.end();
});
37 changes: 37 additions & 0 deletions test/test_article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** @jsx React.DOM */
'use strict'

var React = require('react-tools/build/modules/React');

var statusForm = React.createClass({
render: function(){
return <form class="form-horizontal" role="form">
<div class="form-group">
<label for="brand-lead" class="col-sm-4 control-label">Brand lead:</label>
<div class="col-sm-8">
<input class="form-control"
placeholder="brand lead"
type="text"
id="brand-lead"
ref="brandLead"
data-state="brandLead" />
</div>
</div>
<div class="form-group">
<label for="status-text" class="col-sm-4 control-label">Status:</label>
<div class="col-sm-8">
<textarea
id="status-text"
class="form-control"
rows="40"
ref="statusText"
data-state="statusText"/>
</div>
</div>

<input class="btn btn-success" type="submit" value="Save status"/>
</form>
}
});

module.exports = statusForm;
36 changes: 36 additions & 0 deletions test/test_article.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

var React = require('react-tools/build/modules/React');

var statusForm = React.createClass({
render: function(){
return <form class="form-horizontal" role="form">
<div class="form-group">
<label for="brand-lead" class="col-sm-4 control-label">Brand lead:</label>
<div class="col-sm-8">
<input class="form-control"
placeholder="brand lead"
type="text"
id="brand-lead"
ref="brandLead"
data-state="brandLead" />
</div>
</div>
<div class="form-group">
<label for="status-text" class="col-sm-4 control-label">Status:</label>
<div class="col-sm-8">
<textarea
id="status-text"
class="form-control"
rows="40"
ref="statusText"
data-state="statusText"/>
</div>
</div>

<input class="btn btn-success" type="submit" value="Save status"/>
</form>
}
});

module.exports = statusForm;

0 comments on commit 0d1e5eb

Please sign in to comment.