-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into revanth/cart/giftOptions
- Loading branch information
Showing
23 changed files
with
1,275 additions
and
39 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/peregrine/lib/talons/CartPage/GiftCards/__tests__/useGiftCard.spec.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,38 @@ | ||
import React, { useEffect } from 'react'; | ||
import { createTestInstance } from '@magento/peregrine'; | ||
|
||
import { useGiftCard } from '../useGiftCard'; | ||
|
||
/* | ||
* Member variables. | ||
*/ | ||
|
||
const log = jest.fn(); | ||
const Component = props => { | ||
const talonProps = useGiftCard({ ...props }); | ||
|
||
useEffect(() => { | ||
log(talonProps); | ||
}, [talonProps]); | ||
|
||
return null; | ||
}; | ||
|
||
const props = { | ||
code: 'unit test', | ||
removeGiftCard: jest.fn() | ||
}; | ||
|
||
/* | ||
* Tests. | ||
*/ | ||
|
||
test('it returns the proper shape', () => { | ||
// Act. | ||
createTestInstance(<Component {...props} />); | ||
|
||
// Assert. | ||
expect(log).toHaveBeenCalledWith({ | ||
removeGiftCardWithCode: expect.any(Function) | ||
}); | ||
}); |
121 changes: 121 additions & 0 deletions
121
packages/peregrine/lib/talons/CartPage/GiftCards/__tests__/useGiftCards.spec.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,121 @@ | ||
import React, { useEffect } from 'react'; | ||
import { createTestInstance } from '@magento/peregrine'; | ||
|
||
import { useGiftCards } from '../useGiftCards'; | ||
|
||
/* | ||
* Mocked Modules. | ||
*/ | ||
jest.mock('@apollo/react-hooks', () => { | ||
const deferredFn = jest.fn(); | ||
|
||
const cartResult = { | ||
data: { | ||
cart: { | ||
applied_gift_cards: [ | ||
{ code: 'unit test card 1' }, | ||
{ code: 'unit test card 2' } | ||
] | ||
} | ||
}, | ||
error: null, | ||
loading: false | ||
}; | ||
const balanceResult = { | ||
data: { | ||
giftCardAccount: { | ||
balance: { | ||
currency: 'USD', | ||
value: '100' | ||
}, | ||
code: 'unit test' | ||
} | ||
}, | ||
error: null, | ||
loading: false | ||
}; | ||
const applyCardResult = { | ||
data: null, | ||
error: null, | ||
loading: false | ||
}; | ||
const removeCardResult = { | ||
data: null, | ||
error: null, | ||
loading: false | ||
}; | ||
|
||
const useLazyQuery = jest.fn(input => { | ||
if (input === 'mock cart') return [deferredFn, cartResult]; | ||
return [deferredFn, balanceResult]; | ||
}); | ||
const useMutation = jest.fn(input => { | ||
if (input === 'mock apply') return [deferredFn, applyCardResult]; | ||
return [deferredFn, removeCardResult]; | ||
}); | ||
|
||
return { useLazyQuery, useMutation }; | ||
}); | ||
|
||
jest.mock('@magento/peregrine/lib/context/cart', () => { | ||
const state = { | ||
cartId: 'cart123' | ||
}; | ||
const api = {}; | ||
const useCartContext = jest.fn(() => [state, api]); | ||
|
||
return { useCartContext }; | ||
}); | ||
|
||
/* | ||
* Member variables. | ||
*/ | ||
|
||
const log = jest.fn(); | ||
const Component = props => { | ||
const talonProps = useGiftCards({ ...props }); | ||
|
||
useEffect(() => { | ||
log(talonProps); | ||
}, [talonProps]); | ||
|
||
return null; | ||
}; | ||
|
||
const props = { | ||
applyCardMutation: 'mock apply', | ||
cardBalanceQuery: 'mock balance', | ||
cartQuery: 'mock cart', | ||
removeCardMutation: 'mock remove' | ||
}; | ||
|
||
/* | ||
* Tests. | ||
*/ | ||
|
||
test('it returns the proper shape', () => { | ||
// Act. | ||
createTestInstance(<Component {...props} />); | ||
|
||
// Assert. | ||
expect(log).toHaveBeenCalledWith({ | ||
applyGiftCard: expect.any(Function), | ||
canTogglePromptState: expect.any(Boolean), | ||
checkBalanceData: expect.any(Object), | ||
checkGiftCardBalance: expect.any(Function), | ||
errorLoadingGiftCards: expect.any(Boolean), | ||
errorRemovingCard: expect.any(Boolean), | ||
giftCardsData: expect.any(Array), | ||
isLoadingGiftCards: expect.any(Boolean), | ||
isApplyingCard: expect.any(Boolean), | ||
isCheckingBalance: expect.any(Boolean), | ||
isRemovingCard: expect.any(Boolean), | ||
removeGiftCard: expect.any(Function), | ||
setFormApi: expect.any(Function), | ||
shouldDisplayCardBalance: expect.any(Boolean), | ||
shouldDisplayCardEntry: expect.any(Boolean), | ||
shouldDisplayCardError: expect.any(Boolean), | ||
submitForm: expect.any(Function), | ||
togglePromptState: expect.any(Function) | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
packages/peregrine/lib/talons/CartPage/GiftCards/useGiftCard.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,13 @@ | ||
import { useCallback } from 'react'; | ||
|
||
export const useGiftCard = props => { | ||
const { code, removeGiftCard } = props; | ||
|
||
const removeGiftCardWithCode = useCallback(() => { | ||
removeGiftCard(code); | ||
}, [code, removeGiftCard]); | ||
|
||
return { | ||
removeGiftCardWithCode | ||
}; | ||
}; |
Oops, something went wrong.