Skip to content

Commit

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

// Validation
const _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 Locales
*
* GET /api/v2/locales.json
* https://developer.zendesk.com/rest_api/docs/support/locales#list-locales
*/
list: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/v2/locales.json`,
headers
};
},

/**
* List Available Public Locales
*
* GET /api/v2/locales/public.json
* https://developer.zendesk.com/rest_api/docs/support/locales#list-available-public-locales
*/
public: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/v2/locales/public.json`,
headers
};
},

/**
* List Locales for Agent
*
* GET /api/v2/locales/agent.json
* https://developer.zendesk.com/rest_api/docs/support/locales#list-locales-for-agent
*/
agent: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/v2/locales/agent.json`,
headers
};
},

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

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

/**
* Detect best language for user
*
* GET /api/v2/locales/detect_best_locale.json
* https://developer.zendesk.com/rest_api/docs/support/locales#detect-best-language-for-user
*/
detect: (options = {}) => {
const { error } = Joi.object({
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

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

describe('Locales', () => {
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('List Locales', () => {
it('should process w/ valid input', () => {
expect(endPoint.list()).toEqual({
method: 'GET',
url: `${url}/api/v2/locales.json`,
headers
});
});
});

describe('List Available Public Locales', () => {
it('should process w/ valid input', () => {
expect(endPoint.public()).toEqual({
method: 'GET',
url: `${url}/api/v2/locales/public.json`,
headers
});
});
});

describe('List Locales for Agent', () => {
it('should process w/ valid input', () => {
expect(endPoint.agent()).toEqual({
method: 'GET',
url: `${url}/api/v2/locales/agent.json`,
headers
});
});
});

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

describe('Detect best language for user', () => {
it('should process w/ valid input', () => {
expect(endPoint.detect({ data: {} })).toEqual({
method: 'GET',
url: `${url}/api/v2/locales/detect_best_locale.json`,
headers,
data: {}
});
});

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

0 comments on commit a20b406

Please sign in to comment.