Skip to content

Commit

Permalink
feat: Update getBuilds filter param to support array values (#149)
Browse files Browse the repository at this point in the history
* Update getBuilds filter param to support array values

* Use interpolation rather than concat
  • Loading branch information
timhaines committed Oct 28, 2019
1 parent 503a884 commit f596999
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,15 @@ class PercyClient {
getBuilds(project, filter) {
filter = filter || {};
let queryString = Object.keys(filter)
.map(key => 'filter[' + key + ']=' + filter[key])
.map(key => {
if (Array.isArray(filter[key])) {
// If filter value is an array, match Percy API's format expectations of:
// filter[key][]=value1&filter[key][]=value2
return filter[key].map(array_value => `filter[${key}][]=${array_value}`).join('&');
} else {
return 'filter[' + key + ']=' + filter[key];
}
})
.join('&');

if (queryString.length > 0) {
Expand Down
34 changes: 34 additions & 0 deletions test/main-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,40 @@ describe('PercyClient', function() {
});
});
});

describe('filtered by state, branch, and shas', function() {
it('returns the response body', function(done) {
let responseMock = function(url, requestBody) {
// Verify request data.
assert.equal(requestBody, '');
let responseBody = {foo: 123};
return [201, responseBody];
};

nock('https://percy.io')
.get(
'/api/v1/projects/my_project/builds?filter[branch]=master&filter[state]=finished' +
'&filter[shas][]=my_sha&filter[shas][]=my_other_sha',
)
.reply(201, responseMock);

let request = percyClient.getBuilds('my_project', {
branch: 'master',
state: 'finished',
shas: ['my_sha', 'my_other_sha'],
});

request
.then(response => {
assert.equal(response.statusCode, 201);
assert.deepEqual(response.body, {foo: 123});
done();
})
.catch(err => {
done(err);
});
});
});
});

describe('makeResource', function() {
Expand Down

0 comments on commit f596999

Please sign in to comment.