0

I would like to use Jasmine for unit testing in one of my dojo projects. The problem is that we have a global app object, which is needed for nearly every other object we create.

The app object is responsible for initializing the application and building up the DOM, but this is the problem. When I create the app object in my test it tries to manipulate not existing DOM elements and I got an error.

Here is a little example of the situation:

/* app.js */
define(['dojo/_base/declare', 'dojo/dom-construct'],
function(declare, domConstruct){
    return declare([], {
        constructor: function() {
            this.renderGui();
        },
        renderGui: function() {
            var gui = domConstruct.create('div', {innerHTML: 'Hello World!'});
            domConstruct.place(gui, 'content');
        },
        importantFunction: function() {
            return 100;
        }
    });
})

If I want to test the importantFunction I have to create an instance of app, but in the constructor it calls the renderGui function where i got the error, because there is no element with the id 'content'.

Hope you guys can help me. Thanks in advance!

Sebastian
  • 549
  • 1
  • 4
  • 14

1 Answers1

1

In your test suite you could write some code that creates the DOM nodes that are necessary. Take a look at this answer: https://stackoverflow.com/a/14292476/1915448

In your case you should be adding the #content node in order to make renderGui() work.


The other solution is to mock/stub your Dojo dependencies so that the domConstruct.place() function is no longer trying to add real DOM nodes. Take a look at the StubModule library, which you can use to stub dojo/dom-construct.

Community
  • 1
  • 1
g00glen00b
  • 41,995
  • 13
  • 95
  • 133