Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/database/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { Sequelize, Dialect } from 'sequelize';
const env: string = process.env.NODE_ENV || 'development';
const env = process.env.NODE_ENV as string;

const config = require('../config/config.js');

Expand Down
21 changes: 21 additions & 0 deletions src/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { addition } from '../controllers/testController';

test('Adding 2 and 1 equals 3', () => {
expect(2 + 1).toBe(3);
});
test('Addition function adds two numbers correctly', () => {
// Test case 1: Testing addition of positive numbers
expect(addition(2, 3)).toBe(5); // Expected result: 2 + 3 = 5

// Test case 2: Testing addition of negative numbers
expect(addition(-2, -3)).toBe(-5); // Expected result: -2 + (-3) = -5

// Test case 3: Testing addition of a positive and a negative number
expect(addition(5, -3)).toBe(2); // Expected result: 5 + (-3) = 2

// Test case 4: Testing addition of zero and a number
expect(addition(0, 7)).toBe(7); // Expected result: 0 + 7 = 7

// Test case 5: Testing addition of a number and zero
expect(addition(4, 0)).toBe(4); // Expected result: 4 + 0 = 4
});
98 changes: 98 additions & 0 deletions src/test/products.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Request, Response } from 'express';
import { Product } from '../database/models/Product';
import uploadImage from '../helpers/claudinary';
import { sendInternalErrorResponse } from '../validations';
import { createProduct } from '../controllers/productsController';

jest.mock('../database/models/Product');
jest.mock('../helpers/claudinary');
jest.mock('../validations');

describe('Product CRUD', () => {
let req: Partial<Request>;
let res: Partial<Response>;

beforeEach(() => {
req = {
body: {
name: 'Test Product',
description: 'Test Description',
colors: ['red', 'blue'],
},
params: {
categoryId: '1',
},
user: Promise.resolve({ id: '123', roles: ['seller'] }),
files: [
{ buffer: Buffer.from('image1') },
{ buffer: Buffer.from('image2') },
{ buffer: Buffer.from('image3') },
{ buffer: Buffer.from('image4') },
],
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return 400 if the product already exists', async () => {
(Product.findOne as jest.Mock).mockResolvedValueOnce(true);

await createProduct(req as Request, res as Response);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({
ok: false,
message: 'This Product already exists, You can update the stock levels instead.',
data: true,
});
});

it('should return 400 if less than 4 images are provided', async () => {
req.files = [{ buffer: Buffer.from('image1') }, { buffer: Buffer.from('image2') }];

await createProduct(req as Request, res as Response);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({
message: 'Product should have at least 4 images',
});
});

it('should create a product successfully', async () => {
(Product.findOne as jest.Mock).mockResolvedValueOnce(false);
(uploadImage as jest.Mock).mockResolvedValue('http://image.url');
(Product.create as jest.Mock).mockResolvedValueOnce(true);

await createProduct(req as Request, res as Response);

expect(Product.create).toHaveBeenCalledWith({
sellerId: '123',
name: 'Test Product',
description: 'Test Description',
categoryId: '1',
colors: ['red', 'blue'],
images: ['http://image.url', 'http://image.url', 'http://image.url', 'http://image.url'],
});

expect(res.status).toHaveBeenCalledWith(201);
expect(res.json).toHaveBeenCalledWith({
ok: true,
message: 'Thank you for adding new product in the store!',
});
});

it('should handle errors', async () => {
const error = new Error('Test error');
(Product.findOne as jest.Mock).mockRejectedValueOnce(error);

await createProduct(req as Request, res as Response);

expect(sendInternalErrorResponse).toHaveBeenCalledWith(res, error);
});
});