Skip to content

Commit

Permalink
ISSUE-41 - moment.now cannot be reassigned (#42)
Browse files Browse the repository at this point in the history
* ISSUE-41 - moment.now cannot be reassigned

* Adding tests around compare
  • Loading branch information
jasonmit committed Jun 15, 2016
1 parent 7fb27bf commit d84396d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 25 deletions.
61 changes: 36 additions & 25 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
/* globals self */

import Ember from 'ember';

let ComparableMoment = Ember.Object.extend(Ember.Comparable, moment.fn, {
const moment = self.moment;

const ComparableMoment = Ember.Object.extend(Ember.Comparable, moment.fn, {
compare(a, b) {
if (moment.isMoment(a) && moment.isMoment(b) && a.isBefore(b)) {
return -1;
} else if (moment.isMoment(a) && moment.isMoment(b) && a.isAfter(b)) {
return 1;
} else if (moment.isMoment(a) && !moment.isMoment(b)) {
return 1;
} else if (moment.isMoment(b)) {
return -1;
if (moment.isMoment(a) && moment.isMoment(b)) {
if (a.isBefore(b)) {
return -1;
} else if (a.isSame(b)) {
return 0;
} else {
return 1;
}
}

return 0;
throw new Error('Arguments provided to `compare` are not moment objects');
},

clone() {
return comparableMoment(this);
}
});

let comparableMoment = function() {
return ComparableMoment.create(moment.apply(this, arguments));
function comparableMoment() {
return ComparableMoment.create(moment(...arguments));
};

for (let momentProp in moment) {
if (moment.hasOwnProperty(momentProp)) {
comparableMoment[momentProp] = moment[momentProp];
}
}

// Wrap global moment methods that return a full moment object
['utc', 'unix'].forEach((method) => {
comparableMoment[method] = function() {
return ComparableMoment.create(moment[method].apply(this, arguments));
['utc', 'unix'].forEach((methodName) => {
comparableMoment[methodName] = function() {
return ComparableMoment.create(moment[methodName](...arguments));
};
});

ComparableMoment.reopen({
clone() {
return comparableMoment(this);
for (let momentProp in moment) {
if (moment.hasOwnProperty(momentProp) && !comparableMoment.hasOwnProperty(momentProp)) {
Object.defineProperty(comparableMoment, momentProp, {
enumerable: true,
configurable: true,
get() {
return moment[momentProp];
},
set(newValue) {
moment[momentProp] = newValue;
}
});
}
});
}

export default comparableMoment;
23 changes: 23 additions & 0 deletions tests/unit/utils/moment-export-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,33 @@ test('moment compare fn exists', (assert) => {
assert.equal(typeof instance.compare, 'function');
});

test('moment compare functions property', (assert) => {
var instanceA = moment(0);
var instanceB = moment(10000);
var instanceC = moment(10000);

assert.equal(instanceA.compare(instanceA, instanceB), -1);
assert.equal(instanceB.compare(instanceB, instanceC), 0);
assert.equal(instanceB.compare(instanceB, instanceA), 1);
});


test('moment tz fn exists', (assert) => {
assert.equal(typeof moment.tz, 'function');
});

test('moment getLocale for `es`', (assert) => {
assert.equal(typeof moment.localeData('es'), 'object');
});

test('moment.now is a class function', (assert) => {
assert.equal(typeof moment.now, 'function');
});

test('moment.utc is a class function', (assert) => {
assert.equal(typeof moment.utc, 'function');
});

test('moment().clone is an instance function', (assert) => {
assert.equal(typeof moment().clone, 'function');
});
35 changes: 35 additions & 0 deletions tests/unit/utils/moment-reassign-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* globals self */

import { test, module } from 'qunit';
import moment from 'moment';

let originalMoment;

module('Unit | moment reassign', {
beforeEach() {
originalMoment = self.moment;
},
afterEach() {
self.moment = originalMoment;
}
});

test('moment exports', (assert) => {
assert.ok(moment, 'moment exports an object');
});

test('moment.now reassigned equals self.moment.now', (assert) => {
moment.now = function() {
return 123;
};

assert.equal(self.moment.now(), moment.now());
});

test('moment now reassigned is utilized in moment().format()', (assert) => {
moment.now = function() {
return 1000;
};

assert.equal(moment.utc().year(), 1970);
});

0 comments on commit d84396d

Please sign in to comment.