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