Skip to content

Commit

Permalink
converted object_types
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 23, 2019
1 parent 76ac2b9 commit 840e6e6
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 114 deletions.
136 changes: 136 additions & 0 deletions src/api/support/groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _id = Joi.number().min(1);
const _user_id = Joi.number().min(1);
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 {
/**
* List Groups
*
* GET /api/v2/groups.json
* GET /api/v2/users/{user_id}/groups.json
* https://developer.zendesk.com/rest_api/docs/support/groups#list-groups
*/
list: (options = {}) => {
const { error } = Joi.object({
user_id: _user_id
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { user_id } = options;
return {
method: 'GET',
url: `${url}/api/v2${user_id ? `/users/${user_id}` : ''}/groups.json`,
headers
};
},

/**
* Show assignable groups
*
* GET /api/v2/groups/assignable.json
* https://developer.zendesk.com/rest_api/docs/support/groups#show-assignable-groups
*/
show_assignable: () => {
// Ignore options
return {
method: 'GET',
url: `${url}/api/v2/groups/assignable.json`,
headers
};
},

/**
* Show Group
*
* GET /api/v2/groups/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/groups#show-group
*/
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/groups/${id}.json`,
headers
};
},

/**
* Create Group
*
* POST /api/v2/groups.json
* https://developer.zendesk.com/rest_api/docs/support/groups#create-group
*/
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/groups.json`,
headers,
data
};
},

/**
* Update Group
*
* PUT /api/v2/groups/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/groups#update-group
*/
update: (options = {}) => {
const { error } = Joi.object({
id: _id.required(),
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id, data } = options;
return {
method: 'PUT',
url: `${url}/api/v2/groups/${id}.json`,
headers,
data
};
},

/**
* Delete Group
*
* DELETE /api/v2/groups/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/groups#delete-group
*/
delete: (options = {}) => {
const { error } = Joi.object({
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id } = options;
return {
method: 'DELETE',
url: `${url}/api/v2/groups/${id}.json`,
headers
};
}
};
};
79 changes: 0 additions & 79 deletions src/api/support/groups/index.js

This file was deleted.

35 changes: 0 additions & 35 deletions src/api/support/groups/validate.js

This file was deleted.

146 changes: 146 additions & 0 deletions tests/api/support/groups.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const endpoint = require('../../../src/api/support/groups');
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', () => {
const ep = endpoint(options);
expect(ep).toBeTruthy();
});

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

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

expect(endPoint.list({})).toEqual({
method: 'GET',
url: `${url}/api/v2/groups.json`,
headers
});

expect(endPoint.list({ user_id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/users/123/groups.json`,
headers
});
});

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

describe('show assignable groups', () => {
it('should process without input', () => {
expect(endPoint.show_assignable()).toEqual({
method: 'GET',
url: `${url}/api/v2/groups/assignable.json`,
headers
});
});
});

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

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

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

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

describe('update group', () => {
it('should process w/ valid input', () => {
expect(endPoint.update({ id: 123, data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/v2/groups/123.json`,
headers,
data: {}
});
});

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

describe('delete group', () => {
it('should process w/ valid input', () => {
expect(endPoint.delete({ id: 123 })).toEqual({
method: 'DELETE',
url: `${url}/api/v2/groups/123.json`,
headers
});
});

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

0 comments on commit 840e6e6

Please sign in to comment.