/
chai-checkmark.js
56 lines (52 loc) · 1.33 KB
/
chai-checkmark.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
;(function(context, factory) {
if (typeof require === "function" &&
typeof exports === "object" &&
typeof module === "object"
) {
module.exports = factory()
} else {
if (typeof define === "function" &&
typeof define.amd === "object"
) {
define(factory)
} else {
if (!context.chai) {
throw new Error("chai-checkmark couldn't find Chai.")
}
context.chai.use(factory())
}
}
}(this, function() {
"use strict";
var nextTick = (typeof setImmediate === "function" ?
setImmediate : setTimeout),
noop = function() {}
// Chai Plugin Definition
return function chaiCheckmark(chai, util) {
var Assertion = chai.Assertion,
expect = chai.expect
function check(done) {
/*jshint validthis:true */
var total = util.flag(this, "object"),
count = 0
expect(total).a("number").above(0, "Provide a count to check")
if (done) {
expect(done).a("function", "Provide a function for check")
}
function chain() {
expect(count).below(total,
"Target checkmarks already reached")
count += 1;
}
function mark() {
if (count === total && done) { nextTick(done, 0) }
return noop;
}
mark.getCount = function() { return count; }
Assertion.addChainableMethod("mark", mark, chain)
return mark;
}
Assertion.addMethod("check", check)
Assertion.addMethod("checks", check)
}
}))