adamnowotny / jasmine-signals Goto Github PK
View Code? Open in Web Editor NEWJasmine extension that simplifies writing specs for components using js-signals
License: Other
Jasmine extension that simplifies writing specs for components using js-signals
License: Other
With some test code that dispatches one variable, such as:
function lock() { lockChanged.dispatch( true ); }
Test code
var lockChangedSignalSpy = spyOnSignal( lockChanged ); lock(); expect( lockChangedSignalSpy ).toHaveBeenDispatchedWith( true ); lock(); expect( lockChangedSignalSpy ).toHaveBeenDispatchedWith( true, 'arf' );
Both expects pass, while the second one should fail, as 'arf' was not dispatched. If 'arf' comes before true, it does fail and It does report that it was called twice with only true both times.
Thank you for this helpful plugin! :) While using it we encountered a browser issue:
In jasmine-signals.js, around line 156 there is this function in the afterEach:
afterEach(function () {
spies.forEach(function (d) {
d.stop();
});
spies = [];
});
It assumes that Array has a forEach function. Since spies appears to always be an array, this small change not only is faster due to no function calls, but works in older browsers, which still benefit from testing.
afterEach(function () {
for (var spiesIndex = 0, length = spies.length; spiesIndex < length; spiesIndex++) {
spies[spiesIndex].stop();
}
spies = [];
});
In this test I test that a signal is dispatched, in a normal way..
it("handleSetupLoaded should dispatch a 'setupComplete' Signal and pass the result", function() {
var event = {
result: "mock setup data"
};
//stubs also let us ignore function calls we are not testing in this unit
sinon.stub(this.class, "loadGameAssets");
spyOnSignal(this.class.setupComplete);
this.class.handleSetupLoaded(event);
expect(this.class.setupComplete).toHaveBeenDispatchedWith("mock setup data");
});
When I run the test in the browser SpecRunner.html, it passes, but in phantomjs using grunt-jasmine)
it fails with the following error:
TypeError: 'undefined' is not an object (evaluating 'new jasmine.signals.SignalSpy') in file:///Users/hayesmaker/Workspace/HTML5/kingkong/Trunk/public/javascripts/vendor/jasmine-signals/jasmine-signals.js (line 11)
Why would this test fail?
var spy = spyOnSignal(controller.initialCardsDealt);
controller.initialCardsDealt.dispatch([], []);
expect(spy).toHaveBeenDispatchedWith(jasmine.any[Object],jasmine.any[Object]);
Fails:
Expected [Signal active:true numListeners:1] to have been dispatched with (,) but was with (,)
Error: Expected [Signal active:true numListeners:1] to have been dispatched with (,) but was with (,)
at new jasmine.ExpectationResult (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:114:32)
at null.toHaveBeenDispatchedWith (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:1235:29)
at null. (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/spec/hayesmaker/solitaire/game/components/rulesSpec.js:48:25)
at jasmine.Block.execute (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:1064:17)
at jasmine.Queue.next_ (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:2096:31)
at jasmine.Queue.start (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:2049:8)
at jasmine.Spec.execute (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:2376:14)
at jasmine.Queue.next_ (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:2096:31)
at jasmine.Queue.start (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:2049:8)
at jasmine.Suite.execute (http://localhost:63342/solitaire-showdown/public/js/test/jasmine/lib/jasmine/jasmine.js:2521:14)
In this test I test that a signal is dispatched, in a normal way..
it("handleSetupLoaded should dispatch a 'setupComplete' Signal and pass the result", function() {
var event = {
result: "mock setup data"
};
//stubs also let us ignore function calls we are not testing in this unit
sinon.stub(this.class, "loadGameAssets");
spyOnSignal(this.class.setupComplete);
this.class.handleSetupLoaded(event);
expect(this.class.setupComplete).toHaveBeenDispatchedWith("mock setup data");
});
When I run the test in the browser SpecRunner.html, it passes, but in phantomjs using grunt-jasmine)
it fails with the following error:
TypeError: 'undefined' is not an object (evaluating 'new jasmine.signals.SignalSpy') in file:///Users/hayesmaker/Workspace/HTML5/kingkong/Trunk/public/javascripts/vendor/jasmine-signals/jasmine-signals.js (line 11)
I've just downloaded the latest version of jasmine-signals and noticed that you have removed the reset() method of the signal spy.
namespace.SignalSpy.prototype.reset = function () {
this.count = 0;
};
Has this been deprecated? I cannot find a replacement method, can you add it back in, I did find it quite useful.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.