Skip to content

Commit

Permalink
Merge branch 'develop' into tommy/orders-remove-mock
Browse files Browse the repository at this point in the history
  • Loading branch information
tjwiebell committed Nov 10, 2020
2 parents 5604e84 + 49a0cd5 commit 41d2595
Show file tree
Hide file tree
Showing 68 changed files with 3,311 additions and 238 deletions.
42 changes: 21 additions & 21 deletions packages/extensions/venia-sample-language-packs/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "@magento/venia-sample-language-packs",
"version": "0.0.1",
"description": "Provides demo translations for PWA Studio.",
"publishConfig": {
"access": "public"
},
"license": "(OSL-3.0 OR AFL-3.0)",
"main": "./i18n-intercept.js",
"repository": "github:magento/pwa-studio",
"scripts": {
"clean": " "
},
"peerDependencies": {
"@magento/pwa-buildpack": "~7.0.0",
"@magento/venia-ui": "~5.0.0"
},
"pwa-studio": {
"targets": {
"intercept": "./i18n-intercept"
}
"name": "@magento/venia-sample-language-packs",
"version": "0.0.1",
"publishConfig": {
"access": "public"
},
"description": "Provides demo translations for PWA Studio.",
"main": "./i18n-intercept.js",
"scripts": {
"clean": " "
},
"repository": "github:magento/pwa-studio",
"license": "(OSL-3.0 OR AFL-3.0)",
"peerDependencies": {
"@magento/pwa-buildpack": "~7.0.0",
"@magento/venia-ui": "~5.0.0"
},
"pwa-studio": {
"targets": {
"intercept": "./i18n-intercept"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect } from 'react';
import { createTestInstance } from '@magento/peregrine';

import { useGiftCard } from '../useGiftCard';
import { act } from 'react-test-renderer';

/*
* Member variables.
Expand All @@ -18,9 +19,10 @@ const Component = props => {
return null;
};

const removeGiftCard = jest.fn();
const props = {
code: 'unit test',
removeGiftCard: jest.fn()
removeGiftCard
};

/*
Expand All @@ -36,3 +38,16 @@ test('it returns the proper shape', () => {
removeGiftCardWithCode: expect.any(Function)
});
});

test('it calls removeGiftCard() with the correct value', () => {
// Act.
createTestInstance(<Component {...props} />);

const { removeGiftCardWithCode } = log.mock.calls[0][0];

act(() => {
removeGiftCardWithCode();
});

expect(removeGiftCard).toHaveBeenCalledWith(props.code);
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ const removeCardResult = {
loading: false
};

const setIsCartUpdating = jest.fn();

const props = {
setIsCartUpdating: jest.fn(),
setIsCartUpdating: setIsCartUpdating,
mutations: {
applyCardMutation: 'mock apply',
removeCardMutation: 'mock remove'
Expand Down Expand Up @@ -172,3 +174,112 @@ test('returns error message with invalid request', () => {
})
);
});

test('it runs the card balance query when checkGiftCardBalance() is called', () => {
const checkCardBalance = jest.fn();

useLazyQuery.mockImplementation(input => {
if (input === 'mock balance') return [checkCardBalance, balanceResult];
});

// Act.
const component = createTestInstance(<Component {...props} />);

let talonProps = component.root.findByProps({ id: 'giftCard' }).props;

const { setFormApi } = talonProps;

const mockCardCode = 'mock card code';
const getValue = jest.fn(() => mockCardCode);
const formApi = {
getValue
};

act(() => {
setFormApi(formApi);
component.update(<Component {...props} />);
});

talonProps = component.root.findByProps({ id: 'giftCard' }).props;

expect(talonProps.shouldDisplayCardBalance).toBeFalsy();

act(() => {
talonProps.checkGiftCardBalance();
});

talonProps = component.root.findByProps({ id: 'giftCard' }).props;

expect(talonProps.shouldDisplayCardBalance).toBeTruthy();

expect(getValue).toHaveBeenCalledWith('card');

expect(checkCardBalance).toHaveBeenCalledWith(
expect.objectContaining({
variables: {
giftCardCode: mockCardCode
}
})
);
});

test('it runs the remove card mutation when removeGiftCard() is called', () => {
const removeCard = jest.fn();
removeCardResult.called = true;
removeCardResult.loading = true;

useMutation.mockImplementation(input => {
if (input === 'mock remove') return [removeCard, removeCardResult];
if (input === 'mock apply') return [deferredFn, applyCardResult];
});

// Act.
const component = createTestInstance(<Component {...props} />);

const talonProps = component.root.findByProps({ id: 'giftCard' }).props;

const mockGiftCardCode = 'mock gift card code';

act(() => {
talonProps.removeGiftCard(mockGiftCardCode);
});

expect(removeCard).toHaveBeenCalledWith(
expect.objectContaining({
variables: {
cartId: 'cart123',
giftCardCode: mockGiftCardCode
}
})
);

expect(talonProps.isRemovingCard).toBeTruthy();

expect(setIsCartUpdating).toHaveBeenCalledTimes(1);
expect(setIsCartUpdating).toHaveBeenCalledWith(true);
});

test('it handles no applied gift cards', () => {
useQuery.mockReturnValue({
...cartResult,
data: {
cart: {
applied_gift_cards: null // or []
}
}
});

useLazyQuery.mockReturnValue([deferredFn, balanceResult]);

useMutation.mockImplementation(input => {
if (input === 'mock apply') return [deferredFn, applyCardResult];
return [deferredFn, removeCardResult];
});

// Act.
const component = createTestInstance(<Component {...props} />);

const talonProps = component.root.findByProps({ id: 'giftCard' }).props;

expect(talonProps.giftCardsData).toStrictEqual([]);
});
Loading

0 comments on commit 41d2595

Please sign in to comment.