Skip to content

Commit 52b6ef6

Browse files
feat: TRAC-362 add optional requestOptions to itemAdd and handleItemAdd for locale-aware cart requests
1 parent 1010af5 commit 52b6ef6

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

spec/api/cart.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,44 @@ describe('Cart Api Class', () => {
7575
expect(cart.makeRequest).toHaveBeenCalled();
7676
});
7777

78+
it('should pass requestOptions baseUrl to makeRequest when provided to itemAdd', () => {
79+
const baseUrl = 'https://site.com/es';
80+
cart.itemAdd({}, jest.fn(), { baseUrl });
81+
82+
expect(cart.makeRequest).toHaveBeenCalledWith(
83+
expect.stringContaining('/cart/add'),
84+
'POST',
85+
expect.objectContaining({ baseUrl }),
86+
true,
87+
expect.any(Function),
88+
);
89+
});
90+
91+
it('should pass requestOptions baseUrl to makeRequest when provided to handleItemAdd', () => {
92+
const baseUrl = 'https://site.com/fr';
93+
cart.handleItemAdd({}, jest.fn(), { baseUrl });
94+
95+
expect(cart.makeRequest).toHaveBeenCalledWith(
96+
expect.stringContaining('/cart/add'),
97+
'POST',
98+
expect.objectContaining({ baseUrl }),
99+
true,
100+
expect.any(Function),
101+
);
102+
});
103+
104+
it('should work without requestOptions (backward compatibility)', () => {
105+
cart.itemAdd({}, jest.fn());
106+
107+
expect(cart.makeRequest).toHaveBeenCalledWith(
108+
expect.stringContaining('/cart/add'),
109+
'POST',
110+
expect.not.objectContaining({ baseUrl: expect.anything() }),
111+
true,
112+
expect.any(Function),
113+
);
114+
});
115+
78116
it('should be able to call api on item remove', () => {
79117
cart.itemRemove(1, jest.fn());
80118

src/api/cart.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,27 @@ export default class extends Base {
107107
*
108108
* @param {FormData} formData
109109
* @param {Function} callback
110+
* @param {Object} [requestOptions] - optional
110111
*/
111-
itemAdd(formData, callback) {
112+
itemAdd(formData, callback, requestOptions = {}) {
112113
this.handleItemAdd(formData, (err, response) => {
113114
if (!err) {
114115
this.getBodlEventsCart().emitAddItem(response);
115116
}
116117
callback(err, response);
117-
});
118+
}, requestOptions);
118119
}
119120

120121
/**
121122
* Add item to cart with options (variants)
122123
*
123124
* @param {FormData} formData
124125
* @param {Function} callback
126+
* @param {Object} [requestOptions] - optional
125127
*/
126-
handleItemAdd(formData, callback) {
127-
this.remoteRequest('/cart/add', 'POST', { formData }, (err, response) => {
128+
handleItemAdd(formData, callback, requestOptions = {}) {
129+
const { baseUrl } = requestOptions;
130+
this.remoteRequest('/cart/add', 'POST', { formData, baseUrl }, (err, response) => {
128131
const emitData = {
129132
err,
130133
response,

0 commit comments

Comments
 (0)