Browse files

Added unit tests for MailChimpAPI

  • Loading branch information...
1 parent 7a1f5ae commit 16ff9c86bd43564a351ff32d07c164a89019cead @gomfunkel committed Aug 4, 2012
View
49 test/MailChimpAPI/MailChimpAPI.js
@@ -0,0 +1,49 @@
+var assert = require('assert'),
+ vows = require('vows');
+
+var MailChimpAPI = require('mailchimp').MailChimpAPI;
+
+vows.describe('MailChimpAPI').addBatch({
+
+ 'Instantiating the MailChimp API wrapper': {
+
+ 'without any arguments': {
+ topic: function () { return new MailChimpAPI() },
+ 'throws an error': function (topic) {
+ assert.throws(topic, Error);
+ assert.strictEqual(topic.message, 'You have to provide an API key for this to work.');
+ }
+ },
+
+ 'with an API key': {
+ topic: function () { return new MailChimpAPI('apiKey-dc') },
+ 'successfully creates an instance': function (topic) {
+ assert.isObject(topic);
+ }
+ },
+
+ 'without API key but with parameters': {
+ topic: function () { new MailChimpAPI({ version: '1.3' }) },
+ 'throws an error': function (topic) {
+ assert.throws(topic, Error);
+ }
+ },
+
+ 'with an API key and parameters': {
+ topic: function () { return new MailChimpAPI('apiKey-dc', { version: '1.3' }) },
+ 'successfully creates an instance': function (topic) {
+ assert.isObject(topic);
+ }
+ },
+
+ 'with an API key and an unsupported version': {
+ topic: function () { return new MailChimpAPI('apiKey-dc', { version: '0.1' }) },
+ 'throws an error': function (topic) {
+ assert.throws(topic, Error);
+ assert.strictEqual(topic.message, 'Version 0.1 of the MailChimp API is currently not supported.');
+ }
+ }
+
+ }
+
+}).export(module);
View
64 test/MailChimpAPI/MailChimpAPI_v1_1.js
@@ -0,0 +1,64 @@
+var assert = require('assert'),
+ vows = require('vows'),
+ nock = require('nock');
+
+var apiEndpoint = nock('http://dc.api.mailchimp.com:80').post('/1.1/?output=json&method=inlineCss', { apikey: "apiKey-dc" })
+ .reply(200, '{"error":"You must specify a html value for the inlineCss method","code":-90}')
+ .post('/1.1/?output=json&method=inlineCss', { apikey: "apiKey-dc", html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>" })
+ .reply(200, "\"<html><head><style>body { background-color: #f00; }<\\/style><\\/head><body style=\\\"background-color: #f00;\\\"><\\/body><\\/html>\"")
+ .post('/1.1/?output=json&method=inlineCss', { apikey: "apiKey-dc", html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true" })
+ .reply(200, "\"<html><head><\\/head><body style=\\\"background-color: #f00;\\\"><\\/body><\\/html>\"")
+ .post('/1.1/?output=json&method=inlineCss', { apikey: "apiKey-dc", html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true" })
+ .reply(200, "\"<html><head><\\/head><body style=\\\"background-color: #f00;\\\"><\\/body><\\/html>\"");
+
+var MailChimpAPI = require('mailchimp').MailChimpAPI;
+
+vows.describe('MailChimpAPI v1.1').addBatch({
+
+ 'MailChimpAPI v1.1 wrapper': {
+
+ 'when instantiated': {
+ topic: function () { return new MailChimpAPI('apiKey-dc', { version : '1.1' }) },
+ 'successfully creates an instance': function (api) {
+ assert.isObject(api);
+ assert.strictEqual(api.version, '1.1');
+ },
+
+ /*** inlineCss ***/
+
+ 'and calling method "inlineCss" with mandatory arguments': {
+ topic: function (api) { api.inlineCss({ html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>" }, this.callback) },
+ 'returns html with inline CSS': function (error, data) {
+ assert.isNull(error);
+ assert.isString(data);
+ }
+ },
+
+ 'and calling method "inlineCss" without mandatory arguments': {
+ topic: function (api) { api.inlineCss(this.callback) },
+ 'returns an error': function (error, data) {
+ assert.instanceOf(error, Error);
+ }
+ },
+
+ 'and calling method "inlineCss" with all arguments': {
+ topic: function (api) { api.inlineCss({ html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true" }, this.callback) },
+ 'returns html with inline CSS': function (error, data) {
+ assert.isNull(error);
+ assert.isString(data);
+ }
+ },
+
+ 'and calling method "inlineCss" with superflous arguments': {
+ topic: function (api) { api.inlineCss({ html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true", superflous: "superflous" }, this.callback) },
+ 'returns html with inline CSS and superflous arguments are discarded': function (error, data) {
+ assert.isNull(error);
+ assert.isString(data);
+ }
+ }
+
+ }
+
+ }
+
+}).export(module);
View
64 test/MailChimpAPI/MailChimpAPI_v1_2.js
@@ -0,0 +1,64 @@
+var assert = require('assert'),
+ vows = require('vows'),
+ nock = require('nock');
+
+var apiEndpoint = nock('http://dc.api.mailchimp.com:80').post('/1.2/?output=json&method=inlineCss', { apikey: "apiKey-dc" })
+ .reply(200, '{"error":"You must specify a html value for the inlineCss method","code":-90}')
+ .post('/1.2/?output=json&method=inlineCss', { apikey: "apiKey-dc", html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>" })
+ .reply(200, "\"<html><head><style>body { background-color: #f00; }<\\/style><\\/head><body style=\\\"background-color: #f00;\\\"><\\/body><\\/html>\"")
+ .post('/1.2/?output=json&method=inlineCss', { apikey: "apiKey-dc", html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true" })
+ .reply(200, "\"<html><head><\\/head><body style=\\\"background-color: #f00;\\\"><\\/body><\\/html>\"")
+ .post('/1.2/?output=json&method=inlineCss', { apikey: "apiKey-dc", html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true" })
+ .reply(200, "\"<html><head><\\/head><body style=\\\"background-color: #f00;\\\"><\\/body><\\/html>\"");
+
+var MailChimpAPI = require('mailchimp').MailChimpAPI;
+
+vows.describe('MailChimpAPI v1.2').addBatch({
+
+ 'MailChimpAPI v1.2 wrapper': {
+
+ 'when instantiated': {
+ topic: function () { return new MailChimpAPI('apiKey-dc', { version : '1.2' }) },
+ 'successfully creates an instance': function (api) {
+ assert.isObject(api);
+ assert.strictEqual(api.version, '1.2');
+ },
+
+ /*** inlineCss ***/
+
+ 'and calling method "inlineCss" with mandatory arguments': {
+ topic: function (api) { api.inlineCss({ html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>" }, this.callback) },
+ 'returns html with inline CSS': function (error, data) {
+ assert.isNull(error);
+ assert.isString(data);
+ }
+ },
+
+ 'and calling method "inlineCss" without mandatory arguments': {
+ topic: function (api) { api.inlineCss(this.callback) },
+ 'returns an error': function (error, data) {
+ assert.instanceOf(error, Error);
+ }
+ },
+
+ 'and calling method "inlineCss" with all arguments': {
+ topic: function (api) { api.inlineCss({ html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true" }, this.callback) },
+ 'returns html with inline CSS': function (error, data) {
+ assert.isNull(error);
+ assert.isString(data);
+ }
+ },
+
+ 'and calling method "inlineCss" with superflous arguments': {
+ topic: function (api) { api.inlineCss({ html: "<html><head><style>body { background-color: #f00; }</style></head><body></body></html>", strip_css: "true", superflous: "superflous" }, this.callback) },
+ 'returns html with inline CSS and superflous arguments are discarded': function (error, data) {
+ assert.isNull(error);
+ assert.isString(data);
+ }
+ }
+
+ }
+
+ }
+
+}).export(module);
View
64 test/MailChimpAPI/MailChimpAPI_v1_3.js
@@ -0,0 +1,64 @@
+var assert = require('assert'),
+ vows = require('vows'),
+ nock = require('nock');
+
+var apiEndpoint = nock('http://dc.api.mailchimp.com:80').post('/1.3/?method=folderAdd', { apikey: "apiKey-dc" })
+ .reply(200, '{"error":"You must specify a name value for the folderAdd method","code":-90}')
+ .post('/1.3/?method=folderAdd', { apikey: "apiKey-dc", name: "foldername" })
+ .reply(200, 123)
+ .post('/1.3/?method=folderAdd', { apikey: "apiKey-dc", name: "foldername", type: "autoresponder" })
+ .reply(200, 456)
+ .post('/1.3/?method=folderAdd', { apikey: "apiKey-dc", name: "foldername", type: "autoresponder" })
+ .reply(200, 789);
+
+var MailChimpAPI = require('mailchimp').MailChimpAPI;
+
+vows.describe('MailChimpAPI v1.3').addBatch({
+
+ 'MailChimpAPI v1.3 wrapper': {
+
+ 'when instantiated': {
+ topic: function () { return new MailChimpAPI('apiKey-dc', { version : '1.3' }) },
+ 'successfully creates an instance': function (api) {
+ assert.isObject(api);
+ assert.strictEqual(api.version, '1.3');
+ },
+
+ /*** folderAdd ***/
+
+ 'and calling method "folderAdd" with mandatory arguments': {
+ topic: function (api) { api.folderAdd({ name: "foldername" }, this.callback) },
+ 'creates the folder': function (error, data) {
+ assert.isNull(error);
+ assert.isNumber(data);
+ }
+ },
+
+ 'and calling method "folderAdd" without mandatory arguments': {
+ topic: function (api) { api.folderAdd(this.callback) },
+ 'returns an error': function (error, data) {
+ assert.instanceOf(error, Error);
+ }
+ },
+
+ 'and calling method "folderAdd" with all arguments': {
+ topic: function (api) { api.folderAdd({ name: "foldername", type: "autoresponder" }, this.callback) },
+ 'creates the folder': function (error, data) {
+ assert.isNull(error);
+ assert.isNumber(data);
+ }
+ },
+
+ 'and calling method "folderAdd" with superflous arguments': {
+ topic: function (api) { api.folderAdd({ name: "foldername", type: "autoresponder", superflous: "superflous" }, this.callback) },
+ 'creates the folder and superflous arguments are discarded': function (error, data) {
+ assert.isNull(error);
+ assert.isNumber(data);
+ }
+ }
+
+ }
+
+ }
+
+}).export(module);

0 comments on commit 16ff9c8

Please sign in to comment.