From 5b9b13e4ddf6559dc66cb2c9c4836857c4a0812c Mon Sep 17 00:00:00 2001 From: Andrew Milenic Date: Fri, 30 Aug 2019 20:53:17 -0700 Subject: [PATCH] updated support:tickets --- README.md | 45 +- src/api/v2/routes/support/tickets.js | 486 ++++++++++----- src/api/v2/validators/support/tickets.js | 48 ++ .../src/api/v2/routes/support/tickets.test.js | 588 ++++++++++++------ 4 files changed, 804 insertions(+), 363 deletions(-) create mode 100644 src/api/v2/validators/support/tickets.js diff --git a/README.md b/README.md index 02cbc41..d6f95d6 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,7 @@ A JS library for interacting with the Zendesk API. - Generates Zendesk API request objects 😎 - Send prepared object directly to axios 🤯 -- Mirrors [Zendesk's API documentation](https://developer.zendesk.com/rest_api/docs/zendesk-apis/resources) _(as close as feasible)_ 🤓 -- [@hapi/joi](https://www.npmjs.com/package/@hapi/joi) used for data validation 🔐 -- 🚧 Under construction 🚧 - Watch for updates 👀 +- Mirrors [Zendesk's API documentation](https://developer.zendesk.com/rest_api/docs/zendesk-apis/resources) 👀 ## Installing @@ -80,49 +78,52 @@ const res = await axios(req); ### Support API -- [x] Search -- [x] Users +- [x] **Search** +- [x] **Users** +- [x] **Tickets** +- [x] **Ticket Metrics** +- [x] **Tags** + +#### Under Construction + +- [ ] Views +- [ ] Organizations +- [ ] Groups +- [ ] Ticket Forms +- [ ] Ticket Fields +- [ ] User Fields +- [ ] Organization Fields +- [ ] Brands - [ ] User Identities - [ ] User Passwords - [ ] End Users -- [ ] Groups - [ ] Group Memberships - [ ] Custom Agent Roles -- [ ] Organizations - [ ] Organization Subscriptions - [ ] Organization Memberships - [ ] Requests -- [x] Tickets -- [ ] Ticket Import -- [ ] Attachments -- [ ] Satisfaction Ratings -- [ ] Satisfaction Reasons -- [ ] Suspended Tickets - [ ] Ticket Audits - [ ] Ticket Comments - [ ] Ticket Skips -- [x] Ticket Metrics - [ ] Ticket Metric Events - [ ] Ticket Activities +- [ ] Ticket Import +- [ ] Attachments +- [ ] Satisfaction Ratings +- [ ] Satisfaction Reasons +- [ ] Suspended Tickets - [ ] Sessions -- [x] Tags -- [ ] Views - [ ] Triggers - [ ] Automations - [ ] SLA Policies - [ ] Targets - [ ] Target Failures - [ ] Macros -- [ ] Brands - [ ] Dynamic Content - [ ] Locales - [ ] Schedules - [ ] Sharing Agreements - [ ] Support Addresses -- [ ] Ticket Forms -- [ ] Ticket Fields -- [ ] User Fields -- [ ] Organization Fields - [ ] Job Statuses - [ ] Skill-based Routing - [ ] Incremental Skill-based Routing @@ -142,7 +143,7 @@ const res = await axios(req); - [ ] Channel Framework - [ ] Twitter Channel -### ToDo +### APIs Up Next - Sunshine API - Help Center API diff --git a/src/api/v2/routes/support/tickets.js b/src/api/v2/routes/support/tickets.js index 636e389..e5a729a 100644 --- a/src/api/v2/routes/support/tickets.js +++ b/src/api/v2/routes/support/tickets.js @@ -1,165 +1,321 @@ -module.exports = ({ instance, headers }) => ({ - list: ({ type, id = 0 } = { type: 'tickets', id: 0 }) => ({ - method: 'GET', - url: { - tickets: `https://${instance}.zendesk.com/api/v2/tickets.json`, - organizations: `https://${instance}.zendesk.com/api/v2/organizations/${id}/tickets.json`, - users_requested: `https://${instance}.zendesk.com/api/v2/users/${id}/tickets/requested.json`, - users_ccd: `https://${instance}.zendesk.com/api/v2/users/${id}/tickets/ccd.json`, - users_assigned: `https://${instance}.zendesk.com/api/v2/users/${id}/tickets/assigned.json`, - recent: `https://${instance}.zendesk.com/api/v2/tickets/recent.json` - }[type], - headers - }), - - list_by_external_id: ({ external_id }) => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets.json?external_id=${external_id}`, - headers - }), - - show: ({ id }) => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}.json`, - headers - }), - - show_many: ({ ids }) => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/show_many.json?ids=${ids}`, - headers - }), - - create: ({ data }) => ({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets.json`, - headers, - data - }), - - create_many: ({ data }) => ({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets/create_many.json`, - headers, - data - }), - - update: ({ id, data }) => ({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}.json`, - headers, - data - }), - - update_many: ({ ids = '', data }) => ({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/update_many.json${ - ids ? `?ids=${ids}` : '' - }`, - headers, - data - }), - - mark_as_spam: ({ id }) => ({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/mark_as_spam.json`, - headers - }), - - mark_as_spam_bulk: ({ ids }) => ({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/mark_many_as_spam.json?ids=${ids}`, - headers - }), - - merge: ({ id, data }) => ({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/merge.json`, - headers, - data - }), - - related: ({ id }) => ({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/related.json`, - headers - }), - - delete: ({ id }) => ({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}.json`, - headers - }), - - delete_bulk: ({ ids }) => ({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/tickets/destroy_many.json?ids=${ids}`, - headers - }), - - deleted: () => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets.json`, - headers - }), - - restore: ({ id }) => ({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/${id}/restore.json`, - headers - }), - - restore_bulk: ({ ids }) => ({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/restore_many?ids=${ids}`, - headers - }), - - delete_permanently: ({ id }) => ({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/${id}.json`, - headers - }), - - delete_permanently_bulk: ({ ids }) => ({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/destroy_many?ids=${ids}`, - headers - }), - - collaborators: ({ id }) => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/collaborators.json`, - headers - }), - - followers: ({ id }) => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/followers.json`, - headers - }), - - email_ccs: ({ id }) => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/email_ccs.json`, - headers - }), - - incidents: ({ id }) => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/incidents.json`, - headers - }), - - problems: () => ({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/problems.json`, - headers - }), - - autocomplete_problems: ({ name }) => ({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/problems/autocomplete.json?text=${name}`, - headers - }) -}); +const validate = require('../../validators/support/tickets'); + +module.exports = ({ instance, headers }) => { + const url = `https://${instance}.zendesk.com`; + + return { + list: (options = {}) => { + const { error } = validate.list(options); + if (error) throw new Error(error.details[0].message); + + const { type = 'tickets', id = 0 } = options; + if (id && (type === 'tickets' || type === 'recent')) + throw new Error( + 'if "id" is set, type cannont be "tickets" or "recent"' + ); + + return { + method: 'GET', + url: { + tickets: `${url}/api/v2/tickets.json`, + organizations: `${url}/api/v2/organizations/${id}/tickets.json`, + users_requested: `${url}/api/v2/users/${id}/tickets/requested.json`, + users_ccd: `${url}/api/v2/users/${id}/tickets/ccd.json`, + users_assigned: `${url}/api/v2/users/${id}/tickets/assigned.json`, + recent: `${url}/api/v2/tickets/recent.json` + }[type], + headers + }; + }, + + list_by_external_id: (options = {}) => { + const { error } = validate.list_by_external_id(options); + if (error) throw new Error(error.details[0].message); + + const { external_id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets.json?external_id=${external_id}`, + headers + }; + }, + + show: (options = {}) => { + const { error } = validate.show(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/${id}.json`, + headers + }; + }, + + show_many: (options = {}) => { + const { error } = validate.show_many(options); + if (error) throw new Error(error.details[0].message); + + const { ids } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/show_many.json?ids=${ids}`, + headers + }; + }, + + create: (options = {}) => { + const { error } = validate.create(options); + if (error) throw new Error(error.details[0].message); + + const { data } = options; + return { + method: 'POST', + url: `${url}/api/v2/tickets.json`, + headers, + data + }; + }, + + create_many: (options = {}) => { + const { error } = validate.create_many(options); + if (error) throw new Error(error.details[0].message); + + const { data } = options; + return { + method: 'POST', + url: `${url}/api/v2/tickets/create_many.json`, + headers, + data + }; + }, + + update: (options = {}) => { + const { error } = validate.update(options); + if (error) throw new Error(error.details[0].message); + + const { id, data } = options; + return { + method: 'PUT', + url: `${url}/api/v2/tickets/${id}.json`, + headers, + data + }; + }, + + update_many: (options = {}) => { + const { error } = validate.update_many(options); + if (error) throw new Error(error.details[0].message); + + const { ids = '', data } = options; + const params = ids ? `?ids=${ids}` : ''; + return { + method: 'PUT', + url: `${url}/api/v2/tickets/update_many.json${params}`, + headers, + data + }; + }, + + mark_as_spam: (options = {}) => { + const { error } = validate.mark_as_spam(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'PUT', + url: `${url}/api/v2/tickets/${id}/mark_as_spam.json`, + headers + }; + }, + + mark_as_spam_bulk: (options = {}) => { + const { error } = validate.mark_as_spam_bulk(options); + if (error) throw new Error(error.details[0].message); + + const { ids } = options; + return { + method: 'PUT', + url: `${url}/api/v2/tickets/mark_many_as_spam.json?ids=${ids}`, + headers + }; + }, + + merge: (options = {}) => { + const { error } = validate.merge(options); + if (error) throw new Error(error.details[0].message); + + const { id, data } = options; + return { + method: 'POST', + url: `${url}/api/v2/tickets/${id}/merge.json`, + headers, + data + }; + }, + + related: (options = {}) => { + const { error } = validate.related(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'POST', + url: `${url}/api/v2/tickets/${id}/related.json`, + headers + }; + }, + + delete: (options = {}) => { + const { error } = validate.delete(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/tickets/${id}.json`, + headers + }; + }, + + delete_bulk: (options = {}) => { + const { error } = validate.delete_bulk(options); + if (error) throw new Error(error.details[0].message); + + const { ids } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/tickets/destroy_many.json?ids=${ids}`, + headers + }; + }, + + deleted: (options = null) => { + if (options) throw new Error('no options are allowed'); + + return { + method: 'GET', + url: `${url}/api/v2/deleted_tickets.json`, + headers + }; + }, + + restore: (options = {}) => { + const { error } = validate.restore(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'PUT', + url: `${url}/api/v2/deleted_tickets/${id}/restore.json`, + headers + }; + }, + + restore_bulk: (options = {}) => { + const { error } = validate.restore_bulk(options); + if (error) throw new Error(error.details[0].message); + + const { ids } = options; + return { + method: 'PUT', + url: `${url}/api/v2/deleted_tickets/restore_many?ids=${ids}`, + headers + }; + }, + + delete_permanently: (options = {}) => { + const { error } = validate.delete_permanently(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/deleted_tickets/${id}.json`, + headers + }; + }, + + delete_permanently_bulk: (options = {}) => { + const { error } = validate.delete_permanently_bulk(options); + if (error) throw new Error(error.details[0].message); + + const { ids } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/deleted_tickets/destroy_many?ids=${ids}`, + headers + }; + }, + + collaborators: (options = {}) => { + const { error } = validate.collaborators(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/${id}/collaborators.json`, + headers + }; + }, + + followers: (options = {}) => { + const { error } = validate.followers(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/${id}/followers.json`, + headers + }; + }, + + email_ccs: (options = {}) => { + const { error } = validate.email_ccs(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/${id}/email_ccs.json`, + headers + }; + }, + + incidents: (options = {}) => { + const { error } = validate.incidents(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/${id}/incidents.json`, + headers + }; + }, + + problems: (options = null) => { + if (options) throw new Error('no options are allowed'); + + return { + method: 'GET', + url: `${url}/api/v2/problems.json`, + headers + }; + }, + + autocomplete_problems: (options = {}) => { + const { error } = validate.autocomplete_problems(options); + if (error) throw new Error(error.details[0].message); + + const { name } = options; + return { + method: 'POST', + url: `${url}/api/v2/problems/autocomplete.json?text=${name}`, + headers + }; + } + }; +}; diff --git a/src/api/v2/validators/support/tickets.js b/src/api/v2/validators/support/tickets.js new file mode 100644 index 0000000..1add1a1 --- /dev/null +++ b/src/api/v2/validators/support/tickets.js @@ -0,0 +1,48 @@ +const Joi = require('@hapi/joi'); + +const type = Joi.string().valid( + 'tickets', + 'organizations', + 'users_requested', + 'users_ccd', + 'users_assigned', + 'recent' +); +const id = Joi.number().min(1); +const ids = Joi.string().min(3); +const external_id = Joi.string().min(1); +const name = Joi.string().min(1); +const data = Joi.object(); + +module.exports = { + list: options => Joi.validate(options, { type, id }), + list_by_external_id: options => + Joi.validate(options, { external_id: external_id.required() }), + show: options => Joi.validate(options, { id: id.required() }), + show_many: options => Joi.validate(options, { ids: ids.required() }), + create: options => Joi.validate(options, { data: data.required() }), + create_many: options => Joi.validate(options, { data: data.required() }), + update: options => + Joi.validate(options, { id: id.required(), data: data.required() }), + update_many: options => Joi.validate(options, { ids, data: data.required() }), + mark_as_spam: options => Joi.validate(options, { id: id.required() }), + mark_as_spam_bulk: options => Joi.validate(options, { ids: ids.required() }), + merge: options => + Joi.validate(options, { id: id.required(), data: data.required() }), + related: options => Joi.validate(options, { id: id.required() }), + delete: options => Joi.validate(options, { id: id.required() }), + delete_bulk: options => Joi.validate(options, { ids: ids.required() }), + deleted: null, // no options + restore: options => Joi.validate(options, { id: id.required() }), + restore_bulk: options => Joi.validate(options, { ids: ids.required() }), + delete_permanently: options => Joi.validate(options, { id: id.required() }), + delete_permanently_bulk: options => + Joi.validate(options, { ids: ids.required() }), + collaborators: options => Joi.validate(options, { id: id.required() }), + followers: options => Joi.validate(options, { id: id.required() }), + email_ccs: options => Joi.validate(options, { id: id.required() }), + incidents: options => Joi.validate(options, { id: id.required() }), + problems: null, // no options + autocomplete_problems: options => + Joi.validate(options, { name: name.required() }) +}; diff --git a/tests/src/api/v2/routes/support/tickets.test.js b/tests/src/api/v2/routes/support/tickets.test.js index 856cb04..0943a05 100644 --- a/tests/src/api/v2/routes/support/tickets.test.js +++ b/tests/src/api/v2/routes/support/tickets.test.js @@ -1,268 +1,504 @@ const endpoint = require('../../../../../../src/api/v2/routes/support/tickets'); const instance = 'instance'; -const headers = {}; +const url = `https://${instance}.zendesk.com`; +const headers = { + 'Content-Type': 'application/json', + Authorization: 'Basic <64bit_encoded_credentials>' +}; describe('tickets', () => { - let tickets, id, ids, external_id, data, name; - - beforeEach(() => { - tickets = endpoint({ instance, headers }); - id = 123; - ids = '1,2,3'; - external_id = 456; - data = { test: 'data' }; - name = 'name'; - }); + let tickets; + + beforeEach(() => (tickets = endpoint({ instance, headers }))); + afterEach(() => (tickets = null)); + + describe('list tickets', () => { + it('should process w/ valid input', () => { + expect(tickets.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets.json`, + headers + }); + + expect(tickets.list({ type: 'organizations', id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/organizations/123/tickets.json`, + headers + }); + + expect(tickets.list({ type: 'users_requested', id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/users/123/tickets/requested.json`, + headers + }); + + expect(tickets.list({ type: 'users_ccd', id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/users/123/tickets/ccd.json`, + headers + }); + + expect(tickets.list({ type: 'users_assigned', id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/users/123/tickets/assigned.json`, + headers + }); + + expect(tickets.list({ type: 'recent' })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/recent.json`, + headers + }); + }); - afterEach(() => { - tickets = null; - id = 0; - ids = ''; - external_id = 0; - data = null; - name = ''; + it('should throw error w/ invalid input', () => { + expect(() => tickets.list('invalid')).toThrowError(); + expect(() => tickets.list({ type: 'invalid' })).toThrowError(); + expect(() => + tickets.list({ type: 'tickets', id: 'invalid' }) + ).toThrowError(); + expect(() => tickets.list({ type: 'tickets', id: 123 })).toThrowError(); + }); }); - test('list tickets', () => { - expect(tickets.list()).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets.json`, - headers + describe('list by external id', () => { + it('should process w/ valid input', () => { + expect(tickets.list_by_external_id({ external_id: '123' })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets.json?external_id=123`, + headers + }); }); - expect(tickets.list({ type: 'organizations', id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/organizations/${id}/tickets.json`, - headers + it('should throw error w/ invalid input', () => { + expect(() => tickets.list_by_external_id()).toThrowError(); + expect(() => tickets.list_by_external_id({})).toThrowError(); + expect(() => + tickets.list_by_external_id({ external_id: 123 }) + ).toThrowError(); }); + }); - expect(tickets.list({ type: 'users_requested', id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/users/${id}/tickets/requested.json`, - headers + describe('show', () => { + it('should process w/ valid input', () => { + expect(tickets.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123.json`, + headers + }); }); - expect(tickets.list({ type: 'users_ccd', id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/users/${id}/tickets/ccd.json`, - headers + it('should throw error w/ invalid input', () => { + expect(() => tickets.show()).toThrowError(); + expect(() => tickets.show({})).toThrowError(); + expect(() => tickets.show({ id: 'invalid' })).toThrowError(); }); + }); - expect(tickets.list({ type: 'users_assigned', id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/users/${id}/tickets/assigned.json`, - headers + describe('show many', () => { + it('should process w/ valid input', () => { + expect(tickets.show_many({ ids: '1,2,3' })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/show_many.json?ids=1,2,3`, + headers + }); }); - expect(tickets.list({ type: 'recent' })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/recent.json`, - headers + it('should throw error w/ invalid input', () => { + expect(() => tickets.show_many()).toThrowError(); + expect(() => tickets.show_many({})).toThrowError(); + expect(() => tickets.show_many({ ids: 0 })).toThrowError(); }); }); - test('list by external id', () => { - expect(tickets.list_by_external_id({ external_id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets.json?external_id=${external_id}`, - headers + describe('create', () => { + it('should process w/ valid input', () => { + expect(tickets.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/tickets.json`, + headers, + data: {} + }); }); - }); - test('show', () => { - expect(tickets.show({ id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}.json`, - headers + it('should throw error w/ invalid input', () => { + expect(() => tickets.create()).toThrowError(); + expect(() => tickets.create('invalid')).toThrowError(); + expect(() => tickets.create({})).toThrowError(); + expect(() => tickets.create({ data: 'invalid' })).toThrowError(); }); }); - test('show many', () => { - expect(tickets.show_many({ ids })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/show_many.json?ids=${ids}`, - headers + describe('create many', () => { + it('should process w/ valid input', () => { + expect(tickets.create_many({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/tickets/create_many.json`, + headers, + data: {} + }); }); - }); - test('create', () => { - expect(tickets.create({ data })).toEqual({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets.json`, - headers, - data + it('should throw error w/ invalid input', () => { + expect(() => tickets.create_many()).toThrowError(); + expect(() => tickets.create_many('invalid')).toThrowError(); + expect(() => tickets.create_many({})).toThrowError(); + expect(() => tickets.create_many({ data: 123 })).toThrowError(); }); }); - test('create many', () => { - expect(tickets.create_many({ data })).toEqual({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets/create_many.json`, - headers, - data + describe('update', () => { + it('should process w/ valid input', () => { + expect(tickets.update({ id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/tickets/123.json`, + headers, + data: {} + }); }); - }); - test('update', () => { - expect(tickets.update({ id, data })).toEqual({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}.json`, - headers, - data + it('should throw error w/ invalid input', () => { + expect(() => tickets.update()).toThrowError(); + expect(() => tickets.update({})).toThrowError(); + expect(() => tickets.update({ data: {} })).toThrowError(); + expect(() => tickets.update({ id: 'invalid', data: {} })).toThrowError(); + expect(() => tickets.update({ id: 123, data: 'invalid' })).toThrowError(); }); }); - test('update many', () => { - expect(tickets.update_many({ data })).toEqual({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/update_many.json`, - headers, - data + describe('update many', () => { + it('should process w/ valid input', () => { + expect(tickets.update_many({ data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/tickets/update_many.json`, + headers, + data: {} + }); + + expect(tickets.update_many({ ids: '1,2,3', data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/tickets/update_many.json?ids=1,2,3`, + headers, + data: {} + }); }); - expect(tickets.update_many({ ids, data })).toEqual({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/update_many.json?ids=${ids}`, - headers, - data + it('should throw error w/ invalid input', () => { + expect(() => tickets.update_many()).toThrowError(); + expect(() => tickets.update_many('invalid')).toThrowError(); + expect(() => tickets.update_many({})).toThrowError(); + expect(() => tickets.update_many({ data: 'invalid' })).toThrowError(); + expect(() => tickets.update_many({ ids: 0, data: {} })).toThrowError(); }); }); - test('mark as spam', () => { - expect(tickets.mark_as_spam({ id })).toEqual({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/mark_as_spam.json`, - headers + describe('mark as spam', () => { + it('should process w/ valid input', () => { + expect(tickets.mark_as_spam({ id: 123 })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/tickets/123/mark_as_spam.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.mark_as_spam()).toThrowError(); + expect(() => tickets.mark_as_spam('invalid')).toThrowError(); + expect(() => tickets.mark_as_spam({})).toThrowError(); + expect(() => tickets.mark_as_spam({ id: 0 })).toThrowError(); + expect(() => tickets.mark_as_spam({ id: 'invalid' })).toThrowError(); }); }); - test('mark as spam bulk', () => { - expect(tickets.mark_as_spam_bulk({ ids })).toEqual({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/tickets/mark_many_as_spam.json?ids=${ids}`, - headers + describe('mark as spam bulk', () => { + it('should process w/ valid input', () => { + expect(tickets.mark_as_spam_bulk({ ids: '1,2,3' })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/tickets/mark_many_as_spam.json?ids=1,2,3`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.mark_as_spam_bulk()).toThrowError(); + expect(() => tickets.mark_as_spam_bulk('invalid')).toThrowError(); + expect(() => tickets.mark_as_spam_bulk({})).toThrowError(); + expect(() => tickets.mark_as_spam_bulk({ id: 0 })).toThrowError(); + expect(() => tickets.mark_as_spam_bulk({ id: 'invalid' })).toThrowError(); }); }); - test('merge', () => { - expect(tickets.merge({ id, data })).toEqual({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/merge.json`, - headers, - data + describe('merge', () => { + it('should process w/ valid input', () => { + expect(tickets.merge({ id: 123, data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/tickets/123/merge.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.merge()).toThrowError(); + expect(() => tickets.merge('invalid')).toThrowError(); + expect(() => tickets.merge({})).toThrowError(); + expect(() => tickets.merge({ id: 'invalid' })).toThrowError(); + expect(() => tickets.merge({ id: 0 })).toThrowError(); + expect(() => tickets.merge({ id: 123, data: 'invalid' })).toThrowError(); }); }); - test('related', () => { - expect(tickets.related({ id })).toEqual({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/related.json`, - headers + describe('related', () => { + it('should process w/ valid input', () => { + expect(tickets.related({ id: 123 })).toEqual({ + method: 'POST', + url: `${url}/api/v2/tickets/123/related.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.related()).toThrowError(); + expect(() => tickets.related('invalid')).toThrowError(); + expect(() => tickets.related({})).toThrowError(); + expect(() => tickets.related({ id: 'invalid' })).toThrowError(); + expect(() => tickets.related({ id: 0 })).toThrowError(); + expect(() => + tickets.related({ id: 123, data: 'invalid' }) + ).toThrowError(); }); }); - test('delete', () => { - expect(tickets.delete({ id })).toEqual({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}.json`, - headers + describe('delete', () => { + it('should process w/ valid input', () => { + expect(tickets.delete({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/tickets/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.delete()).toThrowError(); + expect(() => tickets.delete('invalid')).toThrowError(); + expect(() => tickets.delete({})).toThrowError(); + expect(() => tickets.delete({ id: 'invalid' })).toThrowError(); + expect(() => tickets.delete({ id: 0 })).toThrowError(); + expect(() => tickets.delete({ id: 123, data: 'invalid' })).toThrowError(); }); }); - test('delete bulk', () => { - expect(tickets.delete_bulk({ ids })).toEqual({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/tickets/destroy_many.json?ids=${ids}`, - headers + describe('delete bulk', () => { + it('should process w/ valid input', () => { + expect(tickets.delete_bulk({ ids: '1,2,3' })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/tickets/destroy_many.json?ids=1,2,3`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.delete_bulk()).toThrowError(); + expect(() => tickets.delete_bulk('invalid')).toThrowError(); + expect(() => tickets.delete_bulk({})).toThrowError(); + expect(() => tickets.delete_bulk({ ids: 0 })).toThrowError(); }); }); - test('deleted', () => { - expect(tickets.deleted()).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets.json`, - headers + describe('deleted', () => { + it('should process w/ valid input', () => { + expect(tickets.deleted()).toEqual({ + method: 'GET', + url: `${url}/api/v2/deleted_tickets.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.deleted('invalid')).toThrowError(); + expect(() => tickets.deleted({})).toThrowError(); }); }); - test('restore', () => { - expect(tickets.restore({ id })).toEqual({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/${id}/restore.json`, - headers + describe('restore', () => { + it('should process w/ valid input', () => { + expect(tickets.restore({ id: 123 })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/deleted_tickets/123/restore.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.restore()).toThrowError(); + expect(() => tickets.restore('invalid')).toThrowError(); + expect(() => tickets.restore({})).toThrowError(); + expect(() => tickets.restore({ id: 0 })).toThrowError(); }); }); - test('restore bulk', () => { - expect(tickets.restore_bulk({ ids })).toEqual({ - method: 'PUT', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/restore_many?ids=${ids}`, - headers + describe('restore bulk', () => { + it('should process w/ valid input', () => { + expect(tickets.restore_bulk({ ids: '1,2,3' })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/deleted_tickets/restore_many?ids=1,2,3`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.restore_bulk()).toThrowError(); + expect(() => tickets.restore_bulk('invalid')).toThrowError(); + expect(() => tickets.restore_bulk({})).toThrowError(); + expect(() => tickets.restore_bulk({ ids: 0 })).toThrowError(); }); }); - test('delete permanently', () => { - expect(tickets.delete_permanently({ id })).toEqual({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/${id}.json`, - headers + describe('delete permanently', () => { + it('should process w/ valid input', () => { + expect(tickets.delete_permanently({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/deleted_tickets/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.delete_permanently()).toThrowError(); + expect(() => tickets.delete_permanently('invalid')).toThrowError(); + expect(() => tickets.delete_permanently({})).toThrowError(); + expect(() => tickets.delete_permanently({ id: 0 })).toThrowError(); + expect(() => + tickets.delete_permanently({ id: 'invalid' }) + ).toThrowError(); }); }); - test('delete permanently bulk', () => { - expect(tickets.delete_permanently_bulk({ ids })).toEqual({ - method: 'DELETE', - url: `https://${instance}.zendesk.com/api/v2/deleted_tickets/destroy_many?ids=${ids}`, - headers + describe('delete permanently bulk', () => { + it('should process w/ valid input', () => { + expect(tickets.delete_permanently_bulk({ ids: '1,2,3' })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/deleted_tickets/destroy_many?ids=1,2,3`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.delete_permanently_bulk()).toThrowError(); + expect(() => tickets.delete_permanently_bulk('invalid')).toThrowError(); + expect(() => tickets.delete_permanently_bulk({})).toThrowError(); + expect(() => tickets.delete_permanently_bulk({ ids: 0 })).toThrowError(); + expect(() => + tickets.delete_permanently_bulk({ id: 'invalid' }) + ).toThrowError(); }); }); - test('collaborators', () => { - expect(tickets.collaborators({ id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/collaborators.json`, - headers + describe('collaborators', () => { + it('should process w/ valid input', () => { + expect(tickets.collaborators({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/collaborators.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.collaborators()).toThrowError(); + expect(() => tickets.collaborators('invalid')).toThrowError(); + expect(() => tickets.collaborators({})).toThrowError(); + expect(() => tickets.collaborators({ id: 0 })).toThrowError(); + expect(() => tickets.collaborators({ id: 'invalid' })).toThrowError(); }); }); - test('followers', () => { - expect(tickets.followers({ id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/followers.json`, - headers + describe('followers', () => { + it('should process w/ valid input', () => { + expect(tickets.followers({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/followers.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.followers()).toThrowError(); + expect(() => tickets.followers('invalid')).toThrowError(); + expect(() => tickets.followers({})).toThrowError(); + expect(() => tickets.followers({ id: 0 })).toThrowError(); + expect(() => tickets.followers({ id: 'invalid' })).toThrowError(); }); }); - test('email ccs', () => { - expect(tickets.email_ccs({ id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/email_ccs.json`, - headers + describe('email ccs', () => { + it('should process w/ valid input', () => { + expect(tickets.email_ccs({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/email_ccs.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.email_ccs()).toThrowError(); + expect(() => tickets.email_ccs('invalid')).toThrowError(); + expect(() => tickets.email_ccs({})).toThrowError(); + expect(() => tickets.email_ccs({ id: 0 })).toThrowError(); + expect(() => tickets.email_ccs({ id: 'invalid' })).toThrowError(); }); }); - test('incidents', () => { - expect(tickets.incidents({ id })).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/tickets/${id}/incidents.json`, - headers + describe('incidents', () => { + it('should process w/ valid input', () => { + expect(tickets.incidents({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/incidents.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.incidents()).toThrowError(); + expect(() => tickets.incidents('invalid')).toThrowError(); + expect(() => tickets.incidents({})).toThrowError(); + expect(() => tickets.incidents({ id: 0 })).toThrowError(); + expect(() => tickets.incidents({ id: 'invalid' })).toThrowError(); }); }); - test('problems', () => { - expect(tickets.problems()).toEqual({ - method: 'GET', - url: `https://${instance}.zendesk.com/api/v2/problems.json`, - headers + describe('problems', () => { + it('should process w/ valid input', () => { + expect(tickets.problems()).toEqual({ + method: 'GET', + url: `${url}/api/v2/problems.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.problems('invalid')).toThrowError(); + expect(() => tickets.problems({})).toThrowError(); }); }); - test('autocomplete problems', () => { - expect(tickets.autocomplete_problems({ name })).toEqual({ - method: 'POST', - url: `https://${instance}.zendesk.com/api/v2/problems/autocomplete.json?text=${name}`, - headers + describe('autocomplete problems', () => { + it('should process w/ valid input', () => { + expect(tickets.autocomplete_problems({ name: 'name' })).toEqual({ + method: 'POST', + url: `${url}/api/v2/problems/autocomplete.json?text=name`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => tickets.autocomplete_problems()).toThrowError(); + expect(() => tickets.autocomplete_problems('invalid')).toThrowError(); + expect(() => tickets.autocomplete_problems({})).toThrowError(); + expect(() => tickets.autocomplete_problems({ id: 0 })).toThrowError(); + expect(() => + tickets.autocomplete_problems({ id: 'invalid' }) + ).toThrowError(); }); }); });