Skip to content

Commit

Permalink
Merge branch 'fix-components-proptypes'
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisBarranqueiro committed Aug 17, 2016
2 parents 5104064 + 8c3b572 commit 839baff
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/components/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Notification extends Component {
buttons: React.PropTypes.func.isRequired,
button: React.PropTypes.string.isRequired,
buttonText: React.PropTypes.string.isRequired
}),
}).isRequired,
notification: React.PropTypes.shape({
id: React.PropTypes.number.isRequired,
title: React.PropTypes.string,
Expand All @@ -55,9 +55,9 @@ export class Notification extends Component {
name: React.PropTypes.string.isRequired,
onClick: React.PropTypes.func
})
),
allowHTML: React.PropTypes.bool
}),
).isRequired,
allowHTML: React.PropTypes.bool.isRequired
}).isRequired,
removeNotification: React.PropTypes.func.isRequired
};

Expand Down
12 changes: 6 additions & 6 deletions src/components/NotificationsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ export class NotificationsContainer extends Component {
dismissAfter: React.PropTypes.number.isRequired,
closeButton: React.PropTypes.bool.isRequired,
allowHTML: React.PropTypes.bool.isRequired
}),
}).isRequired,
theme: React.PropTypes.shape({
notificationsContainer: React.PropTypes.shape({
className: React.PropTypes.shape({
main: React.PropTypes.string.isRequired,
position: React.PropTypes.func.isRequired
}),
}).isRequired,
transition: React.PropTypes.shape({
name: React.PropTypes.object.isRequired,
enterTimeout: React.PropTypes.number.isRequired,
leaveTimeout: React.PropTypes.number.isRequired
})
}),
}).isRequired
}).isRequired,
notification: React.PropTypes.shape({
className: React.PropTypes.object.isRequired
})
})
}).isRequired
}).isRequired
};

/**
Expand Down
17 changes: 14 additions & 3 deletions test/components/Notification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ describe('<Notification/>', () => {
store = mockStore({notifications: []});
});

it('should not throw error during propTypes validation', () => {
checkPropTypes({
it('should validate props', () => {
const errors = checkPropTypes({
className,
notification,
removeNotification
}, Notification.propTypes, true);
}, Notification.propTypes);

expect(errors.className).toNotExist();
expect(errors.notification).toNotExist();
expect(errors.removeNotification).toNotExist();
});

it('should not validate props', () => {
const errors = checkPropTypes({}, Notification.propTypes);
expect(errors.className).toExist();
expect(errors.notification).toExist();
expect(errors.removeNotification).toExist();
});

it('should mount with initial state', () => {
Expand Down
19 changes: 16 additions & 3 deletions test/components/NotificationsContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@ describe('<NotificationsContainer/>', () => {
store = mockStore({notifications: []});
});

it('should not throw error during propTypes validation', () => {
checkPropTypes({
it('should validate props', () => {
const errors = checkPropTypes({
notifications: [],
position: POSITIONS.topLeft,
defaultValues,
theme
}, NotificationsContainer.propTypes, true);
}, NotificationsContainer.propTypes);

expect(errors.notifications).toNotExist();
expect(errors.position).toNotExist();
expect(errors.defaultValues).toNotExist();
expect(errors.theme).toNotExist();
});

it('should not validate props', () => {
const errors = checkPropTypes({}, NotificationsContainer.propTypes);
expect(errors.notifications).toExist();
expect(errors.position).toExist();
expect(errors.defaultValues).toExist();
expect(errors.theme).toExist();
});

it('should mount with default props', () => {
Expand Down
19 changes: 15 additions & 4 deletions test/components/NotificationsSystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ describe('<NotificationsSystem/>', () => {
const otherProps = {
theme
};

it('should not throw error during propTypes validation', () => {
checkPropTypes({
it('should validate props', () => {
const errors = checkPropTypes({
notifications: [],
theme,
defaultValues
}, NotificationsSystem.propTypes, true);
}, NotificationsSystem.propTypes);

expect(errors.notifications).toNotExist();
expect(errors.theme).toNotExist();
expect(errors.defaultValues).toNotExist();
});

it('should not validate props', () => {
const errors = checkPropTypes({}, NotificationsSystem.propTypes);
expect(errors.notifications).toExist();
expect(errors.theme).toExist();
expect(errors.defaultValues).toExist();
});

it('should mount with default props', () => {
Expand Down
12 changes: 6 additions & 6 deletions test/utils/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ export function genNotification(notification = {}) {
* @returns {void}
*/
export function checkPropTypes(object, propTypes) {
let errors = {};
let propName;
// Check if object have the same property that propTypes (depth: 1 is enough)
// if you remove a propType validation inadvertently, it will throw an error
for (propName in object) {
if (!propTypes.hasOwnProperty(propName)) {
throw new Error(`${propName} prop is not validated by propTypes`);
errors[propName] = 'prop is not validated by propTypes';
}
}

// Check if object have the same property that propTypes object
// and if it have the correct type
for (propName in propTypes) {
if (propTypes.hasOwnProperty(propName) && (object.hasOwnProperty(propName))) {
if (propTypes.hasOwnProperty(propName)) {
let error = propTypes[propName](object, propName, JSON.stringify(object), 'prop');
if (error) {
throw error;
errors[propName] = error.message;
}
}
else {
throw new Error(`${propName} prop is required`);
}
}

return errors;
}

0 comments on commit 839baff

Please sign in to comment.