Skip to content
This repository was archived by the owner on Jun 19, 2018. It is now read-only.

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lewis committed Apr 12, 2015
1 parent 1eea230 commit 610b7ed
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 27 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ The calendar directive uses moment.js to produce all months and days of the week

tl;dr include the appropriate moment locale file (or all of them) and call ```moment.locale('YOUR_LOCALE_STRING')```.

To set Monday as the first day of the week configure it in moment like so:
```javascript
moment.locale('en', {
week : {
dow : 1 // Monday is the first day of the week
}
});
```

## Configuring date formats

You can easily customise the date formats and i18n strings used throughout the calendar by using the calendarConfigProvider. Please note that all formats are those used by moment.js. Example usage:
Expand Down
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ function runTests(action, onDistCode) {
} else {
var appJs = gulp.src('src/**/*.js').pipe($.sort()).pipe($.angularFilesort());
}
var templates = gulp.src('src/templates/*.html');
var karmaSetup = gulp.src('test/karma.setup.js');
var test = gulp.src('test/unit/**/*.js');

return series(vendorJs, appJs, karmaSetup, test)
return series(vendorJs, appJs, templates, karmaSetup, test)
.pipe($.karma({
configFile: 'karma.conf.js',
action: action
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ <h3 id="event-editor">
<script src="//cdn.jsdelivr.net/angular.bootstrap/0.12.1/ui-bootstrap-tpls.min.js"></script>
<script src="src/app.js"></script>
<script src="src/directives/mwlCalendar.js"></script>
<script src="src/filters/truncateEventTitle.js"></script>
<script src="src/filters/calendarTruncateEventTitle.js"></script>
<script src="src/filters/calendarLimitTo.js"></script>
<script src="src/services/calendarConfig.js"></script>
<script src="src/services/calendarTitle.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'./src/templates/**/*.html': ['ng-html2js']
'./src/templates/*.html': ['ng-html2js']
},

ngHtml2JsPreprocessor: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

angular
.module('mwl.calendar')
.filter('truncateEventTitle', function() {
.filter('calendarTruncateEventTitle', function() {

return function(string, length, boxHeight) {
if (!string) {
Expand Down
15 changes: 0 additions & 15 deletions src/filters/eventCountBadgeTotal.js

This file was deleted.

12 changes: 6 additions & 6 deletions src/services/calendarHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ angular
});
}

function getBadgeTotal(events) {
return events.filter(function(event) {
return event.incrementsBadgeTotal !== false;
}).length;
}

function getWeekDayNames() {
var weekdays = [];
var count = 0;
Expand All @@ -36,12 +42,6 @@ angular
return weekdays;
}

function getBadgeTotal(events) {
return events.filter(function(event) {
return event.incrementsBadgeTotal !== false;
}).length;
}

function getYearView(events, currentDay) {

var view = [];
Expand Down
2 changes: 1 addition & 1 deletion src/templates/calendarDayView.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

<!--<span class="cal-hours">{{ event.startsAt | date:'d MMM HH:mm' }} - {{ event.endsAt | date:'d MMM HH:mm' }}<br></span>-->
<a href="javascript:;" class="event-item" ng-click="onEventClick({calendarEvent: event})">
<span>{{ event.title | truncateEventTitle:20:event.height }}</span>
<span>{{ event.title | calendarTruncateEventTitle:20:event.height }}</span>
</a>

</div>
Expand Down
23 changes: 23 additions & 0 deletions test/unit/filters/calendarLimitTo.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
describe('calendarLimitToFilter', function() {

var limitToFilter;

beforeEach(inject(function(_calendarLimitToFilter_) {
limitToFilter = _calendarLimitToFilter_;
}));

it('should return the first 2 items of the array', function() {

var result = limitToFilter([1, 2, 3, 4], 2);
expect(result).to.eql([1, 2]);

});

it('should return the second 2 items of the array', function() {

var result = limitToFilter([1, 2, 3, 4], 2, 2);
expect(result).to.eql([3, 4]);

});

});
21 changes: 21 additions & 0 deletions test/unit/filters/calendarTruncateEventTitle.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe('calendarTruncateEventTitleFilter', function() {

var eventTitleFilter;

beforeEach(inject(function(_calendarTruncateEventTitleFilter_) {
eventTitleFilter = _calendarTruncateEventTitleFilter_;
}));

it('should return an empty string when passed a false value', function() {
expect(eventTitleFilter(null)).to.equal('');
});

it('should return the original string', function() {
expect(eventTitleFilter('test', 10, 100)).to.equal('test');
});

it('should return a truncated string for the first 5 characters', function() {
expect(eventTitleFilter('A really long string', 5, 10)).to.equal('A rea...');
});

});
37 changes: 37 additions & 0 deletions test/unit/services/calendarConfig.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe('calendarConfig', function() {

var calendarConfig;

beforeEach(module('mwl.calendar', function(calendarConfigProvider) {
calendarConfigProvider.setDateFormats({
hour: 'HA'
}).setTitleFormats({
month: 'MMMM'
}).setI18nStrings({
eventsLabel: 'Changed'
});
}));

beforeEach(inject(function(_calendarConfig_) {
calendarConfig = _calendarConfig_;
}));

describe('setDateFormats', function() {
it('should have changed the hour format', function() {
expect(calendarConfig.dateFormats.hour).to.equal('HA');
});
});

describe('setTitleFormats', function() {
it('should have changed the title format', function() {
expect(calendarConfig.titleFormats.month).to.equal('MMMM');
});
});

describe('setI18nStrings', function() {
it('should have changed the i18n strings', function() {
expect(calendarConfig.i18nStrings.eventsLabel).to.equal('Changed');
});
});

});
20 changes: 20 additions & 0 deletions test/unit/services/calendarDebounce.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe('calendarDebounce', function() {

var calendarDebounce, $timeout;

beforeEach(inject(function(_calendarDebounce_, _$timeout_) {
calendarDebounce = _calendarDebounce_;
$timeout = _$timeout_;
}));

it('should only be called once', function() {
var spy = sinon.spy();
var myDebounce = calendarDebounce(spy, 10);
myDebounce();
myDebounce();
myDebounce();
$timeout.flush();
expect(spy).to.have.been.called.once;
});

});
60 changes: 59 additions & 1 deletion test/unit/services/calendarHelper.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,74 @@
describe('calendarHelper', function() {

var calendarHelper;
var calendarHelper, events;

beforeEach(inject(function(_calendarHelper_) {
calendarHelper = _calendarHelper_;

events = [{
title: 'My event title',
type: 'info',
startsAt: new Date(2013,5,1,1),
endsAt: new Date(2014,8,26,15),
incrementsBadgeTotal: true
}, {
title: 'My event title',
type: 'info',
startsAt: new Date(2013,5,1,1),
endsAt: new Date(2014,8,26,15),
incrementsBadgeTotal: false
}];

}));

describe('getWeekDayNames', function() {

it('should get the days of the week starting at sunday', function() {
var weekdays = calendarHelper.getWeekDayNames();
expect(weekdays).to.eql(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']);
});

it('should get the days of the week starting at monday', inject(function(moment) {
moment.locale('en', {
week : {
dow : 1 // Monday is the first day of the week
}
});

var weekdays = calendarHelper.getWeekDayNames();
expect(weekdays).to.eql(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']);

moment.locale('en', {
week : {
dow : 0 // Sunday is the first day of the week
}
});
}));

});

describe('getYearView', function() {

//TODO

});

describe('getMonthView', function() {

//TODO

});

describe('getWeekView', function() {

//TODO

});

describe('getDayView', function() {

//TODO

});

});
26 changes: 26 additions & 0 deletions test/unit/services/calendarTitle.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe('calendarConfig', function() {

var calendarTitle;
var calendarDay = new Date(2015, 04, 01);

beforeEach(inject(function(_calendarTitle_) {
calendarTitle = _calendarTitle_;
}));

it('should give the correct day title', function() {
expect(calendarTitle.day(calendarDay)).to.equal('Friday 1 May, 2015');
});

it('should give the correct week title', function() {
expect(calendarTitle.week(calendarDay)).to.equal('Week 18 of 2015');
});

it('should give the correct month title', function() {
expect(calendarTitle.month(calendarDay)).to.equal('May 2015');
});

it('should give the correct year title', function() {
expect(calendarTitle.year(calendarDay)).to.equal('2015');
});

});
9 changes: 9 additions & 0 deletions test/unit/services/moment.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('moment', function() {

it('should be the window moment object', inject(function($window, moment) {

expect(moment).to.eql($window.moment);

}));

});

0 comments on commit 610b7ed

Please sign in to comment.