|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { |
| 3 | + useCart, |
| 4 | + useUser, |
| 5 | +} from '@vue-storefront/magento'; |
| 6 | + |
| 7 | +import { useUiState } from '~/composables'; |
| 8 | +import { |
| 9 | + render, useCartMock, useUserMock, useUiStateMock, useEmptyCartMock, |
| 10 | +} from '~/test-utils'; |
| 11 | +import CartSidebar from '~/components/CartSidebar'; |
| 12 | + |
| 13 | +jest.mock('@vue-storefront/magento', () => ({ |
| 14 | + ...jest.requireActual('@vue-storefront/magento'), |
| 15 | + useExternalCheckout: jest.fn(() => ({ initializeCheckout: {} })), |
| 16 | + useCart: jest.fn(), |
| 17 | + useUser: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +jest.mock('~/composables/useUiState'); |
| 21 | + |
| 22 | +useUser.mockReturnValue(useUserMock()); |
| 23 | +useCart.mockReturnValue(useCartMock()); |
| 24 | + |
| 25 | +describe('<CartSidebar>', () => { |
| 26 | + it('should be not visible by default', () => { |
| 27 | + useUiState.mockReturnValue(useUiStateMock()); |
| 28 | + const { queryByText } = render(CartSidebar); |
| 29 | + expect(queryByText('My Cart')).toBeNull(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('If the cart is empty', () => { |
| 33 | + beforeAll(() => { |
| 34 | + useCart.mockReturnValue(useEmptyCartMock()); |
| 35 | + }); |
| 36 | + describe('And the cart sidebar is open', () => { |
| 37 | + it('should render empty state', () => { |
| 38 | + useUiState.mockReturnValue(useUiStateMock({ isCartSidebarOpen: true })); |
| 39 | + const { queryByText } = render(CartSidebar); |
| 40 | + expect(queryByText('Your cart is empty')).toBeTruthy(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('go back button must close the cart sidebar', () => { |
| 44 | + const uiStateMock = useUiStateMock({ isCartSidebarOpen: true }); |
| 45 | + useUiState.mockReturnValue(uiStateMock); |
| 46 | + const { queryByText } = render(CartSidebar); |
| 47 | + const closeSidebarBtn = queryByText('Go back shopping'); |
| 48 | + expect(closeSidebarBtn).toBeTruthy(); |
| 49 | + |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
| 51 | + userEvent.click(closeSidebarBtn); |
| 52 | + expect(uiStateMock.toggleCartSidebar).toHaveBeenCalledTimes(1); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('If the cart has two products', () => { |
| 58 | + beforeAll(() => { |
| 59 | + useCart.mockReturnValue(useCartMock()); |
| 60 | + useUiState.mockReturnValue(useUiStateMock({ isCartSidebarOpen: true })); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should render two Product Cards', () => { |
| 64 | + const { container } = render(CartSidebar); |
| 65 | + const productCards = container.querySelectorAll('div.sf-collected-product'); |
| 66 | + expect(productCards).toHaveLength(2); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should display proper total items value', () => { |
| 70 | + const { container } = render(CartSidebar); |
| 71 | + const totalItemsLValue = container.querySelector('div.cart-summary .sf-property__value'); |
| 72 | + const totalItemsLabel = container.querySelector('div.cart-summary .sf-property__name'); |
| 73 | + expect(totalItemsLabel).toHaveTextContent('Total items'); |
| 74 | + expect(totalItemsLValue).toHaveTextContent(2); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should render "go to checkout" button', () => { |
| 78 | + const { getByText } = render(CartSidebar); |
| 79 | + expect(getByText('Go to checkout')).toBeTruthy(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should render promo code input', () => { |
| 83 | + const { getByTestId } = render(CartSidebar); |
| 84 | + expect(getByTestId('promoCode')).toBeTruthy(); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('And exactly one product is out of stock', () => { |
| 88 | + it('should display exactly one out of stock badge', () => { |
| 89 | + const { getAllByText } = render(CartSidebar); |
| 90 | + expect(getAllByText('Out of stock')).toHaveLength(1); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments