Skip to content

Commit

Permalink
Add enzyme's mount API for testing.
Browse files Browse the repository at this point in the history
So, we don't need to use classNames
  • Loading branch information
arunoda committed Apr 18, 2016
1 parent da322d3 commit 4d7479c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dist/client/ui/foldable.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ var Foldable = function (_React$Component) {
{ style: folderSidebarStyle },
_react2.default.createElement(
'span',
{ className: 'foldable-toggle', onClick: this.onToggleCallback },
{ ref: 'foldable-toggle', onClick: this.onToggleCallback },
this.state.collapsed ? '►' : '▼'
)
),
_react2.default.createElement(
'div',
{ className: 'foldable-content', style: folderContentStyle },
{ ref: 'foldable-content', style: folderContentStyle },
content
)
);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"enzyme": "^2.2.0",
"expect": "^1.6.0",
"express": "^4.13.3",
"jsdom": "^8.4.0",
"json-stringify-safe": "^5.0.1",
"node-libs-browser": "^0.5.2",
"page-bus": "^3.0.1",
Expand Down
18 changes: 18 additions & 0 deletions scripts/mocha_runner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
require('babel-core/register');
require('babel-polyfill');

// Add jsdom support, which is required for enzyme.
var jsdom = require('jsdom').jsdom;

var exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});

global.navigator = {
userAgent: 'node.js'
};

process.on('unhandledRejection', function (error) {
console.error('Unhandled Promise Rejection:');
console.error(error && error.stack || error);
Expand Down
16 changes: 7 additions & 9 deletions src/client/ui/__tests__/foldable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { describe, it } = global;
import { expect } from 'chai';
import { shallow } from 'enzyme';
import { mount } from 'enzyme';
import React from 'react';
import Foldable from '../foldable';

Expand All @@ -14,9 +14,8 @@ describe('<Foldable />', function () {

const compactString = '{"name":"test action","args":"things"}';

const wrap = shallow(<Foldable action={data} />);
const content = wrap.find('.foldable-content').first();

const wrap = mount(<Foldable action={data} />);
const content = wrap.ref('foldable-content');
expect(content.text()).to.equal(compactString);
});

Expand All @@ -26,17 +25,16 @@ describe('<Foldable />', function () {
args: 'things',
};

const fullString = '{ "name": "test action",\n "args": "things"\n}';
const fullString = '{\n "name": "test action",\n "args": "things"\n}';

const wrap = shallow(<Foldable action={data} />);
const toggle = wrap.find('.foldable-toggle').first();
const wrap = mount(<Foldable action={data} />);
const toggle = wrap.ref('foldable-toggle');

toggle.simulate('click');

expect(wrap.state()).to.deep.equal({ collapsed: false });

const content = wrap.find('.foldable-content').first();

const content = wrap.ref('foldable-content');
expect(content.text()).to.equal(fullString);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/client/ui/foldable.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ class Foldable extends React.Component {
return (
<div ref="folder" style={ folderStyle }>
<div style={ folderSidebarStyle }>
<span className="foldable-toggle" onClick={ this.onToggleCallback }>
<span ref="foldable-toggle" onClick={ this.onToggleCallback }>
{ this.state.collapsed ? '►' : '▼' }
</span>
</div>

<div className="foldable-content" style={ folderContentStyle }>
<div ref="foldable-content" style={ folderContentStyle }>
{ content }
</div>
</div>
Expand Down

0 comments on commit 4d7479c

Please sign in to comment.