Skip to content

Commit

Permalink
fix(MinutePicker): getInitialDatePosition handles disabled positions
Browse files Browse the repository at this point in the history
  • Loading branch information
arfedulov committed Dec 31, 2018
1 parent bab7718 commit c1ad726
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"scripts": {
"test": "npx cross-env TS_NODE_COMPILER_OPTIONS=\"{\"\"allowJs\"\":true}\" npx mocha -r ts-node/register ./test/setup.js ./test/**/*.{js,jsx,ts,tsx}",
"start": "npx webpack-dev-server",
"prebuild": "npm run lint && npx rimraf dist/*",
"prebuild": "npm run test && npm run lint && npx rimraf dist/*",
"build": "npx tsc --declaration",
"publish-npm": "npm run test && npm run build && npm publish",
"build:example": "npx webpack --production",
"build:example": "npm run test && npm run lint && npx webpack --production",
"lint": "npx tslint src/**"
},
"keywords": [
Expand Down
14 changes: 12 additions & 2 deletions src/pickers/timePicker/MinutePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,24 @@ class MinutePicker
}

protected getSelectableCellPositions(): number[] {
return _.range(0, MINUTES_ON_PAGE);
const disabled = this.getDisabledPositions();
const all = _.range(0, MINUTES_ON_PAGE);
if (disabled) {
return all.filter((pos) => {
return disabled.indexOf(pos) < 0;
});
}

return all;
}

protected getInitialDatePosition(): number {
const selectable = this.getSelectableCellPositions();
if (selectable.indexOf(this.state.date.hour()) < 0) {
if (selectable.indexOf(getMinuteCellPosition(this.state.date.minute())) < 0) {
return selectable[0];
}

return getMinuteCellPosition(this.state.date.minute());
}

protected getDisabledPositions(): number[] {
Expand Down
33 changes: 25 additions & 8 deletions test/pickers/timePicker/testMinutePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('<MinutePicker />: isNextPageAvailable', () => {
const wrapper = mount(<MinutePicker
maxDate={moment('2018-08-12')}
initializeWith={date} />);

assert(_.isBoolean(wrapper.instance().isNextPageAvailable()), 'return boolean');
assert.isFalse(wrapper.instance().isNextPageAvailable(), 'return false');
});
Expand All @@ -150,7 +150,7 @@ describe('<MinutePicker />: isNextPageAvailable', () => {
const wrapper = mount(<MinutePicker
maxDate={moment('2018-08-13')}
initializeWith={date} />);

assert(_.isBoolean(wrapper.instance().isNextPageAvailable()), 'return boolean');
assert.isTrue(wrapper.instance().isNextPageAvailable(), 'return true');
});
Expand All @@ -165,7 +165,7 @@ describe('<MinutePicker />: isPrevPageAvailable', () => {
const wrapper = mount(<MinutePicker
minDate={moment('2018-08-12')}
initializeWith={date} />);

assert(_.isBoolean(wrapper.instance().isPrevPageAvailable()), 'return boolean');
assert.isFalse(wrapper.instance().isPrevPageAvailable(), 'return false');
});
Expand All @@ -176,7 +176,7 @@ describe('<MinutePicker />: isPrevPageAvailable', () => {
const wrapper = mount(<MinutePicker
minDate={moment('2018-07-11')}
initializeWith={date} />);

assert(_.isBoolean(wrapper.instance().isPrevPageAvailable()), 'return boolean');
assert.isTrue(wrapper.instance().isPrevPageAvailable(), 'return true');
});
Expand All @@ -189,7 +189,7 @@ describe('<MinutePicker />: getCurrentDate', () => {
it('return string in format `MMMM DD, YYYY`', () => {
const wrapper = mount(<MinutePicker
initializeWith={date} />);

assert(_.isString(wrapper.instance().getCurrentDate()), 'return string');
assert.equal(wrapper.instance().getCurrentDate(), date.format('MMMM DD, YYYY'), 'return proper value');
});
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('<MinutePicker />: switchToNextPage', () => {
it('shift `date` state field one day forward', () => {
const wrapper = mount(<MinutePicker
initializeWith={date} />);

assert.equal(wrapper.state('date').date(), 12, 'date not changed yet');
wrapper.instance().switchToNextPage();
assert.equal(wrapper.state('date').date(), 12 + 1, 'date shifted one day forward');
Expand All @@ -242,9 +242,26 @@ describe('<MinutePicker />: switchToPrevPage', () => {
it('shift `date` state field one day backward', () => {
const wrapper = mount(<MinutePicker
initializeWith={date} />);

assert.equal(wrapper.state('date').date(), 12, 'date not changed yet');
wrapper.instance().switchToPrevPage();
assert.equal(wrapper.state('date').date(), 12 - 1, 'date shifted one day backward');
});
});
});

describe('<MinutePicker />: getSelectableCellPositions', () => {
const date = moment('2018-08-12 10:00');

it('return minutes positions that are >= `minDate`', () => {
const wrapper = mount(<MinutePicker
minDate={moment('2018-08-12 10:15')}
initializeWith={date} />);
const expected = [ 3, 4, 5, 6, 7, 8, 9, 10, 11 ];
const actual = wrapper.instance().getSelectableCellPositions();

assert.equal(actual.length, expected.length);
expected.forEach((expectPos, i) => {
assert.equal(expectPos, actual[i]);
});
});
});

0 comments on commit c1ad726

Please sign in to comment.