Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions spec/api/countries.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,62 @@ describe('Countries', () => {
});
expect(country.remoteRequest).toHaveBeenCalled();
});

describe('getByName', () => {
let country;

beforeEach(() => {
Base.prototype.remoteRequest = jest.fn();
country = new Country();
});

test('should call remoteRequest with correct url when called without options (legacy signature)', () => {
const callback = jest.fn();

country.getByName('United States', callback);

expect(country.remoteRequest).toHaveBeenCalledWith(
'/country-states/United States',
'GET',
{},
callback,
);
});

test('should call remoteRequest with options when called with options object', () => {
const callback = jest.fn();
const options = { baseUrl: '/fr' };

country.getByName('United States', options, callback);

expect(country.remoteRequest).toHaveBeenCalledWith(
'/country-states/United States',
'GET',
options,
callback,
);
});

test('should pass baseUrl option through to remoteRequest for multi-lang support', () => {
const callback = jest.fn();

country.getByName('United States', { baseUrl: '/fr' }, callback);

const [, , passedOptions] = country.remoteRequest.mock.calls[0];
expect(passedOptions.baseUrl).toBe('/fr');
});

test('should handle undefined options gracefully (null guard)', () => {
const callback = jest.fn();

country.getByName('United States', undefined, callback);

expect(country.remoteRequest).toHaveBeenCalledWith(
'/country-states/United States',
'GET',
{},
callback,
);
});
});
});
17 changes: 13 additions & 4 deletions src/api/countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ export default class extends Base {

/**
* Get country data by country name
* @param name
* @param callback
* @param {String} name
* @param {Object} [options] - optional request options
* @param {Function} callback
*/
getByName(name, callback) {
getByName(name, options, callback) {
let opts = options || {};
let callbackArg = callback;

if (typeof opts === 'function') {
callbackArg = opts;
opts = {};
}

const url = this.endpoint + name;

this.remoteRequest(url, 'GET', {}, callback);
this.remoteRequest(url, 'GET', opts, callbackArg);
}
}
Loading