Skip to content

Commit 8778aa1

Browse files
committed
test(checkout): add form-enable test with testing-library and jest-dom
1 parent 14b2d4c commit 8778aa1

4 files changed

Lines changed: 266 additions & 2 deletions

File tree

package-lock.json

Lines changed: 203 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@
3030
"devDependencies": {
3131
"@tailwindcss/forms": "^0.5.7",
3232
"@tailwindcss/typography": "^0.5.10",
33+
"@testing-library/jest-dom": "^6.9.1",
34+
"@testing-library/react": "^16.3.0",
35+
"@testing-library/user-event": "^14.6.1",
3336
"autoprefixer": "^10.4.19",
3437
"eslint": "^8.57.0",
3538
"eslint-import-resolver-node": "^0.3.9",
3639
"eslint-plugin-import": "^2.32.0",
3740
"eslint-plugin-jsx-a11y": "^6.10.2",
3841
"eslint-plugin-react": "^7.37.5",
3942
"eslint-plugin-react-hooks": "^4.6.2",
43+
"husky": "^8.0.0",
4044
"postcss": "^8.4.38",
41-
"tailwindcss": "^3.4.10",
42-
"husky": "^8.0.0"
45+
"tailwindcss": "^3.4.10"
4346
},
4447
"browserslist": {
4548
"production": [

src/__tests__/checkout.test.jsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
});

src/setupTests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
* Copyright (C) 2025 Dimitrios S. Sfyris
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
import '@testing-library/jest-dom';

0 commit comments

Comments
 (0)