-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
110 lines (97 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// the format used by chai-moment errors. defaults to "LLLL".
var _format = 'LLLL';
function _chaiMoment(chai, utils){
var moment;
if (
typeof window === "object"
&& typeof window.moment == "function"
) {
// browser-side
moment = window.moment;
} else {
// server-side
moment = require('moment');
}
chai.Assertion.addMethod('sameMoment', function(expected, granularity) {
var obj = this._obj
var objMoment = moment(obj)
var expectedMoment = moment(expected)
this.assert(
objMoment.isSame(expectedMoment, granularity)
, "expected " + objMoment.format(_format) + " to be the same as " + expectedMoment.format(_format) + (granularity ? " (granularity: " + granularity + ")" : "")
, "expected " + objMoment.format(_format) + " not to be the same as " + expectedMoment.format(_format) + (granularity ? " (granularity: " + granularity + ")" : "")
, expected
, obj
, true
)
});
chai.Assertion.addMethod('beforeMoment', function(expected, granularity) {
var obj = this._obj
var objMoment = moment(obj)
var expectedMoment = moment(expected)
this.assert(
objMoment.isBefore(expectedMoment, granularity)
, "expected " + objMoment.format(_format) + " to be before " + expectedMoment.format(_format) + (granularity ? " (granularity: " + granularity + ")" : "")
, "expected " + objMoment.format(_format) + " not to be before " + expectedMoment.format(_format) + (granularity ? " (granularity: " + granularity + ")" : "")
, expected
, obj
, true
)
});
chai.Assertion.addMethod('afterMoment', function(expected, granularity) {
var obj = this._obj
var objMoment = moment(obj)
var expectedMoment = moment(expected)
this.assert(
objMoment.isAfter(expectedMoment, granularity)
, "expected " + objMoment.format(_format) + " to be after " + expectedMoment.format(_format) + (granularity ? " (granularity: " + granularity + ")" : "")
, "expected " + objMoment.format(_format) + " not to be after " + expectedMoment.format(_format) + (granularity ? " (granularity: " + granularity + ")" : "")
, expected
, obj
, true
)
});
//export tdd style
var assert = chai.assert;
var allowedGranularities = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second'],
allowedGranularitiesLookup = {};
allowedGranularities.forEach(function(type) {
allowedGranularitiesLookup[type] = true;
});
assert.sameMoment = function (val, exp, granularity, msg) {
if(!allowedGranularitiesLookup[granularity]) msg = granularity;
new chai.Assertion(val, msg).to.be.sameMoment(exp, granularity);
};
assert.beforeMoment = function (val, exp, granularity, msg) {
if(!allowedGranularitiesLookup[granularity]) msg = granularity;
new chai.Assertion(val, msg).to.be.beforeMoment(exp, granularity);
};
assert.afterMoment = function (val, exp, granularity, msg) {
if(!allowedGranularitiesLookup[granularity]) msg = granularity;
new chai.Assertion(val, msg).to.be.afterMoment(exp, granularity);
};
}
_chaiMoment.setErrorFormat = function setErrorFormat(format) {
_format = format;
};
(function(plugin){
if (
typeof require === "function"
&& typeof exports === "object"
&& typeof module === "object"
) {
// NodeJS
module.exports = plugin;
} else if (
typeof define === "function"
&& define.amd
) {
// AMD
define(function () {
return plugin;
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(plugin);
}
}(_chaiMoment));