Create a gist now

Instantly share code, notes, and snippets.

Specs for date utils
requireModules(['common.date_utils'], function (u) {
describe('Moment', function () { // returns a new Moment
it('should create a representation of a fixed date and time', function () {
var moment = new u.Moment(1347915062072);
expect(typeof moment.add).toBe('function');
});
it('should create a Moment from nothing', function () {
var moment = new u.Moment();
expect(typeof moment.add).toBe('function');
});
it('should create a Moment from a JS Date', function () {
var d = new Date();
var moment = new u.Moment(d);
expect(typeof moment.add).toBe('function');
});
it('should create a Moment from a string parsable into a JS date', function () {
var moment = new u.Moment('Fri, 12 Oct 2012 19:15:56 UTC');
expect(typeof moment.add).toBe('function');
});
it('should allow moments to be cloned', function () {
var moment1 = new u.Moment(1347915062072);
var moment2 = moment1.clone();
expect(moment1).not.toBe(moment2);
expect(moment1.getDate()).toEqual(moment2.getDate());
});
it('should create a moment from an ISO-8601 subset string', function () {
var moment1 = new u.Moment('1978-06-14');
var moment2 = new u.Moment('1978-06-14T06:14:00');
var moment3 = new u.Moment('1978-06-14T06:14:00-05:00');
expect(moment1.getDate()).toBe(14);
expect(moment2.getDate()).toBe(14);
expect(moment2.getMinutes()).toBe(14);
expect(moment2.getYear()).toBe(1978);
// expect(moment3.getHours()).toBe(7); Add for your timezone, disabling because this won't always be run in NYC
});
});
describe('Moment toDate', function () {
it('should return a JS date object for a moment', function () {
var stamp = 1347915062072;
var moment = new u.Moment(stamp);
var date = moment.toDate();
expect(typeof date.constructor).toBe('function'); // Is this because of Rhino, or does the method not work?
expect(moment.toDate().valueOf()).toBe(1347915062072);
});
});
describe('Moment setToStartOf', function () { // returns the same Moment???
it('should set the start of a moment by hour', function () {
var moment = new u.Moment(1347915062072);
moment.setToStartOf('hour');
expect(moment.getSeconds()).toBe(0);
expect(moment.getMinutes()).toBe(0);
expect(moment.getHours()).toBe(16);
});
it('should set the start of a moment by day', function () {
var moment = new u.Moment(1347915062072);
moment.setToStartOf('day');
expect(moment.getSeconds()).toBe(0);
expect(moment.getHours()).toBe(0);
expect(moment.getYear()).toBe(2012);
});
it('should set the start of a moment by month', function () {
var moment = new u.Moment(1347915062072);
moment.setToStartOf('month');
expect(moment.getSeconds()).toBe(0);
expect(moment.getHours()).toBe(0);
expect(moment.getMonth()).toBe(8);
expect(moment.getYear()).toBe(2012);
});
it('should set the start of a moment by year', function () {
var moment = new u.Moment(1347915062072);
moment.setToStartOf('year');
expect(moment.getSeconds()).toBe(0);
expect(moment.getHours()).toBe(0);
expect(moment.getMonth()).toBe(0);
expect(moment.getYear()).toBe(2012);
});
});
describe('Moment setToEndOf', function () { // returns the same Moment???
it('should set the end of a moment by hour', function () {
var moment = new u.Moment(1347915062072);
moment.setToEndOf('hour');
expect(moment.getSeconds()).toBe(59);
expect(moment.getMinutes()).toBe(59);
expect(moment.getHours()).toBe(16);
});
it('should set the end of a moment by day', function () {
var moment = new u.Moment(1347915062072);
moment.setToEndOf('day');
expect(moment.getSeconds()).toBe(59);
expect(moment.getMinutes()).toBe(59);
expect(moment.getHours()).toBe(23);
});
it('should set the end of a moment by month', function () {
var moment = new u.Moment(1347915062072);
moment.setToEndOf('month');
expect(moment.getSeconds()).toBe(59);
expect(moment.getMinutes()).toBe(59);
expect(moment.getHours()).toBe(23);
expect(moment.getDate()).toBe(30);
var moment2 = new u.Moment(1328903062072);
moment2.setToEndOf('month');
expect(moment2.getDate()).toBe(29);
});
it('should set the end of a moment by year', function () {
var moment = new u.Moment(1347915062072);
moment.setToEndOf('year');
expect(moment.getSeconds()).toBe(59);
expect(moment.getMinutes()).toBe(59);
expect(moment.getHours()).toBe(23);
expect(moment.getDate()).toBe(31);
expect(moment.getMonth()).toBe(11);
});
});
describe('Moment set functions', function () { // TODO: How to handle +- conditions as well as absolute conditions? '+1'? // returns the same Moment
it('should allow a moment to be set using a Date object', function () {
var d = new Date(1347915062072);
var moment = new u.Moment(d);
expect(moment.getYear()).toBe(2012);
});
it('should allow a moment to be set by year', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getYear()).toBe(2012);
moment.setYear(2013);
expect(moment.getYear()).toBe(2013);
});
it('should allow a moment to be set by month', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMonth()).toBe(8);
moment.setMonth(9);
expect(moment.getMonth()).toBe(9);
});
it('should allow a moment to be set by date', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getDate()).toBe(17);
moment.setDate(20);
expect(moment.getDate()).toBe(20);
});
it('should allow a moment to be set by hour', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getHours()).toBe(16);
moment.setHours(15);
expect(moment.getHours()).toBe(15);
});
it('should allow a moment to be set by minute', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMinutes()).toBe(51);
moment.setMinutes(23);
expect(moment.getMinutes()).toBe(23);
});
it('should allow a moment to be set by second', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getSeconds()).toBe(2);
moment.setSeconds(42);
expect(moment.getSeconds()).toBe(42);
});
it('should allow a moment to be set by millisecond', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMilliseconds()).toBe(72);
moment.setMilliseconds(123);
expect(moment.getMilliseconds()).toBe(123);
});
});
describe('Moment get functions', function () { // returns values of a Moment
it('should return the value of the moment milliseconds', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMilliseconds()).toBe(72);
});
it('should return the value of the moment seconds', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getSeconds()).toBe(2);
});
it('should return the value of the moment minutes', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMinutes()).toBe(51);
});
it('should return the value of the moment hours', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getHours()).toBe(16);
});
it('should return the value of the moment day', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getDay()).toBe(1);
});
it('should return the value of the moment date', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getDate()).toBe(17);
});
it('should return the value of the moment month', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMonth()).toBe(8);
moment.setMonth(0);
expect(moment.getMonth()).toBe(0);
});
it('should return the value of the moment year', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getYear()).toBe(2012);
});
});
// Assumes being run in NYC for now
describe('Moment get functions in UTC', function () { // returns values of a Moment
it('should return the value of the moment milliseconds in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMilliseconds({ utc : true })).toBe(72);
});
it('should return the value of the moment seconds in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getSeconds({ utc : true })).toBe(2);
});
it('should return the value of the moment minutes in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMinutes({ utc : true })).toBe(51);
});
it('should return the value of the moment hours in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getHours({ utc : true })).toBe(20);
});
it('should return the value of the moment day in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getDay({ utc : true })).toBe(1);
});
it('should return the value of the moment date in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getDate({ utc : true })).toBe(17);
});
it('should return the value of the moment month in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getMonth({ utc : true })).toBe(8);
var moment2 = new u.Moment(1330573123213);
expect(moment2.getMonth()).toBe(1);
expect(moment2.getMonth({ utc : true })).toBe(2);
});
it('should return the value of the moment year in UTC', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getYear({ utc : true })).toBe(2012);
var moment2 = new u.Moment(1357015023213);
expect(moment2.getYear()).toBe(2012);
expect(moment2.getYear({utc : true })).toBe(2013);
});
});
describe('Moment isLeapYear', function () { // returns Boolean
it('should return whether the given moment is in a leap year', function () { // with/without UTC?
var moment = new u.Moment();
moment.setYear(2012);
expect(moment.isLeapYear()).toBe(true);
moment.setYear(2013);
expect(moment.isLeapYear()).toBe(false);
});
});
describe('Moment isDst', function () { // returns Boolean
it('should return whether the given moment is in DST', function () {
var moment = new u.Moment(1347915062072);
expect(moment.isDst()).toBe(true);
moment.setMonth(11);
expect(moment.isDst()).toBe(false);
});
});
describe('Moment getTimezoneOffset', function () { // returns Number? (min vs. hours?)
// Disabling because this won't always be run in NYC
xit('should return the timezone offset for the given moment', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getTimezoneOffset()).toBe(240);
});
});
describe('Moment getDaysInMonth', function () { // returns Number
it('should return the number of days in the month of the given moment', function () {
var moment = new u.Moment();
moment.setMonth(8);
expect(moment.getDaysInMonth()).toBe(30);
moment.setMonth(1);
expect(moment.getDaysInMonth()).toBe(29);
moment.setMonth(11);
expect(moment.getDaysInMonth()).toBe(31);
});
});
describe('Moment chaining', function () {
it('should allow setters to be chained', function () {
var moment = new u.Moment();
moment.setMonth(2).setYear(2008).setMinutes(23).setToEndOf('hour');
expect(moment.getMinutes()).toBe(59);
expect(moment.getYear()).toBe(2008);
expect(moment.getMonth()).toBe(2);
});
});
describe('Duration', function () { // returns a new Duration
it('should create a representation of a duration of time', function () {
var duration = new u.Duration(10000);
expect(typeof duration._duration.humanize).toBe('function');
});
it('should allow a duration to be cloned', function () {
var duration1 = new u.Duration(10000);
var duration2 = duration1.clone();
expect(duration1).not.toBe(duration2);
expect(duration1.getAsMilliseconds()).toBe(duration2.getAsMilliseconds());
});
it('should create a duration from an ISO-8601 string with time and period', function () {
var duration1 = new u.Duration('P1Y3M20DT07H23M30S');
var duration2 = new u.Duration('P3DT12M34S');
expect(duration1.getMinutes()).toBe(23);
expect(duration1.getAsMinutes()).toBe(677243.5);
expect(duration2.getMinutes()).toBe(12);
expect(duration2.getSeconds()).toBe(34);
expect(duration2.getDays()).toBe(3);
});
it('should create a duration from an ISO-8601 string with time and period and extra spaces', function () {
var duration1 = new u.Duration('P 1Y 3M 20D T 7H 23M 30S');
expect(duration1.getMinutes()).toBe(23);
expect(duration1.getAsMinutes()).toBe(677243.5);
});
it('should create a duration from an ISO-8601 string with time only', function () {
var duration1 = new u.Duration('PT22H13M45S');
var duration2 = new u.Duration('PT34M');
expect(duration1.getMinutes()).toBe(13);
expect(duration1.getAsMinutes()).toBe(1333.75);
expect(duration2.getMinutes()).toBe(34);
expect(duration2.getHours()).toBe(0);
expect(duration2.getAsMinutes()).toBe(34);
});
it('should create a duration from an ISO-8601 string with period only', function () {
var duration1 = new u.Duration('P2Y10M28D');
var duration2 = new u.Duration('P3M10D');
expect(duration1.getMinutes()).toBe(0);
expect(duration1.getMonths()).toBe(10);
expect(duration1.getDays()).toBe(28);
expect(duration1.getAsDays()).toBe(1048);
expect(duration2.getDays()).toBe(10);
expect(duration2.getMonths()).toBe(3);
expect(duration2.getAsDays()).toBe(100);
});
});
describe('Duration get functions', function () { // returns values of a Duration
it('should get the value of the duration milliseconds', function () {
var duration = new u.Duration(600800);
expect(duration.getMilliseconds()).toBe(800);
});
it('should get the value of the duration seconds', function () {
var duration = new u.Duration(601800);
expect(duration.getSeconds()).toBe(1);
var duration2 = new u.Duration(1000 * 67);
expect(duration2.getSeconds()).toBe(7);
});
it('should get the value of the duration minutes', function () {
var duration = new u.Duration(1000 * 60 * 10);
expect(duration.getMinutes()).toBe(10);
var duration2 = new u.Duration(1000 * 60 * 65);
expect(duration2.getMinutes()).toBe(5);
});
it('should get the value of the duration hours', function () {
var duration = new u.Duration(1000 * 60 * 60);
expect(duration.getHours()).toBe(1);
var duration2 = new u.Duration(1000 * 60 * 125);
expect(duration2.getHours()).toBe(2);
});
it('should get the value of the duration days', function () {
var duration = new u.Duration(1000 * 60 * 60 * 24);
expect(duration.getDays()).toBe(1);
var duration2 = new u.Duration(1000 * 60 * 60 * 100);
expect(duration2.getDays()).toBe(4);
});
it('should get the value of the duration months', function () {
var duration = new u.Duration(1000 * 60 * 60 * 24 * 30);
expect(duration.getMonths()).toBe(1);
var duration2 = new u.Duration(1000 * 60 * 60 * 24 * 75);
expect(duration2.getMonths()).toBe(2);
});
it('should get the value of the duration years', function () {
var duration = new u.Duration(1000 * 60 * 60 * 24 * 366);
expect(duration.getYears()).toBe(1);
var duration2 = new u.Duration(1000 * 60 * 60 * 24 * 800);
expect(duration2.getYears()).toBe(2);
});
});
describe('Duration get as functions', function () { // returns values of a Duration
it('should get the value of the duration as milliseconds', function () {
var duration = new u.Duration(2200);
expect(duration.getAsMilliseconds()).toBe(2200);
});
it('should get the value of the duration as seconds', function () {
var duration = new u.Duration(2200);
expect(duration.getAsSeconds()).toBe(2.2);
});
it('should get the value of the duration as minutes', function () {
var duration = new u.Duration(1000 * 60 * 5);
expect(duration.getAsMinutes()).toBe(5);
var duration2 = new u.Duration(1000 * 60 * 5.4);
expect(duration2.getAsMinutes()).toBe(5.4);
});
it('should get the value of the duration as hours', function () {
var duration = new u.Duration(1000 * 60 * 90);
expect(duration.getAsHours()).toBe(1.5);
});
it('should get the value of the duration as days', function () {
var duration = new u.Duration(1000 * 60 * 60 * 24);
expect(duration.getAsDays()).toBe(1);
var duration2 = new u.Duration(1000 * 60 * 60 * 51);
expect(duration2.getAsDays()).toBe(2.125);
});
it('should get the value of the duration months', function () {
var duration = new u.Duration(1000 * 60 * 60 * 24 * 30);
expect(duration.getAsMonths()).toBe(1);
var duration2 = new u.Duration(1000 * 60 * 60 * 24 * 45);
expect(duration2.getAsMonths()).toBe(1.5);
});
it('should get the value of the duration as years', function () {
var duration = new u.Duration(1000 * 60 * 60 * 24 * 365);
expect(duration.getAsYears()).toBe(1);
var duration2 = new u.Duration(1000 * 60 * 60 * 24 * 438);
expect(duration2.getAsYears()).toBe(1.2);
});
});
describe('addDurations', function () { // returns a new Duration
it('should add two Durations, returning a new Duration', function () {
var duration1 = new u.Duration(10000);
var duration2 = new u.Duration(20000);
var duration3 = new u.Duration(30000);
expect(u.addDurations(duration1, duration2)).toEqual(duration3);
});
it('should allow argument overloading', function () {
var duration = new u.Duration(2500);
expect(u.addDurations(1000, 1500)).toEqual(duration);
expect(u.addDurations(new u.Duration(1000), 1500)).toEqual(duration);
expect(u.addDurations(1000, new u.Duration(1500))).toEqual(duration);
});
});
describe('subtractDurations', function () { // returns a new Duration
it('should subtract two Durations, returning a new Duration which may be negative', function () {
var duration1 = new u.Duration(30000);
var duration2 = new u.Duration(10000);
var duration3 = new u.Duration(20000);
expect(u.subtractDurations(duration1, duration2)).toEqual(duration3);
var duration4 = new u.Duration(10000);
var duration5 = new u.Duration(30000);
var duration6 = new u.Duration(-20000);
expect(u.subtractDurations(duration4, duration5)).toEqual(duration6);
});
it('should allow argument overloading', function () {
var duration = new u.Duration(2500);
expect(u.subtractDurations(4000, 1500)).toEqual(duration);
expect(u.subtractDurations(new u.Duration(4000), 1500)).toEqual(duration);
expect(u.subtractDurations(4000, new u.Duration(1500))).toEqual(duration);
});
});
describe('diffDurations', function () { // returns a new Duration
it('should find the difference between two durations, as an absolute number Duration', function () {
var duration1 = new u.Duration(35000);
var duration2 = new u.Duration(-30000);
var duration3 = new u.Duration(65000);
expect(u.diffDurations(duration1, duration2)).toEqual(duration3);
expect(u.diffDurations(duration2, duration1)).toEqual(duration3);
var duration4 = new u.Duration(70000);
var duration5 = new u.Duration(80000);
var duration6 = new u.Duration(10000);
expect(u.diffDurations(duration4, duration5)).toEqual(duration6);
expect(u.diffDurations(duration5, duration4)).toEqual(duration6);
});
it('should allow argument overloading', function () {
var diff = new u.Duration(5000);
expect(u.diffDurations(4000, 'PT 9S')).toEqual(diff);
expect(u.diffDurations(new u.Duration(4000), -1000)).toEqual(diff);
});
});
describe('addToMoment', function () { // returns a new Moment
it('should add a duration to a moment, returning a new Moment', function () {
var moment1 = new u.Moment(1347915062072);
var duration1 = new u.Duration(1000 * 60 * 60 * 24);
expect(u.addToMoment(moment1, duration1).getDate()).toBe(18);
expect(u.addToMoment(moment1, duration1).getDate()).toBe(18); // two times to ensure it's not mutating the original
var moment2 = new u.Moment(1347915062072);
var duration2 = new u.Duration(-1000 * 60 * 60 * 24);
expect(u.addToMoment(moment2, duration2).getDate()).toBe(16);
});
it('should allow argument overloading', function () {
expect(u.addToMoment(1347915062072, 1000 * 60 * 60 * 24).getDate()).toBe(18);
});
});
describe('subtractFromMoment', function () { // returns a new Moment
it('should subtract a duration from a moment, returning a new Moment', function () {
var moment1 = new u.Moment(1347915062072);
var duration1 = new u.Duration(1000 * 60 * 60 * 24);
expect(u.subtractFromMoment(moment1, duration1).getDate()).toBe(16);
expect(u.subtractFromMoment(moment1, duration1).getDate()).toBe(16); // two times to ensure it's not mutating the original
var moment2 = new u.Moment(1347915062072);
var duration2 = new u.Duration(-1000 * 60 * 60 * 24);
expect(u.subtractFromMoment(moment2, duration2).getDate()).toBe(18);
});
it('should allow argument overloading', function () {
expect(u.subtractFromMoment(1347915062072, 1000 * 60 * 60 * 24).getDate()).toBe(16);
});
});
describe('diffMoments', function () { // returns a new Duration
it('should find the difference between two moments, expressed as a Duration', function () {
var moment1 = new u.Moment(1347915062072);
var moment2 = new u.Moment(1348001462073);
var duration1 = new u.Duration((-1000 * 60 * 60 * 24) - 1);
expect(u.diffMoments(moment1, moment2)).toEqual(duration1);
});
// TODO: Because of moment.js, the commented out test is failing; internal objects aren't equal
it('should allow argument overloading', function () {
var duration1 = new u.Duration('P3D');
//expect(u.diffMoments('2012-01-04', '2012-01-01')).toEqual(duration1);
expect(u.diffMoments('2012-01-01', '2012-01-04').getAsDays()).toEqual(-3);
expect(u.diffMoments('2012-01-04', '2012-01-01').getAsDays()).toEqual(duration1.getAsDays());
expect(u.diffMoments('2012-01-04', '2012-01-01').getAsMilliseconds()).toEqual(duration1.getAsMilliseconds());
expect(u.diffMoments('2012-01-04', '2012-01-01').getMilliseconds()).toEqual(duration1.getMilliseconds());
});
});
describe('Moment add', function () { // returns the original Moment (m.add(Duration))
it('should add a Duration to a Moment, returning the same Moment instance', function () {
var moment = new u.Moment(1347915062072);
var duration = new u.Duration(10);
expect(moment.add(duration).toDate().valueOf()).toBe(1347915062082);
});
it('should return the same Moment instance, not the momentjs representation', function () {
var moment = new u.Moment(1347915062072);
var duration = new u.Duration(10);
expect(moment.add(duration).getTimezoneOffset).toEqual(jasmine.any(Function));
});
it('should allow argument overloading', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getSeconds()).toBe(2);
moment.add(1000);
expect(moment.getSeconds()).toBe(3);
});
});
describe('Moment subtract', function () { // returns the original Moment
it('should subtract a Duration from a Moment, returning the same Moment instance', function () {
var moment = new u.Moment(1347915062072);
var duration = new u.Duration(10);
expect(moment.subtract(duration).toDate().valueOf()).toBe(1347915062062);
});
it('should return the same Moment instance, not the momentjs representation', function () {
var moment = new u.Moment(1347915062072);
var duration = new u.Duration(10);
expect(moment.subtract(duration).getTimezoneOffset).toEqual(jasmine.any(Function));
});
it('should allow argument overloading', function () {
var moment = new u.Moment(1347915062072);
expect(moment.getSeconds()).toBe(2);
moment.subtract(1000);
expect(moment.getSeconds()).toBe(1);
});
});
describe('Duration add', function () { // returns the original Duration
it('should add a Duration to a Duration, returning the original Duration instance', function () {
var duration1 = new u.Duration(10000);
var duration2 = new u.Duration(20000);
expect(duration1.add(duration2).getAsMilliseconds()).toBe(30000);
// duration1 is now 30000
expect(duration2.add(duration1).getAsMilliseconds()).toBe(50000);
});
it('should allow argument overloading', function () {
var duration = new u.Duration(5500);
duration.add(1000).add('PT 1S');
expect(duration.getAsMilliseconds()).toBe(7500);
});
});
describe('Duration subtract', function () { // returns the original Duration
it('should subtract a Duration from a Duration, returning the original Duration instance', function () {
var duration1 = new u.Duration(10000);
var duration2 = new u.Duration(20000);
expect(duration1.subtract(duration2).getAsMilliseconds()).toBe(-10000);
// duration1 is now -10000
expect(duration2.subtract(duration1).getAsMilliseconds()).toBe(30000);
});
it('should allow argument overloading', function () {
var duration = new u.Duration(5500);
duration.subtract(1000).subtract('PT 2S');
expect(duration.getAsMilliseconds()).toBe(2500);
});
});
// Only here for testing by exposing this method publicly; method is not meant to be exposed.
xdescribe('Mutate Duration', function () {
it('should mutate all properties in a Duration object', function () {
var duration1 = new u.Duration(982738947892374);
u.mutateDuration(duration1, 1234567898765);
expect(Math.ceil(duration1.getAsDays())).toBe(14289);
expect(duration1.getDays()).toBe(8);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment