-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISSUE-41 - moment.now cannot be reassigned (#42)
* ISSUE-41 - moment.now cannot be reassigned * Adding tests around compare
- Loading branch information
Showing
3 changed files
with
94 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |