Skip to content

Commit 790e942

Browse files
TRAC-303: add optional options param to getByName for multi-lang support
1 parent 30c7cbd commit 790e942

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

spec/api/countries.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,49 @@ describe('Countries', () => {
2323
});
2424
expect(country.remoteRequest).toHaveBeenCalled();
2525
});
26+
27+
describe('getByName', () => {
28+
let country;
29+
30+
beforeEach(() => {
31+
Base.prototype.remoteRequest = jest.fn();
32+
country = new Country();
33+
});
34+
35+
test('should call remoteRequest with correct url when called without options (legacy signature)', () => {
36+
const callback = jest.fn();
37+
38+
country.getByName('United States', callback);
39+
40+
expect(country.remoteRequest).toHaveBeenCalledWith(
41+
'/country-states/United States',
42+
'GET',
43+
{},
44+
callback,
45+
);
46+
});
47+
48+
test('should call remoteRequest with options when called with options object', () => {
49+
const callback = jest.fn();
50+
const options = { baseUrl: '/fr' };
51+
52+
country.getByName('United States', options, callback);
53+
54+
expect(country.remoteRequest).toHaveBeenCalledWith(
55+
'/country-states/United States',
56+
'GET',
57+
options,
58+
callback,
59+
);
60+
});
61+
62+
test('should pass baseUrl option through to remoteRequest for multi-lang support', () => {
63+
const callback = jest.fn();
64+
65+
country.getByName('United States', { baseUrl: '/fr' }, callback);
66+
67+
const [, , passedOptions] = country.remoteRequest.mock.calls[0];
68+
expect(passedOptions.baseUrl).toBe('/fr');
69+
});
70+
});
2671
});

src/api/countries.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ export default class extends Base {
2727

2828
/**
2929
* Get country data by country name
30-
* @param name
31-
* @param callback
30+
* @param {String} name
31+
* @param {Object} [options] - optional request options
32+
* @param {Function} callback
3233
*/
33-
getByName(name, callback) {
34+
getByName(name, options, callback) {
35+
if (typeof options === 'function') {
36+
callback = options;
37+
options = {};
38+
}
39+
3440
const url = this.endpoint + name;
3541

36-
this.remoteRequest(url, 'GET', {}, callback);
42+
this.remoteRequest(url, 'GET', options, callback);
3743
}
3844
}

0 commit comments

Comments
 (0)