Skip to content

Commit

Permalink
Configuring for run test
Browse files Browse the repository at this point in the history
  • Loading branch information
sAchin-680 committed Sep 23, 2024
1 parent 68088f1 commit 99defd2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const express = require('express');
const dotenv = require('dotenv');
const morgan = require('morgan');
const { ApolloServer } = require('apollo-server-express');

const typeDefs = require('./graphql/schema');
const resolvers = require('./graphql/resolvers');
const rateLimiter = require('./middlewares/rateLimiter');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');
const setupSwagger = require('./swagger');
const compression = require('compression');
const { getProductById } = require('./controllers/productController');

dotenv.config();

Expand All @@ -19,6 +21,7 @@ app.use(morgan('dev'));
app.use(rateLimiter);
app.use(compression());
const csrfProtection = csrf({ cookie: true });
app.get('/api/v1/products/:id', getProductById);

app.use((req, res, next) => {
if (req.path === '/graphql') {
Expand Down
12 changes: 11 additions & 1 deletion tests/productRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@ const request = require('supertest');
const app = require('../app');
const Product = require('../models/Product');

// Mock Product model
jest.mock('../models/Product');

describe('GET /api/v1/products/:id', () => {
afterEach(() => {
// Clear all mocks after each test to ensure no cross-test interference
jest.clearAllMocks();
});

it('should return a product by id', async () => {
// Mock Product.findById to return a product
Product.findById.mockResolvedValue({ _id: '123', name: 'Sample Product' });

const res = await request(app).get('/api/v1/products/123');

// Ensure it returns status 200 and the correct product
expect(res.statusCode).toEqual(200);
expect(res.body).toHaveProperty('_id', '123');
expect(res.body).toHaveProperty('name', 'Sample Product');
});

it('should return 404 if product is not found', async () => {
// Mock Product.findById to return null (product not found)
Product.findById.mockResolvedValue(null);

const res = await request(app).get('/api/v1/products/123');

// Ensure it returns status 404 and the correct error message
expect(res.statusCode).toEqual(404);
expect(res.body).toHaveProperty('message', 'Product not found');
expect(res.body).toEqual({ message: 'Product not found' });
});
});

0 comments on commit 99defd2

Please sign in to comment.