From c946c9f2de5abd8c3bbad37c3baf69513d9a6aa3 Mon Sep 17 00:00:00 2001 From: bc-yevhenii-buliuk Date: Tue, 31 Mar 2026 10:31:17 +0300 Subject: [PATCH] TRAC-303: add optional options param to getByName for multi-lang support --- spec/api/countries.spec.js | 58 ++++++++++++++++++++++++++++++++++++++ src/api/countries.js | 17 ++++++++--- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/spec/api/countries.spec.js b/spec/api/countries.spec.js index dd825bd..283bb20 100644 --- a/spec/api/countries.spec.js +++ b/spec/api/countries.spec.js @@ -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, + ); + }); + }); }); diff --git a/src/api/countries.js b/src/api/countries.js index 137c8f7..23c4701 100644 --- a/src/api/countries.js +++ b/src/api/countries.js @@ -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); } }