Skip to content

Commit

Permalink
✅ components unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
princejwesley committed Oct 6, 2015
1 parent be9b0d4 commit b0726f3
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 24 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
- "iojs"
#before_script:
# - npm install -g gulp
#script: gulp
25 changes: 11 additions & 14 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ const resources = [
const PATHS = {
APP: 'build',
DIST: 'dist',
SRC: 'src',
TMP: 'tmp',
ICON: 'icons'
ICON: 'icons',
STYLES: 'stylesheets'
};

let onError = (err) => {
Expand All @@ -46,7 +48,6 @@ let onError = (err) => {
if (err.stack) {
$.util.log('Stack trace', err.stack.toString());
}
this.emit('end');
};

async function authenticate(api) {
Expand All @@ -67,7 +68,7 @@ let zipExe = async (dists) => {
let spawn = async (command, args, options) => {
let cb = (resolve, reject) => {
let exe = ChildProcess.spawn(command, args, options);
exe.stdout.on('data', (data) => onError(data.toString('utf8')));
exe.stdout.on('data', (data) => console.log(data.toString('utf8').trim()));
exe.stderr.on('data', (err) => onError(err.toString('utf8')));
exe.on('close', resolve);
exe.on('error', reject);
Expand Down Expand Up @@ -204,15 +205,15 @@ gulp.task('react', () =>
);

gulp.task('clean', () => {
require('del').sync([PATHS.APP, PATHS.DIST]);
return require('del').sync([PATHS.APP, PATHS.DIST]);
});

gulp.task('copy', () => {
gulp.src(resources, { base: '.' })
return gulp.src(resources, { base: '.' })
.pipe(gulp.dest(PATHS.APP));
});

gulp.task('watch', (cb) => {
gulp.task('watch',['build'], (cb) => {
$.livereload.listen();
gulp.watch(options.sass.source, ['sass']);
gulp.watch(options.react.source, ['react']);
Expand All @@ -239,7 +240,7 @@ gulp.task('dev-env', () => {
});

gulp.task('build', (cb) => {
runSequence('user-env', ['clean', 'sass', 'react', 'copy'], () => cb());
return runSequence('clean', 'copy', ['sass', 'react'], cb);
});

gulp.task('package', ['build'], (cb) => {
Expand Down Expand Up @@ -270,7 +271,7 @@ gulp.task('packageAll', ['build'], (cb) => {
})();
});

gulp.task('run',[], (cb) => {
gulp.task('run', (cb) => {
(async () => {
try {
await spawn('./node_modules/.bin/electron', [PATHS.APP]);
Expand All @@ -282,13 +283,9 @@ gulp.task('run',[], (cb) => {
})();
});

gulp.task('start', (cb) => {
runSequence('run',['build'], () => cb());
});
gulp.task('start',['user-env'], (cb) => runSequence('build', 'run', cb));

gulp.task('dev', (cb) => {
runSequence('run',['build', 'dev-env'], () => cb());
});
gulp.task('debug', ['build'], (cb) => runSequence('dev-env', 'run', cb));

gulp.task('release',['build'], (cb) => {
(async () => {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
"main": "main/index.js",
"scripts": {
"preinstall": "npm prune",
"test": "BABEL_JEST_STAGE=0 ./node_modules/.bin/jest",
"test": "./node_modules/.bin/jest",
"start": "./node_modules/.bin/gulp start",
"dev": "./node_modules/.bin/gulp dev",
"debug": "./node_modules/.bin/gulp debug",
"package": "./node_modules/.bin/gulp package",
"install": "./node_modules/.bin/gulp package",
"package-all": "./node_modules/.bin/gulp packageAll",
"release": "./node_modules/.bin/gulp release",
"watch": "./node_modules/.bin/gulp watch",
Expand Down
2 changes: 1 addition & 1 deletion src/components/ReplEntryOutputError.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class ReplEntryOutputError extends React.Component {
closeBrace = ')';
}
output.push(
<div className='repl-entry-output-error-stack-lines'>
<div className='repl-entry-output-error-stack-lines' key={output.length}>
<span className='stack-error-at'>&nbsp;&nbsp;at</span>
<span className='stack-error-function'>{p1}</span>
{openBrace}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ReplOutputArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class ReplOutputArray extends React.Component {
{
_.map(this.props.array, (value, idx) => {
return (
<div className='array-entry'>
<div className='array-entry' key={idx}>
{
this.props.noIndex
? null
Expand Down
4 changes: 2 additions & 2 deletions src/components/ReplOutputFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class ReplOutputFunction extends React.Component {
this.state.collapse
? <span className='repl-entry-message-output-object'>
<i className='fa fa-play' onClick={this.onToggleCollapse}></i>
<span className='array-desc'>{label}</span>
<span className='object-desc'>{label}</span>
</span>
: <span className='repl-entry-message-output-object'>
<i className='fa fa-play fa-rotate-90' onClick={this.onToggleCollapse}></i>
Expand All @@ -44,7 +44,7 @@ export default class ReplOutputFunction extends React.Component {
_.map(_.keys(this.props.fun), (key) => {
let value = this.props.fun[key];
return (
<div className='object-entry'>
<div className='object-entry' key={key}>
{
<span className='object-key'>
{key}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ReplOutputObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class ReplOutputObject extends React.Component {
this.state.collapse
? <span className='repl-entry-message-output-object'>
<i className='fa fa-play' onClick={this.onToggleCollapse}></i>
<span className='array-desc'>{label}</span>
<span className='object-desc'>{label}</span>
</span>
: <span className='repl-entry-message-output-object'>
<i className='fa fa-play fa-rotate-90' onClick={this.onToggleCollapse}></i>
Expand All @@ -35,7 +35,7 @@ export default class ReplOutputObject extends React.Component {
{
_.map(this.props.object, (value, key) => {
return (
<div className='object-entry'>
<div className='object-entry' key={key}>
{
<span className='object-key'>
{key}
Expand Down
8 changes: 8 additions & 0 deletions src/components/__mocks__/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

module.exports = {
writeText: jest.genMockFunction(),
readText: jest.genMockFunction(),
readHTML: jest.genMockFunction(),
writeHTML: jest.genMockFunction(),
clear: jest.genMockFunction()
};
2 changes: 1 addition & 1 deletion src/components/__mocks__/shell.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

module.exports = { beep: jest.genMockFunction() }
module.exports = { beep: jest.genMockFunction() };
24 changes: 24 additions & 0 deletions src/components/__tests__/ReplEntries-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

jest.dontMock('../ReplEntries.js');
jest.dontMock('../ReplEntry.js');
jest.dontMock('md5');

describe('ReplEntries', () => {
let React = require('react/addons');
let ReplEntries = require('../ReplEntries.js');
let TestUtils = React.addons.TestUtils;
let entries = [{
formattedOutput: '<span class="literal"> undefined </span>',
plainCode: 'let name = "mancy"',
status: true,
command: '<span class="literal">let</span> name = <span class="string">"mancy"</span>'
}];

it('should rendered properly', () => {
let component = TestUtils.renderIntoDocument(
<ReplEntries entries={entries} />
);
let children = TestUtils.scryRenderedDOMComponentsWithClass(component, 'repl-entry');
expect(children.length).toBe(entries.length);
});
});
55 changes: 55 additions & 0 deletions src/components/__tests__/ReplEntry-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import ReplActions from '../../actions/ReplActions';

jest.dontMock('../ReplEntry.js');
jest.dontMock('../ReplEntryIcon.js');
jest.dontMock('../ReplEntryStatus.js');

describe('ReplEntry', () => {
let React = require('react/addons');
let ReplEntry = require('../ReplEntry.js');
let TestUtils = React.addons.TestUtils;
let entry = {
formattedOutput: '<span class="literal"> undefined </span>',
plainCode: 'let name = "mancy"',
status: true,
command: '<span class="literal">let</span> name = <span class="string">"mancy"</span>'
};

it('should collapse', () => {
let component = TestUtils.renderIntoDocument(
<ReplEntry log={entry} index={0} key={Date.now}/>
);
let icon = TestUtils.findRenderedDOMComponentWithClass(component, 'repl-entry-icon');
let action = TestUtils.findRenderedDOMComponentWithTag(icon, 'i');
TestUtils.Simulate.click(action);
expect(ReplActions.toggleCommandEntryView).toBeCalled();
});

it('should toggle entry', () => {
let component = TestUtils.renderIntoDocument(
<ReplEntry log={entry} index={0} key={Date.now}/>
);
let icon = TestUtils.findRenderedDOMComponentWithClass(component, 'fa fa-minus-circle');
TestUtils.Simulate.click(icon);
expect(ReplActions.toggleEntryView).toBeCalled();
});

it('should remove', () => {
let component = TestUtils.renderIntoDocument(
<ReplEntry log={entry} index={0} key={Date.now}/>
);
let icon = TestUtils.findRenderedDOMComponentWithClass(component, 'fa-times-circle');
TestUtils.Simulate.click(icon);
expect(ReplActions.removeEntry).toBeCalled();
});

it('should reload prompt', () => {
let component = TestUtils.renderIntoDocument(
<ReplEntry log={entry} index={0} key={Date.now}/>
);
let icon = TestUtils.findRenderedDOMComponentWithClass(component, 'repeat');
TestUtils.Simulate.click(icon);
expect(ReplActions.reloadPrompt).toBeCalled();
});

});
33 changes: 33 additions & 0 deletions src/components/__tests__/ReplEntryOutputError-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

jest.dontMock('../ReplEntryOutputError.js');

describe('ReplEntryOutputError', () => {
let React = require('react/addons');
let ReplEntryOutputError = require('../ReplEntryOutputError.js');
let TestUtils = React.addons.TestUtils;
let error = 'name is not defined';
let errorFile = 'repl.js';
let errorFunction = 'REPLServer.defaultEval';
let errorLine = '166';
let errorColumn = '27';
let msg = `ReferenceError: ${error}`;
let trace = [`at ${errorFunction} (${errorFile}:${errorLine}:${errorColumn})`];

it('should rendered properly', () => {
let component = TestUtils.renderIntoDocument(
<ReplEntryOutputError message={msg} trace={trace}>
</ReplEntryOutputError>
);
let errorMsg = TestUtils.findRenderedDOMComponentWithClass(component, 'repl-entry-output-error-message');
let errorTraceFile = TestUtils.findRenderedDOMComponentWithClass(component, 'stack-error-file');
let errorTraceFunction = TestUtils.findRenderedDOMComponentWithClass(component, 'stack-error-function');
let errorTraceLine = TestUtils.findRenderedDOMComponentWithClass(component, 'stack-error-row');
let errorTraceColumn = TestUtils.findRenderedDOMComponentWithClass(component, 'stack-error-column');

expect(React.findDOMNode(errorMsg).textContent).toContain(error);
expect(React.findDOMNode(errorTraceFile).textContent).toContain(errorFile);
expect(React.findDOMNode(errorTraceFunction).textContent).toContain(errorFunction);
expect(React.findDOMNode(errorTraceLine).textContent).toContain(errorLine);
expect(React.findDOMNode(errorTraceColumn).textContent).toContain(errorColumn);
});
});
36 changes: 36 additions & 0 deletions src/components/__tests__/ReplOutputArray-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

jest.dontMock('../ReplOutputArray.js');
jest.dontMock('../../common/ReplOutput.js');

describe('ReplOutputArray', () => {
let React = require('react/addons');
let ReplOutputArray = require('../ReplOutputArray.js');
let TestUtils = React.addons.TestUtils;
let label = 'Array[7]';
let arr = [0, true, 'yes', null, undefined, [1], {fun: () => {}}];

it('should have collapsed array', () => {
let component = TestUtils.renderIntoDocument(
<ReplOutputArray array={arr} label={label} start={0} noIndex={false}/>
);

let desc = TestUtils.findRenderedDOMComponentWithClass(component, 'array-desc');
expect(React.findDOMNode(desc).textContent).toEqual(label);
let entry = TestUtils.scryRenderedDOMComponentsWithClass(component, 'array-entry');
expect(entry.length).toBe(0);
});

it('should expand array', () => {
let component = TestUtils.renderIntoDocument(
<ReplOutputArray array={arr} label={label} start={0} noIndex={false}/>
);

let icon = TestUtils.findRenderedDOMComponentWithClass(component, 'fa-play');
TestUtils.Simulate.click(icon);
jest.runAllTicks();

let entries = TestUtils.scryRenderedDOMComponentsWithClass(component, 'array-entry');
expect(entries.length).toBe(arr.length);
});

});
63 changes: 63 additions & 0 deletions src/components/__tests__/ReplOutputFunction-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

jest.dontMock('../ReplOutputFunction.js');
jest.dontMock('../../common/ReplOutput.js');

describe('ReplOutputFunction', () => {
let React = require('react/addons');
let ReplOutputFunction = require('../ReplOutputFunction.js');
let TestUtils = React.addons.TestUtils;
let label = ' function() {}';
let f = function test() {
return 'test';
};
f.desc = 'testMe';
let funElement = `
<span class='literal'>function</span><span> test() {
<span class='literal'>return</span> <span class='string'> 'test'</span>;
}
</span>
`;
let shortElement = `
<span class='literal'>function</span><span> test() {
</span>
`;

it('should have collapsed function', () => {
let component = TestUtils.renderIntoDocument(
<ReplOutputFunction html={funElement} fun={f} expandable={false} short={shortElement}/>
);

let desc = TestUtils.findRenderedDOMComponentWithClass(component, 'object-desc');
expect(React.findDOMNode(desc).textContent).toEqual(label);
let entry = TestUtils.scryRenderedDOMComponentsWithClass(component, 'object-entry');
expect(entry.length).toBe(0);
});

it('should expand function', () => {
let component = TestUtils.renderIntoDocument(
<ReplOutputFunction html={funElement} fun={f} expandable={false} short={shortElement}/>
);

let icon = TestUtils.findRenderedDOMComponentWithClass(component, 'fa-play');
TestUtils.Simulate.click(icon);
jest.runAllTicks();

let entries = TestUtils.scryRenderedDOMComponentsWithClass(component, 'object-entry');
expect(entries.length).toBe(Object.keys(f).length);
});

it('should expand function source', () => {
let component = TestUtils.renderIntoDocument(
<ReplOutputFunction html={funElement} fun={f} expandable={true} short={shortElement}/>
);

let icon = TestUtils.findRenderedDOMComponentWithClass(component, 'fa-play');
TestUtils.Simulate.click(icon);
jest.runAllTicks();
let source = TestUtils.findRenderedDOMComponentWithClass(component, 'fa-plus-square-o');
TestUtils.Simulate.click(source);
jest.runAllTicks();
TestUtils.scryRenderedDOMComponentsWithClass(component, 'fa-minus-square-o');
});

});
Loading

0 comments on commit b0726f3

Please sign in to comment.