Mojito markup test utility
Build Status

Overview
The markup layout test utility will render the mojit view with mock data and expose
the resulting markup to be validated using YUI Node module.
Markup tests written with this utility will be run as regular mojito unit tests
using mojito test
command
Usage
Three easy steps:
- Require
mojito-markup-test
.
- Add a mojit
spec
.
- Render view and validate markup.
Mojit spec
You must specify the mojit and view to render, along with the mock data to use.
You can mock any data, including parameters, configuration, request and headers.
spec = {
type: 'SimpleMojit',
action: 'index',
params: {
url: {
p: 'Really!'
}
},
appConfig: {
foo: 'bar'
},
req: {
p: 'flowers'
},
res: {},
headers: {
'Content-Type': 'text/html'
}
};
Rendering views
Once you have the mojit spec, you can render the view invoking the utility's render
method.
render (spec, callback)
Receives the mojit spec and a callback function which will get the error (if any),
the rendered markup string, the mojit view meta object and the node (YUI Node)
for the resulting markup.
MarkupTest.render(spec, function (err, markup, meta, node) {
test.resume(function () {
var div, em, i, a, text;
A.isNull(err);
A.areNotEqual('', markup, 'Markup');
A.isNotNull(meta, 'Meta');
A.isNotUndefined(node, 'Parent node');
A.isNotNull(node, 'Parent node');
div = node.one('.msg');
A.isNotUndefined(div, 'msg <div>');
A.isNotNull(div, 'msg <div>');
});
Full example
YUI.add('CompositeMojit_view-tests', function(Y) {
var suite = new YUITest.TestSuite('CompositeMojit'),
A = YUITest.Assert,
MarkupTest = Y.mojito.MarkupTest,
TIMEOUT = 1000;
suite.add(new YUITest.TestCase({
name: 'view',
'test index': function () {
var test = this,
spec = {
type: 'CompositeMojit',
action: 'index',
params: {
url: {
p: 'Really!'
}
},
config: {
foo: 'bar'
},
appConfig: {
some: 'config param'
},
children: {
'child_mojit': {
type: 'ChildMojit'
}
}
};
MarkupTest.render(spec, function (err, markup, meta, node) {
test.resume(function () {
var div, em, i, a, text;
A.isNull(err);
A.areNotEqual('', markup, 'Markup');
A.isNotNull(meta, 'Meta');
A.isNotUndefined(node, 'Parent node');
A.isNotNull(node, 'Parent node');
div = node.one('.msg');
A.isNotUndefined(div, 'msg <div>');
A.isNotNull(div, 'msg <div>');
em = div.one('em');
A.isNotUndefined(em, '<em>');
A.isNotNull(em, '<em>');
text = em.getContent();
A.areEqual('Mojito is working.', text, 'Rendered message <em>');
i = div.one('i');
A.isNotUndefined(i, '<i>');
A.isNotNull(i, '<i>');
text = i.getContent();
A.areEqual(spec.params.url.p, text, 'Rendered message <i>');
a = div.one('a');
A.isNotUndefined(a, '<a>');
A.isNotNull(a, '<a>');
text = a.getContent();
A.areEqual(spec.config.foo, text, 'Rendered message <a>');
});
});
test.wait(TIMEOUT);
}
}));
YUITest.TestRunner.add(suite);
}, '0.0.1', {requires: [
'mojito-test',
'mojito-markup-test'
'CompositeMojit',
'ChildMojit'
]});
See more examples.