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
38 changes: 38 additions & 0 deletions spec/api/cart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,44 @@ describe('Cart Api Class', () => {
expect(cart.makeRequest).toHaveBeenCalled();
});

it('should pass requestOptions baseUrl to makeRequest when provided to itemAdd', () => {
const baseUrl = 'https://site.com/es';
cart.itemAdd({}, jest.fn(), { baseUrl });

expect(cart.makeRequest).toHaveBeenCalledWith(
expect.stringContaining('/cart/add'),
'POST',
expect.objectContaining({ baseUrl }),
true,
expect.any(Function),
);
});

it('should pass requestOptions baseUrl to makeRequest when provided to handleItemAdd', () => {
const baseUrl = 'https://site.com/fr';
cart.handleItemAdd({}, jest.fn(), { baseUrl });

expect(cart.makeRequest).toHaveBeenCalledWith(
expect.stringContaining('/cart/add'),
'POST',
expect.objectContaining({ baseUrl }),
true,
expect.any(Function),
);
});

it('should work without requestOptions (backward compatibility)', () => {
cart.itemAdd({}, jest.fn());

expect(cart.makeRequest).toHaveBeenCalledWith(
expect.stringContaining('/cart/add'),
'POST',
expect.not.objectContaining({ baseUrl: expect.anything() }),
true,
expect.any(Function),
);
});

it('should be able to call api on item remove', () => {
cart.itemRemove(1, jest.fn());

Expand Down
11 changes: 7 additions & 4 deletions src/api/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,27 @@ export default class extends Base {
*
* @param {FormData} formData
* @param {Function} callback
* @param {Object} [requestOptions] - optional
*/
itemAdd(formData, callback) {
itemAdd(formData, callback, requestOptions = {}) {
this.handleItemAdd(formData, (err, response) => {
if (!err) {
this.getBodlEventsCart().emitAddItem(response);
}
callback(err, response);
});
}, requestOptions);
}

/**
* Add item to cart with options (variants)
*
* @param {FormData} formData
* @param {Function} callback
* @param {Object} [requestOptions] - optional
*/
handleItemAdd(formData, callback) {
this.remoteRequest('/cart/add', 'POST', { formData }, (err, response) => {
handleItemAdd(formData, callback, requestOptions = {}) {
const { baseUrl } = requestOptions;
this.remoteRequest('/cart/add', 'POST', { formData, baseUrl }, (err, response) => {
const emitData = {
err,
response,
Expand Down
Loading