Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

fix(datepicker): correctly change months #1701

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.position'])
};
scope.move = function(direction) {
var step = datepickerCtrl.modes[mode].step;
selected.setDate(1); // Avoid having a day that doesn't exist on the objective month.
selected.setMonth( selected.getMonth() + direction * (step.months || 0) );
selected.setFullYear( selected.getFullYear() + direction * (step.years || 0) );
refill();
Expand Down
38 changes: 38 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,44 @@ describe('datepicker directive', function () {
expectSelectedElement( 0, 4 );
});

it('issue 1697 - moves to the next month & render correctly when the selected day doesnt exist on the next month', function() {
$rootScope.date = new Date('January 31, 2010 15:30:00');
$rootScope.$digest();

clickNextButton();

expect(getTitle()).toBe('February 2010');
expect(getLabels()).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']);
expect(getOptions()).toEqual([
['31', '01', '02', '03', '04', '05', '06'],
['07', '08', '09', '10', '11', '12', '13'],
['14', '15', '16', '17', '18', '19', '20'],
['21', '22', '23', '24', '25', '26', '27'],
['28', '01', '02', '03', '04', '05', '06']
]);

expectSelectedElement( 0, 0 );
});

it('issue 1697 - moves to the previous month & render correctly when the selected day doesnt exist on the previous month', function() {
$rootScope.date = new Date('March 31, 2010 15:30:00');
$rootScope.$digest();

clickPreviousButton();

expect(getTitle()).toBe('February 2010');
expect(getLabels()).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']);
expect(getOptions()).toEqual([
['31', '01', '02', '03', '04', '05', '06'],
['07', '08', '09', '10', '11', '12', '13'],
['14', '15', '16', '17', '18', '19', '20'],
['21', '22', '23', '24', '25', '26', '27'],
['28', '01', '02', '03', '04', '05', '06']
]);

expectSelectedElement( null, null );
});

it('updates the model only when a day is clicked in the `next` month', function() {
clickNextButton();
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
Expand Down