Skip to content

Commit d792eea

Browse files
committed
Merge pull request #38 from AdamBrodzinski/karma-test
Karma + Jasmine test runner
2 parents 113695c + 4290d8d commit d792eea

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

app/components/tests/app_spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// See this for some tips with Meteor: https://medium.com/all-about-meteorjs/unit-test-react-components-in-meteor-a19d96684d7d
2+
3+
// If you import ../App you'll have to stub out Meteor, this is
4+
// why it's important to use controller-views that just setup
5+
// data and then the children can be very easily tested with
6+
// just props and state. We'll use a local component for an example
7+
8+
import React from 'react/addons';
9+
//import App from '../App';
10+
//import $ from 'jquery'; // you could use jq to make life easier
11+
const TestUtils = React.addons.TestUtils;
12+
const Simulate = TestUtils.Simulate;
13+
14+
// these should go into a spec helper module
15+
16+
function renderComponent(comp, props) {
17+
return TestUtils.renderIntoDocument(
18+
React.createElement(comp, props)
19+
);
20+
}
21+
22+
function simulateClickOn(selector) {
23+
var button = this.$el.find(selector)[0];
24+
React.addons.TestUtils.Simulate.click(button);
25+
}
26+
27+
28+
const Post = React.createClass({
29+
getDefaultProps() {
30+
return {title: "Default Post Name"};
31+
},
32+
getInitialState() {
33+
return { isVisible: true };
34+
},
35+
handleHide() {
36+
this.setState({isVisible: false});
37+
},
38+
render() {
39+
let visibleClass = (this.state.isVisible) ? 'block' : 'hidden';
40+
return (
41+
<div className='Post' style={{display: visibleClass }}>
42+
<h1>{this.props.title}</h1>
43+
<article>
44+
How now brown cow
45+
</article>
46+
<button onClick={this.handleHide}>Hide</button>
47+
</div>
48+
);
49+
}
50+
});
51+
52+
53+
describe('Sample post component', () => {
54+
it('renders default post name without props', () => {
55+
let comp = renderComponent(Post, {});
56+
expect(comp.props.title).toEqual('Default Post Name');
57+
});
58+
59+
it('renders correct post name with a name prop', () => {
60+
let comp = renderComponent(Post, {title: "Webpack is awesome!"});
61+
expect(comp.props.title).toEqual("Webpack is awesome!");
62+
});
63+
64+
it("should have a default state of visible", () => {
65+
let comp = renderComponent(Post, {});
66+
expect(comp.state.isVisible).toEqual(true);
67+
});
68+
69+
it("should hide when hide button is clicked", () => {
70+
let comp = renderComponent(Post, {});
71+
comp.handleHide();
72+
expect(comp.state.isVisible).toEqual(false);
73+
});
74+
});

karma.conf.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var path = require('path');
2+
var webpackConfig = require('./webpack/webpack.config.client.js');
3+
4+
module.exports = function (config) {
5+
config.set({
6+
//singleRun: true,
7+
reporters: [ 'dots' ],
8+
browsers: [ 'Chrome' ],
9+
files: [ './test/karma.bundle.js' ],
10+
frameworks: [ 'jasmine' ],
11+
plugins: [
12+
'karma-chrome-launcher',
13+
//'karma-firefox-launcher',
14+
'karma-jasmine',
15+
//'karma-mocha',
16+
'karma-sourcemap-loader',
17+
'karma-webpack',
18+
],
19+
// run the bundle through the webpack and sourcemap plugins
20+
preprocessors: {
21+
'./test/karma.bundle.js': [ 'webpack', 'sourcemap' ]
22+
},
23+
// use our own webpack config to mirror test setup
24+
webpack: {
25+
entry: [
26+
'./lib/core-js-no-number',
27+
'regenerator/runtime',
28+
],
29+
devtool: 'eval-source-map',
30+
resolve: webpackConfig.resolve,
31+
module: { loaders: webpackConfig.module.loaders },
32+
},
33+
webpackMiddleware: {
34+
noInfo: true,
35+
}
36+
});
37+
};

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"eslint-plugin-react": "^3.2.2",
1616
"grunt": "^0.4.5",
1717
"grunt-cli": "^0.1.13",
18+
"karma": "^0.13.9",
19+
"karma-chrome-launcher": "^0.2.0",
20+
"karma-jasmine": "^0.2.2",
21+
"karma-sourcemap-loader": "^0.3.5",
22+
"karma-webpack": "^1.7.0",
1823
"node-libs-browser": "^0.5.2",
1924
"react-transform-catch-errors": "^1.0.0",
2025
"react-transform-hmr": "^1.0.0",

test/karma.bundle.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// require all foo_spec.js, bar_spec.jsx files in the app directory
2+
var context = require.context('../app', true, /.+_spec\.jsx?$/);
3+
context.keys().forEach(context);
4+
module.exports = context;

0 commit comments

Comments
 (0)