From a20b40601f186037899c7849872d72806bbcaade Mon Sep 17 00:00:00 2001 From: androozka Date: Tue, 4 Feb 2020 09:32:50 -0800 Subject: [PATCH] built support:locales --- src/api/support/locales.js | 117 ++++++++++++++++++++++++++ tests/src/api/support/locales.test.js | 107 +++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 src/api/support/locales.js create mode 100644 tests/src/api/support/locales.test.js diff --git a/src/api/support/locales.js b/src/api/support/locales.js new file mode 100644 index 0000000..d78d831 --- /dev/null +++ b/src/api/support/locales.js @@ -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 + }; + } + }; +}; diff --git a/tests/src/api/support/locales.test.js b/tests/src/api/support/locales.test.js new file mode 100644 index 0000000..933ae86 --- /dev/null +++ b/tests/src/api/support/locales.test.js @@ -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(); + }); + }); +});