-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): create draft orders react
- Loading branch information
Showing
12 changed files
with
628 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
packages/react/src/checkout/draftOrders/__tests__/useDraftOrder.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
119 changes: 119 additions & 0 deletions
119
packages/react/src/checkout/draftOrders/__tests__/useDraftOrders.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './useDraftOrder.js'; | ||
export * from './useDraftOrders.js'; |
7 changes: 7 additions & 0 deletions
7
packages/react/src/checkout/draftOrders/hooks/types/useDraftOrder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/react/src/checkout/draftOrders/hooks/types/useDraftOrders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
}; |
Oops, something went wrong.