dagda1
dagda1

Reputation: 28760

Backbone Marioniette event .EventAggregator and tesing

I am having a timing issue with my tests. I am using the event aggregator to raise events and the problem is that my specs are completing before the code gets to an event that is raised. I need the specs to run after the evented code has ran.

For example, I am creating a layout and then raising an event:

  DocumentManager.addInitializer(function(){
    DocumentManager.layout = new Layout();

    DocumentManager.layout.on("show", function(){
      DocumentManager.vent.trigger("layout:rendered");
    });

    DocumentManager.content.show(DocumentManager.layout)
  }); 

I then create another view after the layout has been created:

  DocumentManager.vent.on("layout:rendered", function(){
    Documents.folders = new Documents.Folders();

    Documents.folders.reset(window._rootFolder);    

    Documents.treeRoot = new Documents.TreeRoot({
      collection: Documents.folders
    });

    DocumentManager.layout.treeView.show(Documents.treeRoot);

    DocumentManager.vent.trigger("folder:added");
  });

The problem is that my specs finish before this code is ran:

describe 'battlebox', ->
  describe 'versioned documents', ->
    describe 'empty root and no files', ->
      beforeEach ->
        loadFixtures "battlebox.html"
        DocumentManager.start()
        window._rootFolder = Test.Factory.BattleBox.emptyRoot()

      it "should create a root folder", ->
        expect(DocumentManager.Documents.folders.length).toEqual 1

My options are to either trigger the event from the test or refactor the code out of the "layout:rendered" event handler into a method which I "setup" and call from my test.

I am curious if anyone has a better idea?

Upvotes: 0

Views: 189

Answers (2)

Kaleb F.
Kaleb F.

Reputation: 193

I use two types of async checks:

An example with your spec:

describe 'battlebox', ->
  describe 'versioned documents', ->
    describe 'empty root and no files', ->
      beforeEach ->
        loadFixtures "battlebox.html"

      it "should create a root folder", ->
        # Expect event 'layout:rendered' to be triggered
        TestHelpers.eventTriggeredOn DocumentManager.vent, 'layout:rendered', =>
          expect(DocumentManager.Documents.folders.length).toEqual 1

        # Async bit
        DocumentManager.start()
        window._rootFolder = Test.Factory.BattleBox.emptyRoot()

Upvotes: 2

Derick Bailey
Derick Bailey

Reputation: 72858

If the it is running before the beforeEach has finished, then there is some code in your setup that is running asynchronously. You'll have to use Jasmine's async capabilities to make this work. https://github.com/pivotal/jasmine/wiki/Asynchronous-specs

Also - you might want to split your test up in to smaller pieces. Since you have an event that is triggered to facilitate the rendering of the Folders, split the test here. Have one test show that the initializer is setting up the layout correctly. Then have another test that shows the Folders are rendering correctly. You can make the second test work by directly triggering the "layout:rendered" event in your test.

Upvotes: 1

Related Questions