Skip to content

Instantly share code, notes, and snippets.

@jcreamer898
Forked from commadelimited/README.md
Last active January 1, 2016 18:28
Show Gist options
  • Save jcreamer898/8183519 to your computer and use it in GitHub Desktop.
Save jcreamer898/8183519 to your computer and use it in GitHub Desktop.

The file I want to test is set up as an IIFE, which works great in the browser. I'm able to use the method renderInteractions like so:

var something = utils.renderInteractions(doc);

However, when I run the test suite I get the following error:

TypeError: Object #<Object> has no method 'renderInteraction'

I found this Stack Overflow post which seems to indicate that your JavaScript file HAS to export something that Mocha can tap into: http://stackoverflow.com/questions/10204021/how-do-i-test-normal-non-node-specific-javascript-functions-with-mocha

But I'm not sure how to accomplish that.

// if the module has no dependencies, the above pattern can be simplified to
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.utils = factory();
}
}(this, function () {
var publicMethods = {};
publicMethods.renderInteraction = function(doc) {
var interaction = {
id: doc._id,
account: doc.stream
};
if (doc.type === "tweet" && doc.data.retweeted_status && typeof doc.data.retweeted_status === "object") {
// Retweet
$.extend(interaction, {
"native_id": doc.data.id_str,
"user_id": doc.data.user.id_str,
"name": doc.data.user.name,
"screen_name": doc.data.user.screen_name
});
}
return interaction;
};
return publicMethods;
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return publicMethods;
}));
var assert = require("assert"),
utils = require("../src/emmasocial/assets/js/es/src/application.utils");
describe('Interactions', function() {
describe('render retweet', function() {
it('should not emit anything when doc is empty', function(done) {
var document = {},
rendered = utils.renderInteraction(document);
assert.equal(1, 1);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment