-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |