Skip to content

Commit

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

// Validation
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 {
/**
* Show Settings
*
* GET /api/v2/account/settings.json
* https://developer.zendesk.com/rest_api/docs/support/account_settings#show-settings
*/
show: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/v2/account/settings.json`,
headers
};
},

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

const { data } = options;
return {
method: 'PUT',
url: `${url}/api/v2/account/settings.json`,
headers,
data
};
}
};
};
61 changes: 61 additions & 0 deletions tests/src/api/support/account_settings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const endpoint = require('../../../../src/api/support/account_settings');
const { prepare } = require('../../../../src/utils/options');

describe('Account Settings', () => {
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('Show Settings', () => {
it('should process w/ valid input', () => {
expect(endPoint.show()).toEqual({
method: 'GET',
url: `${url}/api/v2/account/settings.json`,
headers
});
});
});

describe('Update Account Settings', () => {
it('should process w/ valid input', () => {
expect(endPoint.update({ data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/v2/account/settings.json`,
headers,
data: {}
});
});

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

0 comments on commit 23851e9

Please sign in to comment.