|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Dimitrios S. Sfyris |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + */ |
| 5 | +// src/__tests__/checkout.test.jsx |
| 6 | +import React from 'react'; |
| 7 | +import { MemoryRouter } from 'react-router-dom'; |
| 8 | +import { render, screen } from '@testing-library/react'; |
| 9 | +import userEvent from '@testing-library/user-event'; |
| 10 | +import Checkout from '../component/Checkout'; |
| 11 | + |
| 12 | +// Mock the cart & auth hooks used by Checkout |
| 13 | +jest.mock('react-use-cart', () => ({ |
| 14 | + useCart: () => ({ |
| 15 | + items: [{ id: 1, name: 'Test Product', price: 10, quantity: 2 }], |
| 16 | + cartTotal: 20, |
| 17 | + isEmpty: false, |
| 18 | + emptyCart: jest.fn(), |
| 19 | + }), |
| 20 | +})); |
| 21 | +jest.mock('../lib/auth', () => ({ |
| 22 | + useAuth: () => ({ user: null, isAuthenticated: false }), |
| 23 | +})); |
| 24 | + |
| 25 | +function renderUI() { |
| 26 | + return render( |
| 27 | + <MemoryRouter> |
| 28 | + <Checkout /> |
| 29 | + </MemoryRouter> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +test('submit button is disabled initially and enables when form is valid and terms agreed', async () => { |
| 34 | + const user = userEvent.setup(); |
| 35 | + renderUI(); |
| 36 | + |
| 37 | + const submit = screen.getByRole('button', { name: /place order/i }); |
| 38 | + expect(submit).toBeDisabled(); |
| 39 | + |
| 40 | + // Fill required fields |
| 41 | + await user.type(screen.getByLabelText(/first name/i), 'John'); |
| 42 | + await user.type(screen.getByLabelText(/last name/i), 'Doe'); |
| 43 | + await user.type(screen.getByLabelText(/^email$/i), 'john@example.com'); |
| 44 | + await user.type(screen.getByLabelText(/address line 1/i), '123 Demo St'); |
| 45 | + await user.type(screen.getByLabelText(/^city$/i), 'Athens'); |
| 46 | + await user.type(screen.getByLabelText(/zip/i), '12345'); |
| 47 | + await user.selectOptions(screen.getByLabelText(/country/i), 'GR'); |
| 48 | + |
| 49 | + // Terms & conditions |
| 50 | + await user.click(screen.getByLabelText(/i agree to the terms/i)); |
| 51 | + |
| 52 | + expect(submit).toBeEnabled(); |
| 53 | +}); |
0 commit comments