Skip to content

Commit

Permalink
Added mocking example. Refs #18
Browse files Browse the repository at this point in the history
  • Loading branch information
badsyntax committed Jul 21, 2015
1 parent 30d38ea commit 7a5fc1c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ export default class Menu extends Component {
###Writing tests:

```js
// Filename: __tests__/Menu-test.js
// Filename: __tests__/Menu-test.jsx

'use strict';

import React from 'react/addons';
import Menu from '../Menu.jsx';
import { expect } from 'chai';

let { TestUtils } = React.addons;

Expand All @@ -104,6 +105,7 @@ describe('Menu', () => {
expect(menuElem.querySelectorAll('li').length).to.equal(2);
});
});

```

## Sass, CSS & webpack
Expand Down
17 changes: 4 additions & 13 deletions app/components/Menu/Menu.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Filename: Menu.jsx

import './_Menu.scss';
import React from 'react';
import MenuItem from './MenuItem';

let { Component, PropTypes } = React;

Expand All @@ -15,20 +14,12 @@ export default class Menu extends Component {
items: PropTypes.array.isRequired
};

state = {
foo: false
}

renderItem(item) {
return (
<li key={'menu-item-' + item.id}>{item.label}</li>
);
}

render() {
return (
<ul className={'menu'}>
{this.props.items.map(this.renderItem, this)}
{this.props.items.map((item) => {
return (<MenuItem item={item} />);
}, this)}
</ul>
);
}
Expand Down
16 changes: 16 additions & 0 deletions app/components/Menu/MenuItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

let { Component, PropTypes } = React;

export default class MenuItem extends Component {

static propTypes = {
item: PropTypes.object.isRequired
};

render() {
return (
<li key={'menu-item-' + this.props.item.id}>{this.props.item.label}</li>
);
}
}
42 changes: 42 additions & 0 deletions app/components/Menu/__tests__/Menu-test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react/addons';
import { expect } from 'chai';

import Menu from '../Menu.jsx';
import MenuItem from '../MenuItem.jsx';

// Here we create a mocked MenuItem component.
class MockedMenuItem extends MenuItem {
render() {
return (
<li className={'mocked-menu-item'}>{this.props.item.label}</li>
);
}
}

// Here we set the mocked MenuItem component.
Menu.__Rewire__('MenuItem', MockedMenuItem);

describe('Menu', () => {

let { TestUtils } = React.addons;

let menuItems = [
{ id: 1, label: 'Option 1' },
{ id: 2, label: 'Option 2' }
];

let menu = TestUtils.renderIntoDocument(
<Menu items={menuItems} />
);
let menuElem = React.findDOMNode(menu);

it('Should render the menu items', () => {
expect(menuElem.querySelectorAll('li').length).to.equal(2);
expect(menuElem.querySelectorAll('li')[0].textContent.trim()).to.equal(menuItems[0].label);
expect(menuElem.querySelectorAll('li')[1].textContent.trim()).to.equal(menuItems[1].label);
});

it('Should render the mocked menu item', () => {
expect(menuElem.querySelectorAll('li')[0].className).to.equal('mocked-menu-item');
});
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
},
"devDependencies": {
"autoprefixer-core": "^5.2.1",
"babel-core": "^5.6.15",
"babel-core": "^5.8.3",
"babel-eslint": "^3.1.23",
"babel-loader": "^5.3.1",
"babel-plugin-rewire": "^0.1.8",
"babel-runtime": "^5.6.15",
"chai": "^3.0.0",
"css-loader": "^0.15.2",
Expand Down Expand Up @@ -108,6 +109,7 @@
2,
"single"
],
"no-underscore-dangle": 0,
"react/display-name": 0,
"react/jsx-quotes": 1,
"react/jsx-no-undef": 1,
Expand All @@ -118,7 +120,7 @@
"react/no-did-update-set-state": 1,
"react/no-multi-comp": 1,
"react/no-unknown-property": 1,
"react/prop-types": 1,
"react/prop-types": 0,
"react/react-in-jsx-scope": 1,
"react/self-closing-comp": 1,
"react/wrap-multilines": 1
Expand Down
4 changes: 2 additions & 2 deletions webpack/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (DEBUG || TEST) {
if (!TEST) {
jsxLoader.push('react-hot');
}
jsxLoader.push('babel-loader?optional[]=runtime&stage=0');
jsxLoader.push('babel-loader?optional[]=runtime&stage=0&plugins=rewire');
sassParams.push('sourceMap', 'sourceMapContents=true');
sassLoader = [
'style-loader',
Expand All @@ -46,7 +46,7 @@ if (DEBUG || TEST) {
'postcss-loader'
].join('!');
} else {
jsxLoader = ['babel-loader?optional[]=runtime&stage=0'];
jsxLoader = ['babel-loader?optional[]=runtime&stage=0&plugins=rewire'];
sassLoader = ExtractTextPlugin.extract('style-loader', [
'css-loader',
'postcss-loader',
Expand Down

0 comments on commit 7a5fc1c

Please sign in to comment.