Mocha reflux test condition change

I am writing mocha tests against the Reflux store to verify that the action causes a state change in the repository. The following is a shortened version of the code:

Store:

var AppStore = Reflux.createStore({ init: function () { this.foo = false; }, listenables: [AppActions], onFooAction: function() { this.foo = !this.foo; this.trigger({action: "foo-ed"}); }; }); 

Act:

 var AppActions = Reflux.createActions([ "fooAction" ]); 

Test:

 it("toggles foo", function () { expect(AppStore.foo).to.equal(false); AppStore.listenables[0].fooAction(); expect(AppStore.foo).to.equal(true); }); 

However, the second statement ( expect(AppStore.foo).to.equal(true); ) does not mean that foo is still false.

By executing console.log in the onFooAction method, I checked that the method is actually running and this.foo switched.

Is there something fundamental that I'm missing here: conceptually or otherwise? I sincerely hope that this is not a matter of time!

+4
source share
1 answer

Actions generate events that stores are listening to. Basically, your test runs too fast.

Usually in my tests, I assume that Reflux will do what it does right, and I call the listener function directly. You want to add a few more statements to make sure that Reflux is connected correctly.

 it("is configured", function () { expect(AppStore.listenables).to.include(AppActions); expect(AppActions.fooAction).to.be.a('function'); }); it("toggles foo", function () { expect(AppStore.foo).to.equal(false); AppStore.onFooAction(); expect(AppStore.foo).to.equal(true); }); 

Another way to check this is with a timeout, but I feel dirty when I add timeouts inside the tests.

 it("toggles foo", function (done) { expect(AppStore.foo).to.equal(false); AppStore.listenables[0].fooAction(); setTimeout(function () { try { expect(AppStore.foo).to.equal(true); done(); } catch (e) { done(e); } }, 15); }); 
+5
source

All Articles