Skip to content

Commit

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

// Validation
const _search_string = Joi.string().min(1);

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

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

/**
* List Search Results
*
* GET /api/v2/search.json?query={search_string}
* https://developer.zendesk.com/rest_api/docs/support/search#list-search-results
*/
return {
list: (options = {}) => {
const { error } = Joi.object({
search_string: _search_string.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { search_string } = options;
return {
method: 'GET',
url: `${url}/api/v2/search.json?query=${search_string}`,
headers
};
},

/**
* Show Results Count
*
* GET /api/v2/search/count.json?query={search_string}
* https://developer.zendesk.com/rest_api/docs/support/search#show-results-count
*/
count: (options = {}) => {
const { error } = Joi.object({
search_string: _search_string.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { search_string } = options;
return {
method: 'GET',
url: `${url}/api/v2/search/count.json?query=${search_string}`,
headers
};
}
};
};
17 changes: 0 additions & 17 deletions src/api/support/search/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/api/support/search/validate.js

This file was deleted.

67 changes: 67 additions & 0 deletions tests/api/support/search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const endpoint = require('../../../src/api/support/search');
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 search results', () => {
it('should process w/ valid input', () => {
expect(endPoint.list({ search_string: 'valid' })).toEqual({
method: 'GET',
url: `${url}/api/v2/search.json?query=valid`,
headers
});
});

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

describe('show results count', () => {
it('should process valid input', () => {
expect(endPoint.count({ search_string: 'valid' }));
});

it('should fail with invalid input', () => {
expect(() => endPoint.count()).toThrowError();
expect(() => endPoint.count({})).toThrowError();
expect(() => endPoint.count('invalid')).toThrowError();
expect(() => endPoint.count({ search_string: 123 })).toThrowError();
expect(() => endPoint.count({ search_string: '' })).toThrowError();
});
});
});

0 comments on commit 86eded4

Please sign in to comment.