-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-add sample components for reference
- Loading branch information
1 parent
2d5f3e5
commit 78d022d
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// ES6 import syntax. | ||
import React, {Component} from 'react'; | ||
|
||
// All React components should be created this way, by extending the | ||
// basic React component class. | ||
class Counter extends Component { | ||
// `displayName` is how the component will show up in the React dev tools. | ||
static displayName = 'Counter'; | ||
|
||
// React components use "state" to manage local state that | ||
// stays within a component. | ||
// This count variable is accessible via `this.state` | ||
state = { | ||
count: 0 | ||
}; | ||
|
||
render () { | ||
// Using JSX to render HTML to the DOM | ||
// The DOM will auto-update the visible count whenever our state changes. | ||
// We add an onClick handler function to the button for incrememting count. | ||
return ( | ||
<div> | ||
<p>{this.state.count}</p> | ||
<button onClick={this._onClick}> | ||
Increase | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
// Handles updating the state of the component when the button is clicked. | ||
_onClick = () => { | ||
// NEVER modify `this.state` directly. Always use `this.setState` instead. | ||
this.setState({ | ||
count: this.state.count + 1 | ||
}); | ||
}; | ||
} | ||
|
||
// Export as default so it can be imported with `import Counter from './Counter'` | ||
export default Counter; | ||
// You can also export secondary named modules, for instance: | ||
export {Counter}; | ||
// That way it can be imported as `import {Counter} from './Counter'` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// More comments in the `Counter file` | ||
import React, {Component, PropTypes} from 'react'; | ||
// Relative imports | ||
import Counter from './Counter'; | ||
|
||
// Export and class declaration all in one line. | ||
// This is the idiomatic way to declare components. | ||
export default class Hello extends Component { | ||
static displayName = 'Hello'; | ||
|
||
// propTypes declare what sort of `props` are expected as input | ||
// from parent components. The `PropTypes` package declares a | ||
// number of useful type checking functions like `PropTypes.string` | ||
static propTypes = { | ||
name: PropTypes.string | ||
}; | ||
|
||
// defaultProps are used if no props are given from the parent component. | ||
static defaultProps = { | ||
name: 'John Appleseed' | ||
}; | ||
|
||
render () { | ||
// Here we instantiate an instance of our Counter as a child component. | ||
return ( | ||
<div> | ||
<h1>Hello {this.props.name}!</h1> | ||
<p>Welcome to React. Here's a counter.</p> | ||
<Counter /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Counter from './../Counter'; | ||
import React from 'react'; | ||
import {shallow} from 'enzyme'; | ||
|
||
describe('<Counter />', () => { | ||
|
||
it('should render a button', () => { | ||
const wrapper = shallow(<Counter />); | ||
expect(wrapper.find('button').length).toEqual(1); | ||
}); | ||
|
||
it('should render a p', () => { | ||
const wrapper = shallow(<Counter />); | ||
expect(wrapper.find('p').length).toEqual(1); | ||
}); | ||
|
||
it('should incrememt the count when clicked', () => { | ||
const wrapper = shallow(<Counter />); | ||
const button = wrapper.find('button'); | ||
button.simulate('click'); | ||
expect(wrapper.state('count')).toEqual(1); | ||
|
||
for (let i = 0; i < 10; i++) { | ||
button.simulate('click'); | ||
} | ||
expect(wrapper.state('count')).toEqual(11); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Counter from './../Counter'; | ||
import Hello from './../Hello'; | ||
import React from 'react'; | ||
import {shallow} from 'enzyme'; | ||
|
||
describe('<Hello />', () => { | ||
|
||
it('should render a counter', () => { | ||
const wrapper = shallow(<Hello />); | ||
expect(wrapper.contains(<Counter />)).toBe(true); | ||
}); | ||
|
||
it('should say hello', () => { | ||
const wrapper = shallow(<Hello name="Jordan" />); | ||
expect(wrapper.find('h1').text()).toEqual('Hello Jordan!'); | ||
}); | ||
}); |