forked from matthijsgroen/chai-backbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chai-backbone.js
110 lines (108 loc) · 4.49 KB
/
chai-backbone.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
// Generated by CoffeeScript 1.4.0
(function() {
(function(chaiBackbone) {
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
return module.exports = chaiBackbone;
} else if (typeof define === "function" && define.amd) {
return define(function() {
return chaiBackbone;
});
} else {
return chai.use(chaiBackbone);
}
})(function(chai, utils) {
var flag, inspect, routeTo;
inspect = utils.inspect;
flag = utils.flag;
chai.Assertion.addMethod('trigger', function(trigger, options) {
var definedActions;
if (options == null) {
options = {};
}
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
this.callback = sinon.spy();
return flag(context, 'object').on(trigger, this.callback);
},
after: function(context) {
var negate, _ref;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
context.assert(this.callback.called, "expected to trigger " + trigger, "expected not to trigger " + trigger);
if (options["with"] != null) {
context.assert((_ref = this.callback).calledWith.apply(_ref, options["with"]), "expected trigger to be called with " + (inspect(options["with"])) + ", but was called with " + (inspect(this.callback.args[0])) + ".", "expected trigger not to be called with " + (inspect(options["with"])) + ", but was");
}
return flag(context, 'negate', negate);
}
});
return flag(this, 'whenActions', definedActions);
});
chai.Assertion.addProperty('route', function() {
return flag(this, 'routing', true);
});
routeTo = function(router, methodName, options) {
var consideredRouter, current_history, route, stub, _i, _len, _ref;
if (options == null) {
options = {};
}
if (!((typeof router === 'object') && (router instanceof Backbone.Router))) {
throw new TypeError('provided router is not a Backbone.Router');
}
current_history = Backbone.history;
Backbone.history = new Backbone.History;
stub = sinon.stub(router, methodName);
router._bindRoutes();
if (options.considering != null) {
_ref = options.considering;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
consideredRouter = _ref[_i];
consideredRouter._bindRoutes();
}
}
Backbone.history.options = {
root: '/'
};
route = flag(this, 'object');
Backbone.history.loadUrl(route);
Backbone.history = current_history;
router[methodName].restore();
this.assert(stub.calledOnce, "expected `" + route + "` to route to " + methodName, "expected `" + route + "` not to route to " + methodName);
if (options["arguments"] != null) {
return this.assert(stub.calledWith.apply(stub, options["arguments"]), "expected `" + methodName + "` to be called with " + (inspect(options["arguments"])) + ", but was called with " + (inspect(stub.args[0])) + " instead", "expected `" + methodName + "` not to be called with " + (inspect(options["arguments"])) + ", but was");
}
};
chai.Assertion.overwriteProperty('to', function(_super) {
return function() {
if (flag(this, 'routing')) {
return routeTo;
} else {
return _super.apply(this, arguments);
}
};
});
return chai.Assertion.addMethod('call', function(methodName) {
var definedActions, object;
object = flag(this, 'object');
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
this.originalMethod = object[methodName];
this.spy = sinon.spy();
object[methodName] = this.spy;
return typeof object.delegateEvents === "function" ? object.delegateEvents() : void 0;
},
after: function(context) {
object[methodName] = this.originalMethod;
if (typeof object.delegateEvents === "function") {
object.delegateEvents();
}
return context.assert(this.spy.callCount > 0, this.spy.printf("expected %n to have been called at least once"), this.spy.printf("expected %n to not have been called"));
}
});
return flag(this, 'whenActions', definedActions);
});
});
}).call(this);