-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5419 from vuestorefront/checkout-shipping
Checkout shipping
- Loading branch information
Showing
21 changed files
with
926 additions
and
98 deletions.
There are no files selected for viewing
32 changes: 0 additions & 32 deletions
32
packages/commercetools/composables/__tests__/getters/shippingMethodHelpers.spec.ts
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
packages/commercetools/composables/__tests__/useShipping/useShipping.spec.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,106 @@ | ||
import useShipping from '../../src/useShipping'; | ||
import useCart from '../../src/useCart'; | ||
|
||
jest.mock('@vue-storefront/commercetools-api', () => ({ | ||
cartActions: { | ||
setShippingMethodAction: () => {}, | ||
setShippingAddressAction: () => {} | ||
} | ||
})); | ||
|
||
jest.mock('../../src/useCart', () => jest.fn(() => {})); | ||
|
||
jest.mock('@vue-storefront/core', () => ({ | ||
useShippingFactory: (params) => () => params | ||
})); | ||
|
||
describe('[commercetools-composables] useShipping', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('provides cart', async () => { | ||
const { provide } = useShipping() as any; | ||
const mockedCart = '12312312'; | ||
(useCart as jest.Mock).mockImplementation(() => mockedCart); | ||
|
||
const toProvide = provide(); | ||
|
||
expect(toProvide).toMatchObject({ cart: mockedCart }); | ||
expect(useCart).toHaveBeenCalled(); | ||
}); | ||
|
||
it('loads shipping address via request if cart is not present', async () => { | ||
const { load } = useShipping() as any; | ||
const loadedShippingAddress = 'loadedShippingAddress'; | ||
const context = { | ||
cart: { | ||
cart: { | ||
value: { | ||
shippingAddress: null | ||
} | ||
}, | ||
load: jest.fn(() => { | ||
context.cart.cart.value.shippingAddress = loadedShippingAddress; | ||
}) | ||
} | ||
}; | ||
|
||
const response = await load(context, {}); | ||
|
||
expect(response).toBe(loadedShippingAddress); | ||
expect(context.cart.load).toHaveBeenCalled(); | ||
}); | ||
|
||
it('loads shipping address from cart if cart is present', async () => { | ||
const { load } = useShipping() as any; | ||
const loadedShippingAddress = 'loadedShippingAddress'; | ||
const context = { | ||
cart: { | ||
cart: { | ||
value: { | ||
shippingAddress: loadedShippingAddress | ||
} | ||
}, | ||
load: jest.fn() | ||
} | ||
}; | ||
|
||
const response = await load(context, {}); | ||
|
||
expect(response).toBe(loadedShippingAddress); | ||
expect(context.cart.load).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('saves shipping details, updates cart and returns shipping details', async () => { | ||
const { save } = useShipping() as any; | ||
const newShippingAddress = 'newShippingAddress'; | ||
const context = { | ||
cart: { | ||
cart: { | ||
value: { | ||
shippingAddress: null | ||
} | ||
}, | ||
setCart: jest.fn(address => { | ||
context.cart.cart.value.shippingAddress = address; | ||
}) | ||
}, | ||
$ct: { | ||
api: { | ||
updateCart: jest.fn(() => ({ | ||
data: { | ||
cart: newShippingAddress | ||
} | ||
})) | ||
} | ||
} | ||
}; | ||
|
||
const response = await save(context, { shippingDetails: newShippingAddress }); | ||
|
||
expect(response).toBe(newShippingAddress); | ||
expect(context.cart.setCart).toHaveBeenCalledWith(newShippingAddress); | ||
}); | ||
}); | ||
|
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
31 changes: 0 additions & 31 deletions
31
packages/commercetools/composables/src/getters/checkoutGetters.ts
This file was deleted.
Oops, something went wrong.
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
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
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
33 changes: 33 additions & 0 deletions
33
packages/commercetools/composables/src/useShipping/index.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,33 @@ | ||
import { useShippingFactory, UseShippingParams, Context } from '@vue-storefront/core'; | ||
import useCart from '../useCart'; | ||
import { cartActions } from '@vue-storefront/commercetools-api'; | ||
import { Address } from './../types/GraphQL'; | ||
|
||
const params: UseShippingParams<Address, {}> = { | ||
provide() { | ||
return { | ||
cart: useCart() | ||
}; | ||
}, | ||
load: async (context: Context, { customQuery }) => { | ||
if (!context.cart.cart?.value?.shippingAddress) { | ||
await context.cart.load({ customQuery }); | ||
} | ||
return context.cart.cart.value.shippingAddress; | ||
}, | ||
save: async (context: Context, { shippingDetails, customQuery }) => { | ||
const cartResponse = await context.$ct.api.updateCart({ | ||
id: context.cart.cart.value.id, | ||
version: context.cart.cart.value.version, | ||
actions: [ | ||
cartActions.setShippingMethodAction(), | ||
cartActions.setShippingAddressAction(shippingDetails) | ||
] | ||
}, customQuery); | ||
|
||
context.cart.setCart(cartResponse.data.cart); | ||
return context.cart.cart.value.shippingAddress; | ||
} | ||
}; | ||
|
||
export default useShippingFactory<Address, {}>(params); |
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
Oops, something went wrong.