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

fix(datepicker): Today button should not set time #1808

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
11 changes: 10 additions & 1 deletion src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,16 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
});

scope.select = function( date ) {
scope.dateSelection( date === 'today' ? new Date() : date);
if (date === 'today') {
var today = new Date();
if (angular.isDate(ngModel.$modelValue)) {
date = new Date(ngModel.$modelValue);
date.setFullYear(today.getFullYear(), today.getMonth(), today.getDate());
} else {
date = new Date(today.setHours(0, 0, 0, 0));
}
}
scope.dateSelection( date );
};

var $popup = $compile(popupEl)(scope);
Expand Down
29 changes: 28 additions & 1 deletion src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,41 @@ describe('datepicker directive', function () {
expect(dropdownEl.find('li').length).toBe(2);
});

it('should have four buttons', function() {
it('should have three buttons', function() {
expect(buttons.length).toBe(3);

expect(buttons.eq(0).text()).toBe('Today');
expect(buttons.eq(1).text()).toBe('Clear');
expect(buttons.eq(2).text()).toBe('Done');
});

it('should have a button to set today date without altering time part', function() {
var today = new Date();
buttons.eq(0).click();
expect($rootScope.date.getFullYear()).toBe(today.getFullYear());
expect($rootScope.date.getMonth()).toBe(today.getMonth());
expect($rootScope.date.getDate()).toBe(today.getDate());

expect($rootScope.date.getHours()).toBe(15);
expect($rootScope.date.getMinutes()).toBe(30);
expect($rootScope.date.getSeconds()).toBe(0);
});

it('should have a button to set today date if blank', function() {
$rootScope.date = null;
$rootScope.$digest();

var today = new Date();
buttons.eq(0).click();
expect($rootScope.date.getFullYear()).toBe(today.getFullYear());
expect($rootScope.date.getMonth()).toBe(today.getMonth());
expect($rootScope.date.getDate()).toBe(today.getDate());

expect($rootScope.date.getHours()).toBe(0);
expect($rootScope.date.getMinutes()).toBe(0);
expect($rootScope.date.getSeconds()).toBe(0);
});

it('should have a button to clear value', function() {
buttons.eq(1).click();
expect($rootScope.date).toBe(null);
Expand Down