Permalink
Browse files

Merge pull request #894 from isao/tests

unit tests for build/*.js
  • Loading branch information...
2 parents 3269354 + 99b3224 commit 2bde38a2bccd84688eb07445c17396b7004f54a6 @isao isao committed Jan 10, 2013
@@ -70,35 +70,30 @@ function getConfigs(opts, buildtype, builddir, store) {
* @method run
* @param {Array} args Trailing cli arguments passed to cli.js
* @param {Object} opts Parsed cli options like -c (see exports.options)
- * @param {Function} cb callback to cli.js, takes string parameter for errors
+ * @param {Function} cb Callback params are: [errmsg], [show_usage], [die]
*/
function run(args, opts, cb) {
var csvctx = util.contextCsvToObject, // shortcut
buildtype = String(args[0]).toLowerCase(),
store,
conf;
- function die(err) {
- cb(err, null, true);
- }
-
switch (buildtype) {
case 'html5app':
break;
case 'hybridapp':
if (!opts.snapshotName || !opts.snapshotTag) {
- die('Build hybridapp requires --snapshotName and --snapshotTag');
+ return cb('Build hybridapp requires --snapshotName and --snapshotTag', null, true);
}
break;
case 'undefined':
- die('Missing type');
- break;
+ return cb('Missing type', null, true);
default:
- die('Invalid type');
+ return cb('Invalid type', null, true);
}
if (!util.isMojitoApp(CWD)) {
- die('Not a Mojito directory');
+ return cb('Not a Mojito directory', null, true);
}
// hash a cli context string like 'device:iphone,environment:test'
@@ -115,7 +110,7 @@ function run(args, opts, cb) {
function next(err) {
if (err) {
- return die('Error removing ' + conf.build.dir + "\n" + err);
+ return cb('Error removing ' + conf.build.dir + "\n" + err, null, true);
}
var Mojito = require(BASE + 'lib/mojito'),
@@ -127,6 +127,7 @@ function mungePage(conf, mapuri, contents) {
}
wr.write(file, contents);
+ return contents;
}
module.exports = {
@@ -17,8 +17,8 @@ function write(filepath, str) {
fs.writeFileSync(filepath, str, 'utf-8');
}
-function writeJson(filepath, str) {
- write(filepath, JSON.stringify(str, null, 4));
+function writeJson(filepath, obj) {
+ write(filepath, JSON.stringify(obj, null, 4));
}
function copy(from, to) {
View
@@ -5,7 +5,7 @@
*/
-/*jslint anon:true, sloppy:true, nomen:true, node:true*/
+/*jslint anon:true, sloppy:true, nomen:true, node:true, unparam: true, todo: true*/
/*global YUI*/
@@ -206,16 +206,19 @@ YUI.add('mojito-test', function(Y, NAME) {
}, '0.1.0', {requires: [
'event',
- "node",
- "node-event-simulate"
+ 'node',
+ 'node-event-simulate'
]});
YUI.add('mojito-test-extra', function(Y, NAME) {
var A = Y.Assert;
- Y.MOJITO_DIR = ('undefined' !== typeof require) ?
- require('path').resolve(__dirname, '../../') + '/' : null;
+ // move to mojito-test, under Y.mojito namespace? i.e. Y.mojito.BASEDIR
+ if (require && ('function' === typeof require.resolve)) {
+ // define the abs path to the mojito base dir on nodejs only
+ Y.MOJITO_DIR = require('path').resolve(__dirname, '../../') + '/';
+ }
// path doesn't need to be given, mainly used during recursion
Y.TEST_CMP = function (x, y, msg, path) {
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
+ * Copyrights licensed under the New BSD License.
+ * See the accompanying LICENSE file for terms.
+ */
+YUI().use('mojito-test-extra', 'test', function(Y) {
+
+ var A = Y.Assert,
+ V = Y.Mock.Value,
+ AA = Y.ArrayAssert,
+ OA = Y.ObjectAssert,
+ cases,
+
+ mockery = require('mockery'),
+ srcpath = Y.MOJITO_DIR + 'lib/app/commands/build/html5app.js',
+ App,
+ app,
+ mocks = {},
+ conf,
+ count;
+
+
+ mocks.init = function() {count++;};
+ mocks.mapStoreUris = mocks.init;
+ mocks.mapDefxUris = mocks.init;
+ mocks.mapFunkySpecUris = mocks.init;
+ mocks.makeManifest = mocks.init;
+ mocks.copyModule = mocks.init;
+ mocks.mungePage = mocks.init;
+
+ mockery.registerAllowable(srcpath);
+ mockery.registerMock('./shared', mocks);
+ mockery.enable({'warnOnUnregistered': false});
+ App = require(srcpath);
+
+ conf = {
+ mojitodir: '/Users/isao/Repos/mojito/myfork/',
+ app: {
+ name: 'staticpf',
+ version: '0.0.1',
+ specs: {
+ frame: {},
+ tunnelProxy: {}
+ },
+ dir: '/path/to/app'
+ },
+ snapshot: {
+ name: '',
+ tag: '',
+ packages: {}
+ },
+ build: {
+ attachManifest: false,
+ forceRelativePaths: false,
+ insertCharset: 'UTF-8',
+ port: 1111,
+ dir: '/path/to/build/dir',
+ type: 'html5app',
+ uris: []
+ },
+ context: {
+ device: 'iphone'
+ },
+ contextqs: '?device=iphone',
+ tunnelpf: '/tunnel',
+ staticpf: 'staticpf'
+ };
+
+ cases = {
+ name: 'build/html5app cases',
+
+ setUp: function() {
+ count = 0;
+ },
+
+ tearDown: function() {
+ mockery.disable();
+ },
+
+ 'test instance': function () {
+ app = new App(null, null);
+ A.isObject(app);
+ A.isFunction(app.exec);
+ A.isNull(app.scraper);
+
+ app = new App(null, 'bam');
+ A.areSame('bam', app.scraper);
+ },
+
+ 'test shared.init() is called by constructore': function () {
+ A.areSame(0, count);
+ app = new App(null, null);
+ A.areSame(1, count);
+ },
+
+ 'test mock store': function () {
+ var store = Y.Mock,
+ scraper = {
+ on: function() {
+ return scraper;
+ },
+ start: function() {
+ return scraper;
+ },
+ fetch: function() {
+ return scraper;
+ },
+ };
+
+ Y.Mock.expect(store, {method: 'getAllURLs', args:[]});
+
+ app = new App(null, scraper);
+ app.exec(conf, store, function() {});
+ Y.Mock.verify(store);
+ },
+
+ 'test exec calls shared.js X times': function () {
+ var store = Y.Mock,
+ scraper = {
+ on: function() {
+ return scraper;
+ },
+ start: function() {
+ return scraper;
+ },
+ fetch: function() {
+ return scraper;
+ },
+ };
+
+ Y.Mock.expect(store, {method: 'getAllURLs', args:[]});
+
+ A.areSame(0, count);
+ app = new App(null, scraper);
+ A.areSame(1, count);
+
+ app.exec(conf, store, function() {});
+ Y.Mock.verify(store);
+ A.areSame(6, count);
+ },
+
+ 'test exec calls scraper.js X times': function () {
+ var store = Y.Mock,
+ scraper = {
+ on: function() {
+ count++;
+ return scraper;
+ },
+ start: function() {
+ count++;
+ return scraper;
+ },
+ fetch: function() {
+ count++;
+ return scraper;
+ },
+ };
+
+ Y.Mock.expect(store, {method: 'getAllURLs', args:[]});
+
+ A.areSame(0, count);
+ app = new App(null, scraper);
+ A.areSame(1, count);
+
+ app.exec(conf, store, function() {});
+ Y.Mock.verify(store);
+ A.areSame(10, count);
+ },
+ };
+
+ Y.Test.Runner.add(new Y.Test.Case(cases));
+});
Oops, something went wrong.

0 comments on commit 2bde38a

Please sign in to comment.