jasmine-signals
is a Jasmine matcher extension that simplifies writing specs for components using JS-Signals.
Adds new matchers to Jasmine:
- toHaveBeenDispatched
- toHaveBeenDispatchedWith
- matching
bower install jasmine-signals
or just copy jasmine-signals.js to wherever you like.
The library uses some methods not available in old versions of IE (forEach, map, filter), so you might need to include es5-shim as well.
First define which signals you're interested in.
var signal = new signals.Signal();
var signalSpy = spyOnSignal(signal);
var signalSpies = jasmine.createSignalSpyObj(['method1', 'method2']);
You can pass a boolean function to specify which dispatches to register.
var signal = new signals.Signal();
var signalSpy = spyOnSignal(signal).matching(function (dispatchInfo) {
return dispatchInfo !== null;
});
signal.dispatch(); // ignored
signal.dispatch(5); // registered
After defining the spy you can set expectations in your spec.
expect(signalSpy).toHaveBeenDispatched(); // expect signal to have been dispatched at least once
expect(signal).toHaveBeenDispatched(); // passing signal is also allowed, first spy will be used
expect(signal).toHaveBeenDispatched(3); // expect signal to have been dispatched 3 times
expect(signal).toHaveBeenDispatchedWith(1, 5); // expect signal to have been dispatched with parameters
expect(signalSpies.method1).toHaveBeenDispatched();
expect(signalSpies['method2']).not.toHaveBeenDispatched();
It's possible to use jasmine-signals
as an AMD (Asynchronous Module Definition) module.
jasmine-signals
depends on JS-Signals, so first define signals
path:
require.config({
paths: {
signals: 'components/js-signals/signals',
jasmineSignals: 'components/jasmine-signals/jasmine-signals'
}
});
Then use it in Jasmine
tests like this:
define(['myClass', 'jasmineSignals'], function(myClass, spyOnSignal) {
it('should signal completed', function () {
spyOnSignal(myClass.completed);
myClass.doSomething();
expect(myClass.completed).toHaveBeenDispatched();
});
});
Just look at the specs: jasmine-signalsSpec.js
Install node.js, bower, get sources from git
npm update
bower update
grunt
or grunt jasmine-server
to run the tests.
This code is distributed under Apache License version 2.0