Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Jasmine extension that simplifies writing specs for components using js-signals

License

Notifications You must be signed in to change notification settings

AdamNowotny/jasmine-signals

Repository files navigation

jasmine-signalsBuild Status

jasmine-signals is a Jasmine matcher extension that simplifies writing specs for components using JS-Signals.

Adds new matchers to Jasmine:

  • toHaveBeenDispatched
  • toHaveBeenDispatchedWith
  • matching

How to use

Installation

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.

Create

First define which signals you're interested in.

var signal = new signals.Signal();
var signalSpy = spyOnSignal(signal);
var signalSpies = jasmine.createSignalSpyObj(['method1', 'method2']);

Filtering signals

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

Expectations

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();

AMD

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();
	});
});

Examples

Just look at the specs: jasmine-signalsSpec.js

Development

Install node.js, bower, get sources from git

npm update
bower update

grunt or grunt jasmine-server to run the tests.

License

This code is distributed under Apache License version 2.0

About

Jasmine extension that simplifies writing specs for components using js-signals

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •