diff --git a/docs/resources/templates.md b/docs/resources/templates.md
index df905da..f149aac 100644
--- a/docs/resources/templates.md
+++ b/docs/resources/templates.md
@@ -1,49 +1,34 @@
# Templates
-This library provides easy access to the [Templates](https://www.sparkpost.com/api#/reference/templates/) Resource.
+This library provides easy access to the [Templates](https://developers.sparkpost.com/api/templates) Resource.
+
+*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).*
## Methods
-* **all(callback)**
+* **list()**
List a summary of all templates.
- * `callback` - executed after task is completed. **required**
- * standard `callback(err, data)`
- * `err` - any error that occurred
- * `data` - full response from request client
-* **find(options, callback)**
- Retrieve details about a specified template by its id
+* **get(id[, options])**
+ Get details about a specified template by its id
* `options.id` - the id of the template you want to look up **required**
* `options.draft` - specifies a draft or published template
- * `callback` - see all function
-* **create(options, callback)**
+* **create(template)**
Create a new template
- * `options.template` - a template object **required**
- * `callback` - see all function
-* **delete(id, callback)**
+ * `template` - an object of [template attributes](https://developers.sparkpost.com/api/templates#header-template-attributes) **required**
+* **update(id, template[, options])**
+ Update an existing template
+ * `id` - the id of the template you want to update **required**
+ * `template` - an object of [template attributes](https://developers.sparkpost.com/api/templates#header-template-attributes) **required**
+ * `options.update_published` - If true, directly overwrite the existing published template. If false, create a new draft.
+* **delete(id)**
Delete an existing template
* `id` - the id of the template you want to delete **required**
- * `callback` - see all function
-* **preview(options, callback)**
+* **preview(id[, options])**
Preview the most recent version of an existing template by id
- * `options.id` - the id of the template you want to look up **required**
- * `options.data` - Object of substitution data
+ * `id` - the id of the template you want to look up **required**
+ * `options.substitution_data` - Object of substitution data
* `options.draft` - specifies a draft or published template
- * `callback` - see all function
## Examples
-```js
-var SparkPost = require('sparkpost');
-var client = new SparkPost('YOUR_API_KEY');
-
-client.templates.all(function(err, data) {
- if(err) {
- console.log(err);
- return;
- }
-
- console.log(data);
-});
-
-```
-Check out all the examples provided [here](/examples/templates).
+Visit our examples section to see all of [our template resource examples](/examples/templates).
diff --git a/examples/templates/create.js b/examples/templates/create.js
new file mode 100644
index 0000000..f7f048a
--- /dev/null
+++ b/examples/templates/create.js
@@ -0,0 +1,36 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key)
+ , template = {
+ id: 'TEST_ID',
+ name: 'Test Template',
+ content: {
+ from: 'test@test.com',
+ subject: 'Test email template!',
+ html: 'This is a test email template!'
+ }
+ };
+
+// Promise
+client.templates.create(template)
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Callback
+client.templates.create(template, function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/create_template.js b/examples/templates/create_template.js
deleted file mode 100644
index a6cb7fc..0000000
--- a/examples/templates/create_template.js
+++ /dev/null
@@ -1,25 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key)
- , options = {
- template: {
- id: 'TEST_ID'
- , name: 'Test Template'
- , content: {
- from: 'test@test.com'
- , subject: 'Test email template!'
- , html: 'This is a test email template!'
- }
- }
- };
-
-client.templates.create(options, function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/examples/templates/delete.js b/examples/templates/delete.js
new file mode 100644
index 0000000..a46f8aa
--- /dev/null
+++ b/examples/templates/delete.js
@@ -0,0 +1,27 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key);
+
+// Promise
+client.templates.delete('TEST_ID')
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Callback
+client.templates.delete('TEST_ID', function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/delete_template.js b/examples/templates/delete_template.js
deleted file mode 100644
index 93ebaa0..0000000
--- a/examples/templates/delete_template.js
+++ /dev/null
@@ -1,14 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key);
-
-client.templates['delete']('TEST_ID', function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/examples/templates/get.js b/examples/templates/get.js
new file mode 100644
index 0000000..25bddaf
--- /dev/null
+++ b/examples/templates/get.js
@@ -0,0 +1,27 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key);
+
+// Promise
+client.templates.get('TEST_ID')
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Callback
+client.templates.get('TEST_ID', function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/get_all_templates.js b/examples/templates/get_all_templates.js
deleted file mode 100644
index e079062..0000000
--- a/examples/templates/get_all_templates.js
+++ /dev/null
@@ -1,14 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key);
-
-client.templates.all(function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/examples/templates/get_draft.js b/examples/templates/get_draft.js
new file mode 100644
index 0000000..5023f2d
--- /dev/null
+++ b/examples/templates/get_draft.js
@@ -0,0 +1,30 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key)
+ , options = {
+ draft: true
+ };
+
+// Promise
+client.templates.get('TEST_ID', options)
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Callback
+client.templates.get('TEST_ID', options, function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/get_draft_template.js b/examples/templates/get_draft_template.js
deleted file mode 100644
index d4f6b7c..0000000
--- a/examples/templates/get_draft_template.js
+++ /dev/null
@@ -1,18 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key)
- , options = {
- id: 'TEST_ID'
- , draft: true
- };
-
-client.templates.find(options, function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/examples/templates/get_template.js b/examples/templates/get_template.js
deleted file mode 100644
index 8a64a28..0000000
--- a/examples/templates/get_template.js
+++ /dev/null
@@ -1,17 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key)
- , options = {
- id: 'TEST_ID'
- };
-
-client.templates.find(options, function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/examples/templates/list.js b/examples/templates/list.js
new file mode 100644
index 0000000..c249931
--- /dev/null
+++ b/examples/templates/list.js
@@ -0,0 +1,27 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key);
+
+// Promise
+client.templates.list()
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Callback
+client.templates.list(function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/preview.js b/examples/templates/preview.js
new file mode 100644
index 0000000..f25059f
--- /dev/null
+++ b/examples/templates/preview.js
@@ -0,0 +1,30 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key)
+ , options = {
+ id: 'TEST_ID',
+ substitution_data: {}
+ };
+
+client.templates.preview(options)
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Using a callback
+client.templates.preview(options, function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/preview_template.js b/examples/templates/preview_template.js
deleted file mode 100644
index b3b62ba..0000000
--- a/examples/templates/preview_template.js
+++ /dev/null
@@ -1,18 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key)
- , options = {
- id: 'TEST_ID'
- , data: {}
- };
-
-client.templates.preview(options, function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/examples/templates/update.js b/examples/templates/update.js
new file mode 100644
index 0000000..372e326
--- /dev/null
+++ b/examples/templates/update.js
@@ -0,0 +1,34 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key)
+ , template = {
+ content: {
+ from: 'test@test.com',
+ subject: 'Updated Test email template!',
+ html: 'This is a test email template! Updated!'
+ }
+ };
+
+// Promise
+client.templates.update('TEST_ID', template)
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Callback
+client.templates.update('TEST_ID', template, function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/update_published.js b/examples/templates/update_published.js
new file mode 100644
index 0000000..ba5042b
--- /dev/null
+++ b/examples/templates/update_published.js
@@ -0,0 +1,37 @@
+'use strict';
+
+var key = 'YOURAPIKEY'
+ , SparkPost = require('sparkpost')
+ , client = new SparkPost(key)
+ , template = {
+ content: {
+ from: 'test@test.com',
+ subject: 'Updated Published Test email template!',
+ html: 'This is a published test email template! Updated!'
+ }
+ }
+ , options = {
+ update_published: true
+ };
+
+// Promise
+client.templates.update('TEST_ID', template, options)
+ .then(data => {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ })
+ .catch(err => {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ });
+
+// Callback
+client.templates.update('TEST_ID', template, options, function(err, data) {
+ if (err) {
+ console.log('Whoops! Something went wrong');
+ console.log(err);
+ } else {
+ console.log('Congrats you can use our client library!');
+ console.log(data);
+ }
+});
diff --git a/examples/templates/update_published_template.js b/examples/templates/update_published_template.js
deleted file mode 100644
index 3cbc1a8..0000000
--- a/examples/templates/update_published_template.js
+++ /dev/null
@@ -1,25 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key)
- , options = {
- template: {
- id: 'TEST_ID'
- , content: {
- from: 'test@test.com'
- , subject: 'Updated Published Test email template!'
- , html: 'This is a published test email template! Updated!'
- }
- }
- , update_published: true
- };
-
-client.templates.update(options, function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/examples/templates/update_template.js b/examples/templates/update_template.js
deleted file mode 100644
index d3973c8..0000000
--- a/examples/templates/update_template.js
+++ /dev/null
@@ -1,24 +0,0 @@
-'use strict';
-
-var key = 'YOURAPIKEY'
- , SparkPost = require('sparkpost')
- , client = new SparkPost(key)
- , options = {
- template: {
- id: 'TEST_ID'
- , content: {
- from: 'test@test.com'
- , subject: 'Updated Test email template!'
- , html: 'This is a test email template! Updated!'
- }
- }
- };
-
-client.templates.update(options, function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- console.log('Congrats you can use our client library!');
- }
-});
diff --git a/lib/templates.js b/lib/templates.js
index 0afc637..3e879dd 100644
--- a/lib/templates.js
+++ b/lib/templates.js
@@ -1,81 +1,112 @@
'use strict';
-var api = 'templates'
- , Promise = require('./Promise')
- , toApiFormat = require('./toApiFormat');
+const api = 'templates';
+const Promise = require('./Promise');
+const _ = require('lodash');
module.exports = function(client) {
- var templates = {
- all: function(callback) {
+ return {
+ /**
+ * List an overview of all templates.
+ *
+ * @param {RequestCb} [callback]
+ * @returns {Promise}
+ */
+ list: function(callback) {
var options = {
uri: api
};
return client.get(options).asCallback(callback);
},
- find: function(options, callback) {
+ /**
+ * Get details about a specified template by its id.
+ *
+ * @param {string} id
+ * @param {Object} options
+ * @param {RequestCb} [callback]
+ * @returns {Promise}
+ */
+ get: function(id, options, callback) {
var reqOpts;
options = options || {};
- if (!options.id) {
+ if (!id) {
return Promise.reject(new Error('template id is required')).asCallback(callback);
}
reqOpts = {
- uri: api + '/' + options.id
+ uri: api + '/' + id
+ , qs: options
};
- if (options.draft) {
- reqOpts.qs = reqOpts.qs || {};
- reqOpts.qs.draft = options.draft;
- }
-
return client.get(reqOpts).asCallback(callback);
},
- create: function(options, callback) {
+ /**
+ * Create a new template.
+ *
+ * @param {Object} template
+ * @param {RequestCb} [callback]
+ * @returns {Promise}
+ */
+ create: function(template, callback) {
var reqOpts;
- options = options || {};
- if (!options.template) {
+ if (!template || typeof template !== 'object') {
return Promise.reject(new Error('template object is required')).asCallback(callback);
}
reqOpts = {
uri: api
- , json: toApiFormat(options.template)
+ , json: template
};
return client.post(reqOpts).asCallback(callback);
},
- update: function(options, callback) {
- var object, reqOpts;
- options = options || {};
+ /**
+ * Update an existing template.
+ *
+ * @param {String} id
+ * @param {Object} template
+ * @param {Object} options
+ * @param {RequestCb} callback
+ * @returns {Promise}
+ */
+ update: function(id, template, options, callback) {
+ var reqOpts;
+
+ // Handle optional options argument
+ if (typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
- if (!options.template) {
+ if (!id) {
+ return Promise.reject(new Error('template id is required')).asCallback(callback);
+ }
+
+ if (!template || typeof template !== 'object') {
return Promise.reject(new Error('template object is required')).asCallback(callback);
}
- object = toApiFormat(options.template);
reqOpts = {
- uri: api + '/' + object.id
- , json: object
+ uri: api + '/' + id
+ , json: template
+ , qs: options
};
- if (options.update_published) {
- reqOpts.qs = reqOpts.qs || {};
- reqOpts.qs.update_published = options.update_published;
- }
-
return client.put(reqOpts).asCallback(callback);
},
+ /**
+ * Delete an existing template.
+ *
+ * @param {String} id
+ * @param {RequestCb} [callback]
+ * @returns {Promise}
+ */
delete: function(id, callback) {
var options;
- if (typeof id === 'function') {
- callback = id;
- id = null;
- }
-
- if (!id) {
+ if (!id || typeof id !== 'string') {
return Promise.reject(new Error('template id is required')).asCallback(callback);
}
@@ -84,29 +115,40 @@ module.exports = function(client) {
};
return client.delete(options).asCallback(callback);
},
- preview: function(options, callback) {
+ /**
+ * Preview the most recent version of an existing template by id.
+ *
+ * @param {String} id
+ * @param {Object} options
+ * @param {RequestCb} [callback]
+ * @returns {Promise}
+ */
+ preview: function(id, options, callback) {
var reqOpts;
options = options || {};
- if (!options.id) {
+ // Handle optional options argument
+ if (typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
+
+ if (!id) {
return Promise.reject(new Error('template id is required')).asCallback(callback);
}
reqOpts = {
- uri: api + '/' + options.id + '/preview'
- , json: {
- substitution_data: options.data
- }
+ uri: api + '/' + id + '/preview'
+ , json: _.cloneDeep(options)
+ , qs: {}
};
- if (options.draft) {
- reqOpts.qs = reqOpts.qs || {};
- reqOpts.qs.draft = options.draft;
+ if (reqOpts.json.draft) {
+ reqOpts.qs.draft = reqOpts.json.draft;
+ delete reqOpts.json.draft;
}
return client.post(reqOpts).asCallback(callback);
}
};
-
- return templates;
};
diff --git a/lib/webhooks.js b/lib/webhooks.js
index 0b2f63d..d5a9b42 100644
--- a/lib/webhooks.js
+++ b/lib/webhooks.js
@@ -1,8 +1,8 @@
'use strict';
-let _ = require('lodash')
- , Promise = require('./Promise');
const api = 'webhooks';
+const Promise = require('./Promise');
+const _ = require('lodash');
module.exports = function(client) {
var webhooks = {
diff --git a/test/spec/templates.spec.js b/test/spec/templates.spec.js
index 9161cea..a0760bc 100644
--- a/test/spec/templates.spec.js
+++ b/test/spec/templates.spec.js
@@ -1,180 +1,183 @@
-var chai = require('chai')
+'use strict';
+
+var _ = require('lodash')
+ , chai = require('chai')
, expect = chai.expect
- , sinon = require('sinon')
- , sinonChai = require('sinon-chai')
- , Promise = require('../../lib/Promise');
+ , sinon = require('sinon');
+
+require('sinon-as-promised');
-chai.use(sinonChai);
+chai.use(require('sinon-chai'));
+chai.use(require('chai-as-promised'));
describe('Templates Library', function() {
var client, templates;
beforeEach(function() {
client = {
- get: sinon.stub().returns(Promise.resolve({})),
- post: sinon.stub().returns(Promise.resolve({})),
- put: sinon.stub().returns(Promise.resolve({})),
- delete: sinon.stub().returns(Promise.resolve({}))
+ get: sinon.stub().resolves({}),
+ post: sinon.stub().resolves({}),
+ put: sinon.stub().resolves({}),
+ delete: sinon.stub().resolves({})
};
templates = require('../../lib/templates')(client);
});
- describe('all Method', function() {
- it('should call client get method with the appropriate uri', function(done) {
- templates.all(function(err, data) {
- expect(client.get.firstCall.args[0].uri).to.equal('templates');
- done();
- });
+ describe('list Method', function() {
+ it('should call client get method with the appropriate uri', function() {
+ return templates.list()
+ .then(function() {
+ expect(client.get.firstCall.args[0].uri).to.equal('templates');
+ });
});
});
- describe('find Method', function() {
- it('should call client get method with the appropriate uri', function(done) {
- var options = {
- id: 'test'
- };
- templates.find(options, function(err, data) {
- expect(client.get.firstCall.args[0].uri).to.equal('templates/test');
- done();
- });
+ describe('get Method', function() {
+ it('should call client get method with the appropriate uri', function() {
+ var id = 'test';
+ return templates.get(id)
+ .then(function() {
+ expect(client.get.firstCall.args[0].uri).to.equal('templates/test');
+ });
});
- it('should throw an error if id is missing', function(done) {
- templates.find(null, function(err) {
- expect(err.message).to.equal('template id is required');
- expect(client.get).not.to.have.been.called;
- done();
- });
+ it('should throw an error if id is missing', function() {
+ return expect(templates.get()).to.be.rejectedWith('template id is required');
});
- it('should allow draft to be set in options', function(done) {
- var options = {
- id: 'test',
- draft: true
- };
+ it('should allow draft to be set in options', function() {
+ var id = 'test'
+ , options = {
+ draft: true
+ };
- templates.find(options, function(err, data) {
- expect(client.get.firstCall.args[0].qs).to.deep.equal({draft: true});
- done();
- });
+
+ return templates.get(id, options)
+ .then(function() {
+ expect(client.get.firstCall.args[0].qs).to.deep.equal({draft: true});
+ });
});
});
describe('create Method', function() {
- it('should call client post method with the appropriate uri', function(done) {
- var options = {
- template: {
- id: "test"
- }
+ it('should call client post method with the appropriate uri and payload', function() {
+ var template = {
+ id: 'test'
};
- templates.create(options, function(err, data) {
- expect(client.post.firstCall.args[0].uri).to.equal('templates');
- done();
- });
+ return templates.create(template)
+ .then(function() {
+ expect(client.post.firstCall.args[0].uri).to.equal('templates');
+ expect(client.post.firstCall.args[0].json).to.deep.equal(template);
+ });
});
- it('should throw an error if id is missing', function(done) {
- templates.create(null, function(err) {
- expect(err.message).to.equal('template object is required');
- expect(client.post).not.to.have.been.called;
- done();
- });
+ it('should throw an error if template object is missing', function() {
+ return expect(templates.create()).to.be.rejectedWith('template object is required');
});
});
describe('update Method', function() {
- it('should call client put method with the appropriate uri', function(done) {
- var options = {
- template: {
- id: "test"
- }
- };
-
- templates.update(options, function(err, data) {
- expect(client.put.firstCall.args[0].uri).to.equal('templates/test');
- done();
- });
+ it('should call client put method with the appropriate uri and payload', function() {
+ var id = 'test'
+ , template = {
+ name: 'A new name!'
+ };
+
+ return templates.update(id, template)
+ .then(function() {
+ expect(client.put.firstCall.args[0].uri).to.equal('templates/test');
+ expect(client.put.firstCall.args[0].json).to.deep.equal(template);
+ });
});
- it('should throw an error if id is missing', function(done) {
- templates.update(null, function(err) {
- expect(err.message).to.equal('template object is required');
- expect(client.put).not.to.have.been.called;
- done();
- });
+ it('should throw an error if template id is missing', function() {
+ return expect(templates.update()).to.be.rejectedWith('template id is required');
});
- it('should allow update_published to be set in options', function(done) {
- var options = {
- template: {
- id: "test"
- },
- update_published: true
- };
+ it('should throw an error if template object is missing', function() {
+ return expect(templates.update('test')).to.be.rejectedWith('template object is required');
+ });
- templates.update(options, function(err, data) {
- expect(client.put.firstCall.args[0].qs).to.deep.equal({update_published: true});
- done();
+ it('should not throw an error if optional 3nd argument is a function (callback)', function() {
+ let cb = sinon.stub()
+ , id = 'test'
+ , template = {
+ name: 'A new name!'
+ };
+ return templates.update(id, template, cb).then(function() {
+ expect(cb.callCount).to.equal(1);
});
});
- });
- describe('delete Method', function() {
- it('should call client delete method with the appropriate uri', function(done) {
- templates['delete']('test', function(err, data) {
- expect(client['delete'].firstCall.args[0].uri).to.equal('templates/test');
- done();
- });
+ it('should allow update_published to be set in options', function() {
+ var id = 'test'
+ , template = {
+ name: 'Test Template'
+ }
+ , options = {
+ update_published: true
+ };
+
+ return templates.update(id, template, options)
+ .then(function() {
+ expect(client.put.firstCall.args[0].qs).to.deep.equal(options);
+ });
});
+ });
- it('should throw an error if id is null', function(done) {
- templates['delete'](null, function(err) {
- expect(err.message).to.equal('template id is required');
- expect(client['delete']).not.to.have.been.called;
- done();
- });
+ describe('delete Method', function() {
+ it('should call client delete method with the appropriate uri', function() {
+ return templates.delete('test')
+ .then(function() {
+ expect(client.delete.firstCall.args[0].uri).to.equal('templates/test');
+ });
});
- it('should throw an error if id is missing', function(done) {
- templates['delete'](function(err) {
- expect(err.message).to.equal('template id is required');
- expect(client['delete']).not.to.have.been.called;
- done();
- });
+ it('should throw an error if id is missing', function() {
+ return expect(templates.delete()).to.be.rejectedWith('template id is required');
});
});
describe('preview Method', function() {
- it('should call client post method with the appropriate uri', function(done) {
- var options = {
- id: 'test'
- };
- templates.preview(options, function(err, data) {
- expect(client.post.firstCall.args[0].uri).to.equal('templates/test/preview');
- done();
- });
+ it('should call client post method with the appropriate uri and payload', function() {
+ var id = 'test'
+ , options = {
+ substitution_data: {
+ 'name': 'Natalie',
+ 'age': 35,
+ 'member': true
+ }
+ };
+ return templates.preview(id, options)
+ .then(function() {
+ expect(client.post.firstCall.args[0].uri).to.equal('templates/test/preview');
+ expect(client.post.firstCall.args[0].json).to.deep.equal(options);
+ });
});
- it('should throw an error if id is missing', function(done) {
- templates.preview(null, function(err) {
- expect(err.message).to.equal('template id is required');
- expect(client.post).not.to.have.been.called;
- done();
+ it('should throw an error if id is missing', function() {
+ return expect(templates.preview()).to.be.rejectedWith('template id is required');
+ });
+
+ it('should not throw an error if optional 2nd argument is a function (callback)', function() {
+ let cb = sinon.stub();
+ return templates.preview('test', cb).then(function() {
+ expect(cb.callCount).to.equal(1);
});
});
- it('should allow draft to be set in options', function(done) {
- var options = {
- id: 'test',
- draft: true
- };
+ it('should allow draft to be set in options', function() {
+ var id = 'test'
+ , options = {
+ draft: true
+ };
- templates.preview(options, function(err, data) {
- expect(client.post.firstCall.args[0].qs).to.deep.equal({draft: true});
- done();
- });
+ return templates.preview(id, options)
+ .then(function() {
+ expect(client.post.firstCall.args[0].qs).to.deep.equal({draft: true});
+ });
});
});
});