Skip to content

Commit

Permalink
built support:twitter_channel
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Jan 25, 2020
1 parent c02e7cd commit cf221b1
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/api/support/twitter_channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _id = Joi.number().positive();
const _data = Joi.object();

// Initialize Endpoint
module.exports = (options = {}) => {
const { error } = validate(options);
if (error) throw new Error(error.details[0].message);

const { url, headers } = prepare(options);

return {
/**
* Listing Monitored Twitter Handles
*
* GET /api/v2/channels/twitter/monitored_twitter_handles.json
* https://developer.zendesk.com/rest_api/docs/support/twitter_channel#listing-monitored-twitter-handles
*/
list: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/v2/channels/twitter/monitored_twitter_handles.json`,
headers
};
},

/**
* Getting Monitored Twitter Handle
*
* GET /api/v2/channels/twitter/monitored_twitter_handles/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/twitter_channel#getting-monitored-twitter-handle
*/
show: (options = {}) => {
const { error } = Joi.object({
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id } = options;
return {
method: 'GET',
url: `${url}/api/v2/channels/twitter/monitored_twitter_handles/${id}.json`,
headers
};
},

/**
* Create Ticket from Tweet
*
* POST /api/v2/channels/twitter/tickets.json
* https://developer.zendesk.com/rest_api/docs/support/twitter_channel#create-ticket-from-tweet
*/
create: (options = {}) => {
const { error } = Joi.object({
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { data } = options;
return {
method: 'POST',
url: `${url}/api/v2/channels/twitter/tickets.json`,
headers,
data
};
},

/**
* Getting Twicket status
*
* GET /api/v2/channels/twitter/tickets/{id}/statuses.json
* https://developer.zendesk.com/rest_api/docs/support/twitter_channel#getting-twicket-status
*/
status: (options = {}) => {
const { error } = Joi.object({
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id } = options;
return {
method: 'GET',
url: `${url}/api/v2/channels/twitter/tickets/${id}/statuses.json`,
headers
};
}
};
};
94 changes: 94 additions & 0 deletions tests/src/api/support/twitter_channel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const endpoint = require('../../../../src/api/support/twitter_channel');
const { prepare } = require('../../../../src/utils/options');

describe('object types', () => {
let endPoint, options, url, headers;

beforeEach(() => {
options = {
instance: 'instance',
email: 'user@email.com',
token: 'token'
};
endPoint = endpoint(options);
({ url, headers } = prepare(options));
});

afterEach(() => {
options = null;
endPoint = null;
url = null;
headers = null;
});

describe('init', () => {
it('should setup endpoint object', () => {
expect(endpoint(options)).toBeTruthy();
});

it('should fail with invalid input', () => {
expect(() => endpoint()).toThrowError();
expect(() => endpoint({})).toThrowError();
});
});

describe('listing monitored twitter handles', () => {
it('should process w/ valid input', () => {
expect(endPoint.list()).toEqual({
method: 'GET',
url: `${url}/api/v2/channels/twitter/monitored_twitter_handles.json`,
headers
});
});
});

describe('getting monitored twitter handle', () => {
it('should process w/ valid input', () => {
expect(endPoint.show({ id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/channels/twitter/monitored_twitter_handles/123.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.show()).toThrowError();
expect(() => endPoint.show({})).toThrowError();
expect(() => endPoint.show({ id: 'invalid' })).toThrowError();
});
});

describe('create ticket from tweet', () => {
it('should process w/ valid input', () => {
expect(endPoint.create({ data: {} })).toEqual({
method: 'POST',
url: `${url}/api/v2/channels/twitter/tickets.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.create()).toThrowError();
expect(() => endPoint.create('invalid')).toThrowError();
expect(() => endPoint.create({})).toThrowError();
expect(() => endPoint.create({ data: 'invalid' })).toThrowError();
});
});

describe('getting twicket status', () => {
it('should process w/ valid input', () => {
expect(endPoint.status({ id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/channels/twitter/tickets/123/statuses.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.status()).toThrowError();
expect(() => endPoint.status({})).toThrowError();
expect(() => endPoint.status({ id: 'invalid' })).toThrowError();
});
});
});

0 comments on commit cf221b1

Please sign in to comment.