Skip to content

Commit 6ecaaef

Browse files
committed
Component Interface with PropTypes (End 4 Chapter)
1 parent ef1ce4c commit 6ecaaef

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"axios": "^0.18.0",
7+
"prop-types": "^15.7.2",
78
"react": "^16.8.4",
89
"react-dom": "^16.8.4",
910
"react-scripts": "2.1.8"

src/App.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react';
22
import axios from 'axios';
3+
import PropTypes from 'prop-types';
34
import './App.css';
45

56
const DEFAULT_QUERY = 'redux';
@@ -204,10 +205,22 @@ const Table = ({ list, onDismiss }) =>
204205
)}
205206
</div>
206207

208+
Table.propTypes = {
209+
list: PropTypes.arrayOf(
210+
PropTypes.shape({
211+
objectID: PropTypes.string.isRequired,
212+
author: PropTypes.string,
213+
url: PropTypes.string,
214+
num_comments: PropTypes.number,
215+
points: PropTypes.number,
216+
})
217+
).isRequired,
218+
onDismiss: PropTypes.func.isRequired,
219+
};
207220

208221
const Button = ({
209222
onClick,
210-
className = '',
223+
className,
211224
children,
212225
}) =>
213226
<button
@@ -218,6 +231,16 @@ const Button = ({
218231
{children}
219232
</button>
220233

234+
Button.defaultProps = {
235+
className: '',
236+
};
237+
238+
Button.propTypes = {
239+
onClick: PropTypes.func.isRequired,
240+
className: PropTypes.string,
241+
children: PropTypes.node.isRequired,
242+
};
243+
221244
export default App;
222245

223246
export {

src/App.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ describe('Button', () => {
4747

4848
it('renders without crashing', () => {
4949
const div = document.createElement('div');
50-
ReactDOM.render(<Button>Give Me More</Button>, div);
50+
ReactDOM.render(<Button onClick={() => {}}>Give Me More</Button>, div);
5151
ReactDOM.unmountComponentAtNode(div);
5252
});
5353

5454
test('has a valid snapshot', () => {
5555
const component = renderer.create(
56-
<Button>Give Me More</Button>
56+
<Button onClick={() => {}}>Give Me More</Button>
5757
);
5858
const tree = component.toJSON();
5959
expect(tree).toMatchSnapshot();

src/__snapshots__/App.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ exports[`App has a valid snapshot 1`] = `
4242
exports[`Button has a valid snapshot 1`] = `
4343
<button
4444
className=""
45+
onClick={[Function]}
4546
type="button"
4647
>
4748
Give Me More

0 commit comments

Comments
 (0)