Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Improves coverage of the componentWillReceiveProps
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Machado committed Mar 20, 2017
1 parent deb0cf6 commit ac0dcde
Show file tree
Hide file tree
Showing 4 changed files with 1,436 additions and 243 deletions.
24 changes: 22 additions & 2 deletions components/calendar/calendar.js
Expand Up @@ -23,7 +23,9 @@ const {
function processProps(props) {
const { initialDates, maxDate, minDate, selectionType } = props;
const maxLimit = maxDate ? normalizeDate(new Date(maxDate), 23, 59, 59, 999) : null;
const renderDate = normalizeDate(((initialDates && initialDates.length) ? new Date(initialDates[0]) : new Date()));
const renderDate = normalizeDate(((initialDates && initialDates.length && initialDates[0])
? new Date(initialDates[0])
: new Date()));

let minLimit = minDate ? normalizeDate(new Date(minDate)) : null;
let selectedDates = [null, null];
Expand Down Expand Up @@ -78,7 +80,25 @@ export default class Calendar extends Component {
}

componentWillReceiveProps(newProps) {
this.setState(() => processProps(newProps));
const { initialDates, maxDate, minDate, selectionType } = newProps;

let propsChanged = (
(maxDate !== this.props.maxDate) ||
(minDate !== this.props.minDate) ||
(selectionType !== this.props.selectionType)
);

if (initialDates) {
if (this.props.initialDates) {
propsChanged = propsChanged || initialDates.some((item, idx) => item !== this.props.initialDates[idx]);
} else {
propsChanged = true;
}
}

if (propsChanged) {
this.setState(() => processProps(newProps));
}
}

/**
Expand Down

0 comments on commit ac0dcde

Please sign in to comment.