Skip to content
Snippets Groups Projects
Commit c45c74fd authored by Winnie Hellmann's avatar Winnie Hellmann
Browse files

Add users endpoint to frontend API class (!11374)

parent 7fb99697
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -137,6 +137,18 @@ const Api = {
.error(callback);
},
 
users(query, options) {
const url = Api.buildUrl(this.usersPath);
return Api.wrapAjaxCall({
url,
data: Object.assign({
search: query,
per_page: 20,
}, options),
dataType: 'json',
});
},
buildUrl(url) {
let urlRoot = '';
if (gon.relative_url_root != null) {
Loading
Loading
@@ -144,6 +156,20 @@ const Api = {
}
return urlRoot + url.replace(':version', gon.api_version);
},
wrapAjaxCall(options) {
return new Promise((resolve, reject) => {
// jQuery 2 is not Promises/A+ compatible (missing catch)
$.ajax(options) // eslint-disable-line promise/catch-or-return
.then(data => resolve(data),
(jqXHR, textStatus, errorThrown) => {
const error = new Error(`${options.url}: ${errorThrown}`);
error.textStatus = textStatus;
reject(error);
},
);
});
},
};
 
export default Api;
Loading
Loading
@@ -253,4 +253,29 @@ describe('Api', () => {
});
});
});
describe('users', () => {
it('fetches users', (done) => {
const query = 'dummy query';
const options = { unused: 'option' };
const expectedUrl = `${dummyUrlRoot}/api/${dummyApiVersion}/users.json`;
const expectedData = Object.assign({
search: query,
per_page: 20,
}, options);
spyOn(jQuery, 'ajax').and.callFake((request) => {
expect(request.url).toEqual(expectedUrl);
expect(request.dataType).toEqual('json');
expect(request.data).toEqual(expectedData);
return sendDummyResponse();
});
Api.users(query, options)
.then((response) => {
expect(response).toBe(dummyResponse);
})
.then(done)
.catch(done.fail);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment