0
votes

So I am using Reflux for my stores and actions. I have an application store that some application wide data including a flag on whether or not prevent double click is enabled. my tests look like this (using mocha, chai, and sinon):

var applicationStore = require('../../../../web/app/components/core/application.store.js');
var initialState = _.clone(applicationStore._internalData, true);

function resetToInitialState() {
  applicationStore._internalData = _.clone(initialState, true);
}

chai.use(sinonChai);

describe('application store', function() {
  beforeEach(function() {
    resetToInitialState();
  });

  it('should have default data set properly', function() {
    expect(applicationStore.getPreventDoubleClick()).to.be.false;
  });

  it('should be able to enable prevent double click flag', function() {
    applicationStore._onEnablePreventDoubleClick();

    expect(applicationStore.getPreventDoubleClick()).to.be.true;
  });

  it('should be able to disable prevent double click flag', function() {
    applicationStore._onEnablePreventDoubleClick();
    applicationStore._onDisablePreventDoubleClick();

    expect(applicationStore.getPreventDoubleClick()).to.be.false;
  });
});

Since the all stores are effectively singletons, is manually reseting it's internal data in the beforeEach a valid way to test to make sure no test effects another one? Is there a better way of doing this with mocha/chai/sinon?

1

1 Answers

-1
votes

Since I use JestJS I haven't ran into that issue. It seems to deal with singletons really well.

I'm assuming, recommending to switch testing frameworks is out of the question.

If that is the case, then your set up seems ok. My other thought would be to have it inline with the store.

var _internalData = false;

var applicationStore = {
    reset: function() {
        _internalData = false;
    }
};

Then you could just call applicationStore.reset() to reset it, and test the reset functionality.

Neither ideal really.