Skip to content

Commit

Permalink
feat(react): create draft orders react
Browse files Browse the repository at this point in the history
  • Loading branch information
escabora committed Jan 8, 2024
1 parent e0ce847 commit 172bc3a
Show file tree
Hide file tree
Showing 12 changed files with 628 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/react/src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ Object {
"useCountryAddressSchemas": [Function],
"useCountryStateCities": [Function],
"useCountryStates": [Function],
"useDraftOrder": [Function],
"useDraftOrders": [Function],
"useExchange": [Function],
"useExchangeBookRequest": [Function],
"useExchangeFilters": [Function],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { cleanup, renderHook } from '@testing-library/react';
import {
draftOrderId,
mockDraftOrderItemId as itemId,
mockDraftOrderResponse,
mockDraftOrderState,
mockInitialStateDraftOrders,
} from 'tests/__fixtures__/checkout/draftOrders.fixtures.mjs';
import {
fetchDraftOrder,
removeDraftOrder,
removeDraftOrderItem,
updateDraftOrder,
updateDraftOrderItem,
} from '@farfetch/blackout-redux';
import { mockDraftOrdersQuery as query } from 'tests/__fixtures__/checkout/index.mjs';
import { useDraftOrder } from '../../index.js';
import { withStore } from '../../../../tests/helpers/index.js';
import type {
DraftOrder,
DraftOrderData,
PatchDraftOrderItemData,
} from '@farfetch/blackout-client';

jest.mock('@farfetch/blackout-redux', () => ({
...jest.requireActual('@farfetch/blackout-redux'),
fetchDraftOrder: jest.fn(() => () => Promise.resolve()),
updateDraftOrder: jest.fn(() => () => Promise.resolve()),
updateDraftOrderItem: jest.fn(() => () => Promise.resolve()),
removeDraftOrder: jest.fn(() => () => Promise.resolve()),
removeDraftOrderItem: jest.fn(() => () => Promise.resolve()),
}));

const getRenderedHook = (
state = mockInitialStateDraftOrders,
draftOrderId: DraftOrder['id'],
) => {
const {
result: { current },
} = renderHook(() => useDraftOrder(draftOrderId), {
wrapper: withStore(state),
});

return current;
};

describe('useDraftOrder', () => {
beforeEach(jest.clearAllMocks);

afterEach(cleanup);

it('should return values correctly with initial state', () => {
const current = getRenderedHook(mockDraftOrderState, draftOrderId);

expect(current).toStrictEqual({
isLoading: false,
error: null,
updateIsLoading: false,
updateError: null,
removeIsLoading: false,
removeError: null,
draftOrder: mockDraftOrderResponse,
actions: {
fetch: expect.any(Function),
update: expect.any(Function),
updateItem: expect.any(Function),
remove: expect.any(Function),
removeItem: expect.any(Function),
},
});
});

it('should return the loading state correctly to fetch', () => {
const { isLoading } = getRenderedHook(mockDraftOrderState, draftOrderId);

expect(isLoading).toBe(false);
});

it('should return the loading state correctly to update', () => {
const { updateIsLoading } = getRenderedHook(
mockDraftOrderState,
draftOrderId,
);

expect(updateIsLoading).toBe(false);
});

it('should return the loading state correctly to remove', () => {
const { removeIsLoading } = getRenderedHook(
mockDraftOrderState,
draftOrderId,
);

expect(removeIsLoading).toBe(false);
});

it('should return the error state correctly to fetch', () => {
const { error } = getRenderedHook(mockDraftOrderState, draftOrderId);

expect(error).toBeNull();
});

it('should return the error state correctly to update', () => {
const { updateError } = getRenderedHook(mockDraftOrderState, draftOrderId);

expect(updateError).toBeNull();
});

it('should return the error state correctly to remove', () => {
const { removeError } = getRenderedHook(mockDraftOrderState, draftOrderId);

expect(removeError).toBeNull();
});

it('should return the a draft order state correctly', () => {
const { draftOrder } = getRenderedHook(mockDraftOrderState, draftOrderId);

expect(draftOrder).toStrictEqual(mockDraftOrderResponse);
});

describe('actions', () => {
it('should call `fetch`, `update`, `updateItem`, `remove` and `removeItem` action', () => {
const {
actions: { fetch, update, updateItem, remove, removeItem },
} = getRenderedHook(mockDraftOrderState, draftOrderId);

const dataOrder: DraftOrderData = {
metadata: {
message: 'Some engraved message within the product',
},
};

const dataItem: PatchDraftOrderItemData = {
quantity: 2,
...dataOrder,
};

fetch(draftOrderId, query);
update(draftOrderId, dataOrder);
updateItem(draftOrderId, itemId, dataItem);
remove(draftOrderId);
removeItem(draftOrderId, itemId);

expect(fetchDraftOrder).toHaveBeenCalled();
expect(updateDraftOrder).toHaveBeenCalled();
expect(updateDraftOrderItem).toHaveBeenCalled();
expect(removeDraftOrderItem).toHaveBeenCalled();
expect(removeDraftOrder).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { cleanup, renderHook } from '@testing-library/react';
import { createDraftOrder, fetchDraftOrders } from '@farfetch/blackout-redux';
import {
mockDraftOrderResponse,
mockDraftOrderState,
mockInitialStateDraftOrders,
mockDraftOrdersQuery as query,
} from 'tests/__fixtures__/checkout/draftOrders.fixtures.mjs';
import { checkoutId as orderId } from 'tests/__fixtures__/checkout/checkout.fixtures.mjs';
import { useDraftOrders } from '../../index.js';
import { withStore } from '../../../../tests/helpers/index.js';
import type {
CheckoutOrder,
DraftOrdersQuery,
PostDraftOrderData,
} from '@farfetch/blackout-client';

jest.mock('@farfetch/blackout-redux', () => ({
...jest.requireActual('@farfetch/blackout-redux'),
fetchDraftOrders: jest.fn(() => () => Promise.resolve()),
createDraftOrder: jest.fn(() => () => Promise.resolve()),
}));

const getRenderedHook = (
state = mockInitialStateDraftOrders,
query: DraftOrdersQuery,
orderId: CheckoutOrder['id'],
) => {
const {
result: { current },
} = renderHook(() => useDraftOrders(query, orderId), {
wrapper: withStore(state),
});

return current;
};

describe('useDraftOrders', () => {
beforeEach(jest.clearAllMocks);

afterEach(cleanup);

it('should return values correctly with initial state', () => {
const current = getRenderedHook(mockDraftOrderState, query, orderId);

expect(current).toStrictEqual({
isLoading: false,
createIsLoading: false,
error: null,
createError: null,
draftOrders: [mockDraftOrderResponse],
actions: {
fetch: expect.any(Function),
create: expect.any(Function),
},
});
});

it('should return the loading state correctly to draft orders', () => {
const { isLoading } = getRenderedHook(mockDraftOrderState, query, orderId);

expect(isLoading).toBe(false);
});

it('should return the loading state correctly to create draft order', () => {
const { createIsLoading } = getRenderedHook(
mockDraftOrderState,
query,
orderId,
);

expect(createIsLoading).toBe(false);
});

it('should return the error state correctly to draft orders', () => {
const { error } = getRenderedHook(mockDraftOrderState, query, orderId);

expect(error).toBeNull();
});

it('should return the error state correctly to create draft order', () => {
const { createError } = getRenderedHook(
mockDraftOrderState,
query,
orderId,
);

expect(createError).toBeNull();
});

it('should return the a draft orders state correctly to draft orders', () => {
const { draftOrders } = getRenderedHook(
mockDraftOrderState,
query,
orderId,
);

expect(draftOrders).toStrictEqual([mockDraftOrderResponse]);
});

describe('actions', () => {
it('should call `fetch` all draft orders and `create` draft order action', () => {
const {
actions: { fetch, create },
} = getRenderedHook(mockDraftOrderState, query, orderId);

const data: PostDraftOrderData = {
orderId: 12343243,
customerId: '123',
};

fetch(query);
create(data);

expect(fetchDraftOrders).toHaveBeenCalled();
expect(createDraftOrder).toHaveBeenCalled();
});
});
});
2 changes: 2 additions & 0 deletions packages/react/src/checkout/draftOrders/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as useDraftOrder } from './useDraftOrder.js';
export { default as useDraftOrders } from './useDraftOrders.js';
2 changes: 2 additions & 0 deletions packages/react/src/checkout/draftOrders/hooks/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useDraftOrder.js';
export * from './useDraftOrders.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Config, DraftOrderItem } from '@farfetch/blackout-client';

export type UseDraftOrderOptions = {
enableAutoFetch?: boolean;
fetchConfig?: Config;
itemId?: DraftOrderItem['id'];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { CheckoutOrder, Config } from '@farfetch/blackout-client';

export type UseDraftOrdersOptions = {
enableAutoFetch?: boolean;
fetchConfig?: Config;
orderId?: CheckoutOrder['id'];
};
Loading

0 comments on commit 172bc3a

Please sign in to comment.