Skip to content

Commit

Permalink
Merge branch 'develop' into revanth/cart/giftOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dpatil-magento authored Feb 10, 2020
2 parents b0b001a + 18fd612 commit 89641b3
Show file tree
Hide file tree
Showing 23 changed files with 1,275 additions and 39 deletions.
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)
});
});
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 packages/peregrine/lib/talons/CartPage/GiftCards/useGiftCard.js
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
};
};
Loading

0 comments on commit 89641b3

Please # to comment.