Skip to content

Commit

Permalink
feat: expose missing address endpoints in v1
Browse files Browse the repository at this point in the history
feat: add missing address endpoints
  • Loading branch information
pedro-gbf authored and jpklzm committed Jan 25, 2024
1 parent 7a59906 commit b14703e
Show file tree
Hide file tree
Showing 20 changed files with 755 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import join from 'proper-url-join';
import moxios from 'moxios';

export default {
success: params => {
moxios.stubRequest(
join('/api/account/v1/users', params.userId, 'addresses/billing/current'),
{
method: 'delete',
status: 204,
},
);
},
failure: params => {
moxios.stubRequest(
join('/api/account/v1/users', params.userId, 'addresses/billing/current'),
{
method: 'delete',
response: {
errors: [
{
code: 0,
message: 'error',
developerMessage: 'This is developer message',
moreInformation: 'Error more information',
exception: {},
},
],
},
status: 400,
},
);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import join from 'proper-url-join';
import moxios from 'moxios';

export default {
success: params => {
moxios.stubRequest(
join(
'/api/account/v1/users',
params.userId,
'addresses/shipping/current',
),
{
method: 'delete',
status: 204,
},
);
},
failure: params => {
moxios.stubRequest(
join(
'/api/account/v1/users',
params.userId,
'addresses/shipping/current',
),
{
method: 'delete',
response: {
errors: [
{
code: 0,
message: 'error',
developerMessage: 'This is developer message',
moreInformation: 'Error more information',
exception: {},
},
],
},
status: 400,
},
);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`deleteDefaultBillingAddress should receive a client request error 1`] = `
Object {
"code": 0,
"developerMessage": "This is developer message",
"exception": Object {},
"message": "error",
"moreInformation": "Error more information",
"status": 400,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`deleteDefaultShippingAddress should receive a client request error 1`] = `
Object {
"code": 0,
"developerMessage": "This is developer message",
"exception": Object {},
"message": "error",
"moreInformation": "Error more information",
"status": 400,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { deleteDefaultBillingAddress } from '../';
import client from '../../../helpers/client';
import fixture from '../__fixtures__/deleteDefaultBillingAddress.fixtures';
import moxios from 'moxios';

describe('deleteDefaultBillingAddress', () => {
const userId = '123456';
const expectedConfig = undefined;
const spy = jest.spyOn(client, 'delete');
const expectedUrl = `/account/v1/users/${userId}/addresses/billing/current`;

beforeEach(() => {
moxios.install(client);
jest.clearAllMocks();
});

afterEach(() => moxios.uninstall(client));

it('should handle a client request successfully', async () => {
fixture.success({ userId });

expect.assertions(2);

await expect(deleteDefaultBillingAddress(userId)).resolves.toBe(204);
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});

it('should receive a client request error', async () => {
fixture.failure({ userId });

expect.assertions(2);

await expect(deleteDefaultBillingAddress(userId)).rejects.toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { deleteDefaultShippingAddress } from '../';
import client from '../../../helpers/client';
import fixture from '../__fixtures__/deleteDefaultShippingAddress.fixtures';
import moxios from 'moxios';

describe('deleteDefaultShippingAddress', () => {
const userId = '123456';
const expectedConfig = undefined;
const spy = jest.spyOn(client, 'delete');
const expectedUrl = `/account/v1/users/${userId}/addresses/shipping/current`;

beforeEach(() => {
moxios.install(client);
jest.clearAllMocks();
});

afterEach(() => moxios.uninstall(client));

it('should handle a client request successfully', async () => {
fixture.success({ userId });

expect.assertions(2);

await expect(deleteDefaultShippingAddress(userId)).resolves.toBe(204);
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});

it('should receive a client request error', async () => {
fixture.failure({ userId });

expect.assertions(2);

await expect(
deleteDefaultShippingAddress(userId),
).rejects.toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(expectedUrl, expectedConfig);
});
});
26 changes: 26 additions & 0 deletions packages/core/src/addresses/client/deleteDefaultBillingAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import client, { adaptError } from '../../helpers/client';
import join from 'proper-url-join';

/**
* Responsible for unsetting the users default billing address.
*
* @function deleteDefaultBillingAddress
* @memberof module:addresses/client
*
* @param {string} userId - Identifier of the user.
* @param {object} [config] - Custom configurations to send to the client
* instance (axios).
*
* @returns {Promise} Promise that will resolve when the call to
* the endpoint finishes.
*/
export default (userId, config) =>
client
.delete(
join('/account/v1/users', userId, 'addresses/billing/current'),
config,
)
.then(response => response.status)
.catch(error => {
throw adaptError(error);
});
26 changes: 26 additions & 0 deletions packages/core/src/addresses/client/deleteDefaultShippingAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import client, { adaptError } from '../../helpers/client';
import join from 'proper-url-join';

/**
* Responsible for unsetting the users default shipping address.
*
* @function deleteDefaultShippingAddress
* @memberof module:addresses/client
*
* @param {string} userId - Identifier of the user.
* @param {object} [config] - Custom configurations to send to the client
* instance (axios).
*
* @returns {Promise} Promise that will resolve when the call to
* the endpoint finishes.
*/
export default (userId, config) =>
client
.delete(
join('/account/v1/users', userId, 'addresses/shipping/current'),
config,
)
.then(response => response.status)
.catch(error => {
throw adaptError(error);
});
14 changes: 8 additions & 6 deletions packages/core/src/addresses/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
export { default as getPredictions } from './getPredictions';
export { default as getPredictionDetails } from './getPredictionDetails';

export { default as getAddresses } from './getAddresses';
export { default as deleteAddress } from './deleteAddress';
export { default as deleteDefaultBillingAddress } from './deleteDefaultBillingAddress';
export { default as deleteDefaultContactAddress } from './deleteDefaultContactAddress';
export { default as deleteDefaultShippingAddress } from './deleteDefaultShippingAddress';
export { default as getAddress } from './getAddress';
export { default as getAddresses } from './getAddresses';
export { default as getDefaultContactAddress } from './getDefaultContactAddress';
export { default as getSchema } from './getSchema';
export { default as postAddress } from './postAddress';
export { default as putAddress } from './putAddress';
export { default as deleteAddress } from './deleteAddress';
export { default as putDefaultBillingAddress } from './putDefaultBillingAddress';
export { default as putDefaultShippingAddress } from './putDefaultShippingAddress';
export { default as getSchema } from './getSchema';
export { default as putDefaultContactAddress } from './putDefaultContactAddress';
export { default as deleteDefaultContactAddress } from './deleteDefaultContactAddress';
export { default as getDefaultContactAddress } from './getDefaultContactAddress';
export { default as putDefaultShippingAddress } from './putDefaultShippingAddress';
84 changes: 84 additions & 0 deletions packages/core/src/addresses/redux/__tests__/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ describe('Addresses reducers', () => {
actionTypes.SET_DEFAULT_SHIPPING_ADDRESS_FAILURE,
actionTypes.SET_DEFAULT_CONTACT_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_CONTACT_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_FAILURE,
actionTypes.GET_DEFAULT_CONTACT_ADDRESS_FAILURE,
])('should handle %s action type', actionType => {
const error = 'foo';
Expand Down Expand Up @@ -175,6 +177,8 @@ describe('Addresses reducers', () => {
actionTypes.SET_DEFAULT_SHIPPING_ADDRESS_REQUEST,
actionTypes.SET_DEFAULT_CONTACT_ADDRESS_REQUEST,
actionTypes.DELETE_DEFAULT_CONTACT_ADDRESS_REQUEST,
actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_REQUEST,
actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_REQUEST,
actionTypes.GET_DEFAULT_CONTACT_ADDRESS_REQUEST,
])('should handle %s action type', actionType => {
expect(
Expand All @@ -194,6 +198,8 @@ describe('Addresses reducers', () => {
actionTypes.SET_DEFAULT_SHIPPING_ADDRESS_SUCCESS,
actionTypes.SET_DEFAULT_CONTACT_ADDRESS_SUCCESS,
actionTypes.DELETE_DEFAULT_CONTACT_ADDRESS_SUCCESS,
actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS,
actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS,
actionTypes.GET_DEFAULT_CONTACT_ADDRESS_SUCCESS,
])('should handle %s action type', actionType => {
expect(
Expand All @@ -215,6 +221,8 @@ describe('Addresses reducers', () => {
actionTypes.SET_DEFAULT_SHIPPING_ADDRESS_FAILURE,
actionTypes.SET_DEFAULT_CONTACT_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_CONTACT_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_FAILURE,
actionTypes.GET_DEFAULT_CONTACT_ADDRESS_FAILURE,
])('should handle %s action type', actionType => {
expect(
Expand Down Expand Up @@ -770,6 +778,76 @@ describe('Addresses reducers', () => {
).toEqual(expectedResult);
});
});

describe('delete default billing address', () => {
it('should handle DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS action type', () => {
const state = {
addresses: {
1: {
id: 1,
address: 'data',
isCurrentBilling: true,
},
},
};

// Should unmark the previous default as the default address
const expectedResult = {
addresses: {
1: {
id: 1,
address: 'data',
isCurrentBilling: false,
},
},
};

expect(
entitiesMapper[actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS](
state,
{
meta: { addressId: 1, userId: '1' },
type: actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS,
},
),
).toEqual(expectedResult);
});
});

describe('delete default shipping address', () => {
it('should handle DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS action type', () => {
const state = {
addresses: {
1: {
id: 1,
address: 'data',
isCurrentShipping: true,
},
},
};

// Should unmark the previous default as the default address
const expectedResult = {
addresses: {
1: {
id: 1,
address: 'data',
isCurrentShipping: false,
},
},
};

expect(
entitiesMapper[actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS](
state,
{
meta: { addressId: 1, userId: '1' },
type: actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS,
},
),
).toEqual(expectedResult);
});
});
});

describe('address() reducer', () => {
Expand Down Expand Up @@ -802,6 +880,8 @@ describe('Addresses reducers', () => {
actionTypes.SET_DEFAULT_SHIPPING_ADDRESS_REQUEST,
actionTypes.SET_DEFAULT_CONTACT_ADDRESS_REQUEST,
actionTypes.DELETE_DEFAULT_CONTACT_ADDRESS_REQUEST,
actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_REQUEST,
actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_REQUEST,
])('should handle %s action type', actionType => {
expect(
reducer(undefined, {
Expand All @@ -822,6 +902,8 @@ describe('Addresses reducers', () => {
actionTypes.SET_DEFAULT_SHIPPING_ADDRESS_FAILURE,
actionTypes.SET_DEFAULT_CONTACT_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_CONTACT_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_FAILURE,
actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_FAILURE,
])('should handle %s action type', actionType => {
expect(
reducer(undefined, {
Expand All @@ -843,6 +925,8 @@ describe('Addresses reducers', () => {
actionTypes.SET_DEFAULT_SHIPPING_ADDRESS_SUCCESS,
actionTypes.SET_DEFAULT_CONTACT_ADDRESS_SUCCESS,
actionTypes.DELETE_DEFAULT_CONTACT_ADDRESS_SUCCESS,
actionTypes.DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS,
actionTypes.DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS,
])('should handle %s action type', actionType => {
expect(
reducer(undefined, {
Expand Down
Loading

0 comments on commit b14703e

Please sign in to comment.