|
1 |
| -import request from 'supertest'; |
2 |
| -import { Router } from 'express'; |
3 |
| -import cartController from './shopingCartController.js'; |
4 |
| -import authMiddleware from '../helpers/jwt.js'; |
5 |
| -import adminCheck from '../helpers/adminCheck.js'; |
6 |
| -import { cleanBody } from '../helpers/sanitizer.js'; |
7 | 1 |
|
8 |
| -const router = Router(); |
9 |
| - |
10 |
| -// Mock authMiddleware and adminCheck |
11 |
| -jest.mock('../helpers/jwt.js', () => jest.fn()); |
12 |
| -jest.mock('../helpers/adminCheck.js', () => jest.fn()); |
13 |
| - |
14 |
| -// Mock cartController methods |
15 |
| -cartController.createCart = jest.fn(); |
16 |
| -cartController.getAll = jest.fn(); |
17 |
| -cartController.getUserCart = jest.fn(); |
18 |
| -cartController.editCart = jest.fn(); |
19 |
| -cartController.deleteCart = jest.fn(); |
20 |
| - |
21 |
| -// Mock router methods |
22 |
| -router.post = jest.fn(); |
23 |
| -router.get = jest.fn(); |
24 |
| -router.put = jest.fn(); |
25 |
| -router.delete = jest.fn(); |
26 |
| - |
27 |
| -describe('Cart Routes', () => { |
28 |
| - beforeEach(() => { |
29 |
| - jest.clearAllMocks(); |
30 |
| - }); |
31 |
| - |
32 |
| - it('should call the appropriate methods when POST /create is called', async () => { |
33 |
| - router.post('/create', authMiddleware, cleanBody, cartController.createCart); |
34 |
| - |
35 |
| - const req = { body: { products: [] }, user: { id: 'user_id' } }; |
36 |
| - const res = { json: jest.fn() }; |
37 |
| - const next = jest.fn(); |
38 |
| - |
39 |
| - await request(router).post('/create').send(req.body).set('Authorization', 'Bearer token'); |
40 |
| - |
41 |
| - expect(authMiddleware).toHaveBeenCalled(); |
42 |
| - expect(cleanBody).toHaveBeenCalled(); |
43 |
| - expect(cartController.createCart).toHaveBeenCalledWith(req, res, next); |
44 |
| - }); |
45 |
| - |
46 |
| - it('should call the appropriate methods when GET /getAll is called', async () => { |
47 |
| - router.get('/getAll', authMiddleware, adminCheck, cartController.getAll); |
48 |
| - |
49 |
| - const req = { query: { page: '1', size: '10' } }; |
50 |
| - const res = { json: jest.fn() }; |
51 |
| - const next = jest.fn(); |
52 |
| - |
53 |
| - await request(router).get('/getAll').query(req.query).set('Authorization', 'Bearer token'); |
54 |
| - |
55 |
| - expect(authMiddleware).toHaveBeenCalled(); |
56 |
| - expect(adminCheck).toHaveBeenCalled(); |
57 |
| - expect(cartController.getAll).toHaveBeenCalledWith(req, res, next); |
58 |
| - }); |
59 |
| - |
60 |
| - it('should call the appropriate methods when GET /:userId is called', async () => { |
61 |
| - router.get('/:userId', authMiddleware, cartController.getUserCart); |
62 |
| - |
63 |
| - const req = { params: { userId: 'user_id' } }; |
64 |
| - const res = { json: jest.fn() }; |
65 |
| - const next = jest.fn(); |
66 |
| - |
67 |
| - await request(router).get(`/${req.params.userId}`).set('Authorization', 'Bearer token'); |
68 |
| - |
69 |
| - expect(authMiddleware).toHaveBeenCalled(); |
70 |
| - expect(cartController.getUserCart).toHaveBeenCalledWith(req, res, next); |
71 |
| - }); |
72 |
| - |
73 |
| - it('should call the appropriate methods when PUT /update/:cartId is called', async () => { |
74 |
| - router.put('/update/:cartId', authMiddleware, cleanBody, cartController.editCart); |
75 |
| - |
76 |
| - const req = { body: {}, params: { cartId: 'cart_id' }, user: { id: 'user_id' } }; |
77 |
| - const res = { json: jest.fn() }; |
78 |
| - const next = jest.fn(); |
79 |
| - |
80 |
| - await request(router) |
81 |
| - .put(`/update/${req.params.cartId}`) |
82 |
| - .send(req.body) |
83 |
| - .set('Authorization', 'Bearer token'); |
84 |
| - |
85 |
| - expect(authMiddleware).toHaveBeenCalled(); |
86 |
| - expect(cleanBody).toHaveBeenCalled(); |
87 |
| - expect(cartController.editCart).toHaveBeenCalledWith(req, res, next); |
88 |
| - }); |
89 |
| - |
90 |
| - it('should call the appropriate methods when DELETE /delete/:cartId is called', async () => { |
91 |
| - router.delete('/delete/:cartId', authMiddleware, cartController.deleteCart); |
92 |
| - |
93 |
| - const req ={ params: { cartId: 'cart_id' }, user: { id: 'user_id' } }; |
94 |
| - const res = { json: jest.fn() }; |
95 |
| - const next = jest.fn(); |
96 |
| - |
97 |
| - await request(router).delete(`/delete/${req.params.cartId}`).set('Authorization', 'Bearer token'); |
98 |
| - |
99 |
| - expect(authMiddleware).toHaveBeenCalled(); |
100 |
| - expect(cartController.deleteCart).toHaveBeenCalledWith(req, res, next); |
101 |
| - }); |
102 |
| -}); |
0 commit comments