dgrid

One of the nice features of testing with Intern and Leadfoot is the ease of authoring functional tests to mimic end-user behavior. The API for retrieving relevant DOM nodes is relatively straightforward, usually with a single line of code needed to get a reference to the relevant node.

When we started writing tests for Dijit, we realized that it was often a fair amount of boilerplate to get references to specific widget instances, attach points within those widgets, and property values of widgets. One of the advantages of Intern is you can integrate this boilerplate into a helper. So, we set out to create a simple Intern helper utility to make these operations as efficient to author as normal functional tests.

dijit-intern-helper provides a simple helper utility with three primary methods:

  • byId(widgetId): A method to retrieve a serialized reference to all methods and properties on a widget
  • getProperty(widgetId, property): Return a property value on a widget instance
  • nodeById(widgetId, attachPoint): Return a reference to a specific node within a widget instance (default is the domNode)

Note that byId is less useful for some features (such as references to DOM objects), because it is simply a serialization of items from the browser back to the Node.js Intern test runner. So we more commonly use getProperty and nodeById. Here is an example of how a test might look without this utility:

.executeAsync(function (done) {
	require(['dijit/registry'], function (registry) {
		done(registry.byId('titlePane').titleBarNode);
	});
})
.then(function (node, setContext) {
	setContext(node);
})
.click()

And with the dijit-intern-helper, the reference to the relevant node can be reduced to this:

.then(dijit.nodeById('titlePane', 'titleBarNode'))
.click()

There are times where it is sufficient to just use normal DOM querying methods provided by Intern, but in cases where you want to operate on a specific widget instance, we find that this utility can reduce the complexity of your tests (and make them easier to read). We find this is especially powerful when combined with the page object pattern.

Usage

To use dijit-intern-helper, simply grab the source code and then within your tests, simply add the dependency to dijit-intern-helper within your define depedencies:

define([
    'intern!object', 'intern/chai!assert', 'require',
    'intern/dojo/node!dijit-intern-helper/helpers/dijit',
    'intern/dojo/node!leadfoot/helpers/pollUntil'
], function (registerSuite, assert, require, dijit, pollUntil) {

Learning more

To learn more about testing with Intern and Dijit, we offer Dojo 202 and Intern workshops. We are also happy to help you with your efforts to improve your tests and code quality via our JavaScript support services. Finally, if you want to discuss how we can help you implement a better approach to testing your applications, please contact us for an initial consultation.