Skip to content

Commit

Permalink
feat: add missing address endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jpklzm committed Jan 23, 2024
1 parent b77be2f commit c3a3528
Show file tree
Hide file tree
Showing 18 changed files with 679 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import join from 'proper-url-join';
import moxios from 'moxios';

export default {
success: params => {
console.log(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);
});
});
4 changes: 2 additions & 2 deletions packages/core/src/addresses/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export { default as getPredictions } from './getPredictions';
export { default as getPredictionDetails } from './getPredictionDetails';

export { default as deleteAddress } from './deleteAddress';
export { default as deleteDefaultBillingAddress } from './deleteDefaultContactAddress';
export { default as deleteDefaultBillingAddress } from './deleteDefaultBillingAddress';
export { default as deleteDefaultContactAddress } from './deleteDefaultContactAddress';
export { default as deleteDefaultShippingAddress } 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';
Expand Down
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
32 changes: 32 additions & 0 deletions packages/core/src/addresses/redux/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,37 @@ export const GET_DEFAULT_CONTACT_ADDRESS_REQUEST =
export const GET_DEFAULT_CONTACT_ADDRESS_SUCCESS =
'@farfetch/blackout-core/GET_DEFAULT_CONTACT_ADDRESS_SUCCESS';

/**
* Action type dispatched when delete default billing address request fails.
*/
export const DELETE_DEFAULT_BILLING_ADDRESS_FAILURE =
'@farfetch/blackout-redux/DELETE_DEFAULT_BILLING_ADDRESS_FAILURE';
/**
* Action type dispatched when delete default billing address request starts.
*/
export const DELETE_DEFAULT_BILLING_ADDRESS_REQUEST =
'@farfetch/blackout-redux/DELETE_DEFAULT_BILLING_ADDRESS_REQUEST';
/**
* Action type dispatched when delete default billing address request succeeds.
*/
export const DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS =
'@farfetch/blackout-redux/DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS';

/**
* Action type dispatched when delete default shipping address request fails.
*/
export const DELETE_DEFAULT_SHIPPING_ADDRESS_FAILURE =
'@farfetch/blackout-redux/DELETE_DEFAULT_SHIPPING_ADDRESS_FAILURE';
/**
* Action type dispatched when delete default shipping address request starts.
*/
export const DELETE_DEFAULT_SHIPPING_ADDRESS_REQUEST =
'@farfetch/blackout-redux/DELETE_DEFAULT_SHIPPING_ADDRESS_REQUEST';
/**
* Action type dispatched when delete default shipping address request succeeds.
*/
export const DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS =
'@farfetch/blackout-redux/DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS';

/** Action type dispatched when reseting the addresses. */
export const RESET_ADDRESSES = '@farfetch/blackout-core/RESET_ADDRESSES';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`doDeleteDefaultBillingAddress() action creator should create the correct actions for when the delete default billing address procedure is successful: delete default billing address success payload 1`] = `
Object {
"meta": Object {
"addressId": "2222222",
"userId": "121212",
},
"type": "@farfetch/blackout-redux/DELETE_DEFAULT_BILLING_ADDRESS_SUCCESS",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`doDeleteDefaultShippingAddress() action creator should create the correct actions for when the delete default shipping address procedure is successful: delete default shipping address success payload 1`] = `
Object {
"meta": Object {
"addressId": "2222222",
"userId": "121212",
},
"type": "@farfetch/blackout-redux/DELETE_DEFAULT_SHIPPING_ADDRESS_SUCCESS",
}
`;
Loading

0 comments on commit c3a3528

Please sign in to comment.