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

fix(datepicker): week count issues #2306

Closed
wants to merge 5 commits 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
5 changes: 3 additions & 2 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst

if ( scope.showWeeks ) {
scope.weekNumbers = [];
var weekNumber = getISO8601WeekNumber( scope.rows[0][0].date ),
var thursdayIndex = (4 + 7 - ctrl.startingDay) % 7,
curWeek = 0,
numWeeks = scope.rows.length;
while( scope.weekNumbers.push(weekNumber++) < numWeeks ) {}
while( curWeek < numWeeks ) {scope.weekNumbers.push(getISO8601WeekNumber( scope.rows[curWeek++][thursdayIndex].date ));}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably better re-written as a for loop. It was previously simpler as a while loop but now it makes more sense as a for loop as it's more readable.

}
};

Expand Down
73 changes: 72 additions & 1 deletion src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('datepicker directive', function () {
});

it('renders the week numbers based on ISO 8601', function() {
expect(getWeeks()).toEqual(['34', '35', '36', '37', '38', '39']);
expect(getWeeks()).toEqual(['35', '36', '37', '38', '39', '40']);
});

it('value is correct', function() {
Expand Down Expand Up @@ -1711,4 +1711,75 @@ describe('datepicker directive', function () {
expect(getTitle()).toBe('2013');
});
});

describe('thurdays determine week count', function() {

beforeEach(inject(function() {
$rootScope.date = new Date('June 07, 2014');
}));

it('with the default starting day (sunday)', function() {
element = $compile('<datepicker ng-model="date"></datepicker>')($rootScope);
$rootScope.$digest();

expect(getWeeks()).toEqual(['23', '24', '25', '26', '27', '28']);
});

describe('when starting date', function() {
it('is monday', function() {
element = $compile('<datepicker ng-model="date" starting-day="1"></datepicker>')($rootScope);
$rootScope.$digest();

expect(getWeeks()).toEqual(['22', '23', '24', '25', '26', '27']);
});

it('is thursday', function() {
element = $compile('<datepicker ng-model="date" starting-day="4"></datepicker>')($rootScope);
$rootScope.$digest();

expect(getWeeks()).toEqual(['22', '23', '24', '25', '26', '27']);
});

it('is saturday', function() {
element = $compile('<datepicker ng-model="date" starting-day="6"></datepicker>')($rootScope);
$rootScope.$digest();

expect(getWeeks()).toEqual(['23', '24', '25', '26', '27', '28']);
});
});

describe('first week in january', function() {
beforeEach(inject(function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty beforeEach can be removed

}));

it('in current year', function() {
$rootScope.date = new Date('January 07, 2014');
element = $compile('<datepicker ng-model="date"></datepicker>')($rootScope);
$rootScope.$digest();

expect(getWeeks()).toEqual(['1', '2', '3', '4', '5', '6']);
});

it('in last year', function() {
$rootScope.date = new Date('January 07, 2010');
element = $compile('<datepicker ng-model="date"></datepicker>')($rootScope);
$rootScope.$digest();

expect(getWeeks()).toEqual(['53', '1', '2', '3', '4', '5']);
});
});

describe('last week(s) in december', function() {
beforeEach(inject(function() {
$rootScope.date = new Date('December 07, 2014');
}));

it('in next year', function() {
element = $compile('<datepicker ng-model="date"></datepicker>')($rootScope);
$rootScope.$digest();

expect(getWeeks()).toEqual(['49', '50', '51', '52', '1', '2']);
});
});
});
});