Skip to content

Commit 641f597

Browse files
author
andresn
committed
merging latest/same changes in PR StephenGrider/pull/217
2 parents bd84031 + 22f0fe6 commit 641f597

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This fork contains the following updates as of 3/28/2018:
55
- Upgraded to React-Redux 5.
66
- Upgraded to Webpack 4.
77
- Changed babel preset to 'env': https://www.npmjs.com/package/babel-preset-env
8-
- Moved from using Mocha/Chai to Jest/Enzyme.
8+
- Moved from using Mocha/Chai to Jest/Enzyme. Demo test is within src/components/.
99
- Package control using Yarn instead of NPM.
1010
- Added support for JS object rest spread (...destructuring) and class properties.
1111

src/components/app.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@ export default class App extends Component {
44

55
constructor(props) {
66
super(props);
7+
this.state = {
8+
buttonText: 'Get Started!'
9+
};
10+
this.handleGetStarted = this.handleGetStarted.bind(this);
11+
}
12+
13+
handleGetStarted() {
14+
this.setState({
15+
'buttonText': 'Started!'
16+
});
717
}
818

919
render() {
20+
const buttonText = this.state.buttonText;
1021
return (
11-
<div>Redux Simple Starter</div>
22+
<div>
23+
<h1>Redux Simple Starter</h1>
24+
<button className="get-started" onClick={this.handleGetStarted}>{buttonText}</button>
25+
</div>
1226
);
1327
}
1428
}

src/components/app.test.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
import React from 'react';
2+
import {
3+
mountComponentWithRedux
4+
} from '../../test_helper';
25
import App from './app';
3-
import { shallow } from 'enzyme';
6+
47

58
describe(
6-
'App',
9+
'Components::App',
710
() => {
11+
let component;
12+
beforeEach(
13+
() => {
14+
component = mountComponentWithRedux(App);
15+
}
16+
);
17+
it(
18+
'renders',
19+
() => {
20+
expect(component.exists()).toBe(true);
21+
}
22+
);
23+
it(
24+
'renders button',
25+
() => {
26+
expect(component.find('.get-started').exists()).toBe(true);
27+
}
28+
);
829
it(
9-
'renders correctly',
30+
'button is clickable',
1031
() => {
11-
// `yarn test`, or `yarn test:watch` to run
12-
// More examples here:
13-
// https://github.com/vjwilson/enzyme-example-jest/blob/master/src/__tests__/Foo-test.js
14-
expect(
15-
shallow(<App />).contains(<div>Redux Simple Starter</div>)
16-
).toBe(true);
32+
component.find('.get-started').simulate('click');
33+
expect(component.find('.get-started').text()).toEqual('Started!');
1734
}
1835
);
1936
}

test_helper.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import { Provider } from 'react-redux';
3+
import { createStore } from 'redux';
4+
import { mount } from 'enzyme';
5+
import reducers from './src/reducers';
6+
7+
function mountComponentWithRedux(
8+
ComponentClass,
9+
state = {},
10+
props = {}
11+
) {
12+
const componentInstance = mount( // produces app by going through full React lifecycle
13+
<Provider store={createStore(reducers, state)}>
14+
<ComponentClass {...props} />
15+
</Provider>
16+
);
17+
return componentInstance;
18+
}
19+
20+
export { mountComponentWithRedux };

0 commit comments

Comments
 (0)