-
Notifications
You must be signed in to change notification settings - Fork 217
feat(payment): PAYPAL-4935 added CartActionCreator for handling/storing cart information #2802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bc-nick
wants to merge
8
commits into
bigcommerce:master
Choose a base branch
from
bc-nick:PAYPAL-4935
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c723c0c
feat(payment): PAYPAL-4935 added CartActionCreator for handling/stori…
bc-nick c6f09ae
feat(payment): PAYPAL-4935 updates related to new requirements
bc-nick 0ecd084
feat(payment): PAYPAL-4935 updates after review
bc-nick 5728185
feat(payment): PAYPAL-4935 updates after review
bc-nick 95c18b6
feat(payment): PAYPAL-4935 updates after review
bc-nick 4df438a
feat(payment): PAYPAL-4935 renaming
bc-nick 7d966d5
feat(payment): PAYPAL-4935 added host
bc-nick 479676b
feat(payment): PAYPAL-4935 changes related to gql logic
bc-nick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,107 @@ | ||
import { createRequestSender, RequestSender } from '@bigcommerce/request-sender'; | ||
import { from, of } from 'rxjs'; | ||
import { catchError, toArray } from 'rxjs/operators'; | ||
|
||
import { Cart } from '../cart'; | ||
import CheckoutStore from '../checkout/checkout-store'; | ||
import { getCheckoutStoreState } from '../checkout/checkouts.mock'; | ||
import createCheckoutStore from '../checkout/create-checkout-store'; | ||
import { getErrorResponse, getResponse } from '../common/http-request/responses.mock'; | ||
|
||
import CartActionCreator from './cart-action-creator'; | ||
import { CartActionType } from './cart-actions'; | ||
import CartRequestSender from './cart-request-sender'; | ||
import { getCart } from './carts.mock'; | ||
import { getGQLCartResponse, getGQLCurrencyResponse } from './gql-cart/mocks/gql-cart.mock'; | ||
|
||
describe('CartActionCreator', () => { | ||
let cartActionCreator: CartActionCreator; | ||
let requestSender: RequestSender; | ||
let cartRequestSender: CartRequestSender; | ||
let store: CheckoutStore; | ||
let cart: Cart; | ||
|
||
beforeEach(() => { | ||
cart = getCart(); | ||
requestSender = createRequestSender(); | ||
|
||
cartRequestSender = new CartRequestSender(requestSender); | ||
|
||
store = createCheckoutStore(getCheckoutStoreState()); | ||
|
||
jest.spyOn(cartRequestSender, 'loadCart').mockReturnValue( | ||
Promise.resolve(getResponse(getGQLCartResponse())), | ||
); | ||
|
||
jest.spyOn(cartRequestSender, 'loadCartCurrency').mockReturnValue( | ||
Promise.resolve(getResponse(getGQLCurrencyResponse())), | ||
); | ||
|
||
cartActionCreator = new CartActionCreator(cartRequestSender); | ||
}); | ||
|
||
it('emits action to notify loading progress', async () => { | ||
const actions = await from(cartActionCreator.loadCart(cart.id)(store)) | ||
.pipe(toArray()) | ||
.toPromise(); | ||
|
||
expect(cartRequestSender.loadCart).toHaveBeenCalledWith(cart.id, undefined, undefined); | ||
|
||
expect(actions).toEqual( | ||
expect.arrayContaining([ | ||
{ type: CartActionType.LoadCartRequested }, | ||
{ | ||
type: CartActionType.LoadCartSucceeded, | ||
payload: expect.objectContaining({ | ||
id: cart.id, | ||
currency: { | ||
code: cart.currency.code, | ||
name: cart.currency.name, | ||
symbol: cart.currency.symbol, | ||
decimalPlaces: cart.currency.decimalPlaces, | ||
}, | ||
lineItems: expect.objectContaining({ | ||
physicalItems: cart.lineItems.physicalItems.map((item) => | ||
expect.objectContaining({ | ||
id: item.id, | ||
variantId: item.variantId, | ||
productId: item.productId, | ||
sku: item.sku, | ||
name: item.name, | ||
url: item.url, | ||
quantity: item.quantity, | ||
isShippingRequired: item.isShippingRequired, | ||
}), | ||
), | ||
}), | ||
}), | ||
}, | ||
]), | ||
); | ||
}); | ||
|
||
it('emits error action if unable to load cart', async () => { | ||
jest.spyOn(cartRequestSender, 'loadCart').mockReturnValue( | ||
Promise.reject(getErrorResponse()), | ||
); | ||
|
||
const errorHandler = jest.fn((action) => of(action)); | ||
|
||
const actions = await from(cartActionCreator.loadCart(cart.id)(store)) | ||
.pipe(catchError(errorHandler), toArray()) | ||
.toPromise(); | ||
|
||
expect(cartRequestSender.loadCart).toHaveBeenCalledWith(cart.id, undefined, undefined); | ||
|
||
expect(actions).toEqual( | ||
expect.arrayContaining([ | ||
{ type: CartActionType.LoadCartRequested }, | ||
{ | ||
type: CartActionType.LoadCartFailed, | ||
error: true, | ||
payload: getErrorResponse(), | ||
}, | ||
]), | ||
); | ||
}); | ||
}); |
This file contains hidden or 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,71 @@ | ||
import { createAction, createErrorAction, ThunkAction } from '@bigcommerce/data-store'; | ||
import { Response } from '@bigcommerce/request-sender'; | ||
import { merge } from 'lodash'; | ||
import { Observable, Observer } from 'rxjs'; | ||
|
||
import { RequestOptions } from '@bigcommerce/checkout-sdk/payment-integration-api'; | ||
|
||
import { InternalCheckoutSelectors } from '../checkout'; | ||
import { cachableAction } from '../common/data-store'; | ||
import ActionOptions from '../common/data-store/action-options'; | ||
|
||
import Cart from './cart'; | ||
import { CartActionType, LoadCartAction } from './cart-actions'; | ||
import CartRequestSender from './cart-request-sender'; | ||
import { GQLCartResponse, GQLCurrencyResponse, GQLRequestResponse, mapToCart } from './gql-cart'; | ||
|
||
export default class CartActionCreator { | ||
constructor(private _cartRequestSender: CartRequestSender) {} | ||
|
||
@cachableAction | ||
loadCart( | ||
cartId: string, | ||
options?: RequestOptions & ActionOptions, | ||
): ThunkAction<LoadCartAction, InternalCheckoutSelectors> { | ||
return (store) => { | ||
return new Observable((observer: Observer<LoadCartAction>) => { | ||
const state = store.getState(); | ||
const gqlUrl = state.config.getGQLRequestUrl(); | ||
|
||
observer.next(createAction(CartActionType.LoadCartRequested, undefined)); | ||
|
||
this._cartRequestSender | ||
.loadCart(cartId, gqlUrl, options) | ||
.then((cartResponse) => { | ||
return this._cartRequestSender | ||
.loadCartCurrency( | ||
cartResponse.body.data.site.cart.currencyCode, | ||
gqlUrl, | ||
options, | ||
) | ||
.then((currencyResponse) => { | ||
observer.next( | ||
createAction( | ||
CartActionType.LoadCartSucceeded, | ||
this.transformToCartResponse( | ||
merge(cartResponse, currencyResponse), | ||
), | ||
), | ||
); | ||
observer.complete(); | ||
}); | ||
}) | ||
.catch((response) => { | ||
observer.error(createErrorAction(CartActionType.LoadCartFailed, response)); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
private transformToCartResponse( | ||
response: Response<GQLRequestResponse<GQLCartResponse & GQLCurrencyResponse>>, | ||
): Cart { | ||
const { | ||
body: { | ||
data: { site }, | ||
}, | ||
} = response; | ||
|
||
return mapToCart(site); | ||
} | ||
} |
This file contains hidden or 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,26 @@ | ||
import { Action } from '@bigcommerce/data-store'; | ||
|
||
import Cart from './cart'; | ||
|
||
export enum CartActionType { | ||
LoadCartRequested = 'LOAD_CART_REQUESTED', | ||
LoadCartSucceeded = 'LOAD_CART_SUCCEEDED', | ||
LoadCartFailed = 'LOAD_CART_FAILED', | ||
} | ||
|
||
export type LoadCartAction = | ||
| LoadCartRequestedAction | ||
| LoadCartSucceededAction | ||
| LoadCartFailedAction; | ||
|
||
export interface LoadCartRequestedAction extends Action { | ||
type: CartActionType.LoadCartRequested; | ||
} | ||
|
||
export interface LoadCartSucceededAction extends Action<Cart> { | ||
type: CartActionType.LoadCartSucceeded; | ||
} | ||
|
||
export interface LoadCartFailedAction extends Action<Error> { | ||
type: CartActionType.LoadCartFailed; | ||
} |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.