Skip to content

Commit

Permalink
buyer is now to with via stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyShimwa committed May 28, 2024
1 parent 262e48b commit 6f7d9fb
Show file tree
Hide file tree
Showing 13 changed files with 1,124 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = jest.fn((emailType, recipient, data) => {
console.log('Mock sendEmail called with:', { emailType, recipient, data });

Check warning on line 2 in src/__mocks__/index.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected console statement
return Promise.resolve({ id: 'mocked-id', message: 'Mocked email sent' });
});

7 changes: 7 additions & 0 deletions src/__mocks__/mailer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
sendCode: jest.fn((to, subject, htmlTemplatePath, replacements) => {
console.log('Mock sendCode called with:', { to, subject, htmlTemplatePath, replacements });

Check warning on line 3 in src/__mocks__/mailer.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected console statement
return Promise.resolve({ id: 'mocked-id', message: 'Mocked email sent' });
}),
};

75 changes: 75 additions & 0 deletions src/__test__/buyerController.tesst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import request from 'supertest';
import app from '../app';
import {
afterAllHook,
beforeAllHook,
getBuyerToken,
getVendorToken,
} from './testSetup';
beforeAll(beforeAllHook);
afterAll(afterAllHook);

describe('Buyer Controller test', () => {
let buyerToken: string;
let vendorToken: string;
let productId: number;
let categoryId: number;

beforeAll(async () => {
buyerToken = await getBuyerToken();
vendorToken = await getVendorToken();
});

it('should get a product by id', async () => {
// create a category
const categoryData = {
name: 'Category4',
description: 'category description',
};

const categoryResponse = await request(app)
.post('/api/v1/category')
.set('Authorization', `Bearer ${vendorToken}`)
.send(categoryData);

categoryId = categoryResponse.body.data.id;

const productData = {
name: 'New Product Two',
image: 'new_product.jpg',
gallery: [],
shortDesc: 'This is a new product',
longDesc: 'Detailed description of the new product',
categoryId: categoryId,
quantity: 10,
regularPrice: 5,
salesPrice: 4,
tags: ['tag1', 'tag2'],
type: 'Simple',
isAvailable: true,
};

const response = await request(app)
.post('/api/v1/product')
.set('Authorization', `Bearer ${vendorToken}`)
.send(productData);

productId = response.body.data.id;

const getResponse = await request(app)
.get(`/api/v1/buyer/get_product/${productId}`)
.set('Authorization', `Bearer ${buyerToken}`);


expect(getResponse.statusCode).toEqual(200);
expect(getResponse.body.msg).toEqual('Product retrieved successfully');
});

it('should return a 404 if product is not found', async () => {
const response = await request(app)
.get('/api/v1/buyer/get_product/5')
.set('Authorization', `Bearer ${buyerToken}`);
expect(response.status).toBe(404);
expect(response.body.msg).toBe('Product not found');
});
});
183 changes: 183 additions & 0 deletions src/__test__/category.tesst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import request from 'supertest';
import app from '../app';
import { afterAllHook, beforeAllHook, getVendorToken } from './testSetup';

beforeAll(beforeAllHook);
afterAll(afterAllHook);

describe('Category Creation Tests', () => {
beforeAll(async () => {
token = await getVendorToken();
});
let token: string;
let categoryId: number;

it('should create a new category with valid data', async () => {
const categoryData = {
name: 'Test Category',
description: 'Test category description',
};

const response = await request(app)
.post('/api/v1/category')
.set('Authorization', `Bearer ${token}`)
.send(categoryData);
expect(response.status).toBe(201);
expect(response.body.message).toBe('Category successfully created');
expect(response.body.data).toHaveProperty('id');
expect(response.body.data).toHaveProperty('name', categoryData.name);
expect(response.body.data).toHaveProperty(
'description',
categoryData.description
);
categoryId = response.body.data.id;
});

it('should return a 400 status code if name is missing', async () => {
const invalidData = {
description: 'Test category description',
};

const response = await request(app)
.post('/api/v1/category')
.set('Authorization', `Bearer ${token}`)
.send(invalidData);

expect(response.status).toBe(400);
expect(response.body.errors[0].msg).toBe('Category name is required');
});

it('should return 400 if request data is invalid', async () => {
const invalidData = {};

const response = await request(app)
.put(`/api/v1/category/${categoryId}`)
.set('Authorization', `Bearer ${token}`)
.send(invalidData);

expect(response.status).toBe(400);
expect(response.body.errors).toBeDefined();
});

it('should return a 409 status code if category name already exists', async () => {
const existingCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
};
await request(app)
.post('/api/v1/category')
.set('Authorization', `Bearer ${token}`)
.send(existingCategoryData);

const newCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
};
const response = await request(app)
.post('/api/v1/category')
.set('Authorization', `Bearer ${token}`)
.send(newCategoryData);

expect(response.status).toBe(409);
expect(response.body.message).toBe('Category name already exists');
});

it('should return all categories with status 200', async () => {
const response = await request(app).get('/api/v1/category');
expect(response.status).toBe(200);
expect(response.body.message).toBe('Data retrieved successfully');
expect(response.body.data).toBeDefined;
});

it('should return a category by ID with status 200', async () => {
const response = await request(app).get(`/api/v1/category/${categoryId}`);

expect(response.status).toBe(200);
expect(response.body.message).toBe('Data retrieved successfully');
expect(response.body.data).toBeDefined;
});

it('should return 404 if category is not found', async () => {
const nonExistentCategoryId = 9999;

const response = await request(app).get(
`/api/v1/category/${nonExistentCategoryId}`
);

expect(response.status).toBe(404);
expect(response.body.message).toBe('Category Not Found');
});

it('should update the category with status 200', async () => {
const updatedCategoryData = {
name: 'Updated Category Name',
description: 'Updated category description',
};

const response = await request(app)
.put(`/api/v1/category/${categoryId}`)
.set('Authorization', `Bearer ${token}`)
.send(updatedCategoryData);

expect(response.status).toBe(200);
expect(response.body.message).toBe('Category successfully updated');
expect(response.body.data.name).toBe(updatedCategoryData.name);
expect(response.body.data.description).toBe(
updatedCategoryData.description
);
});

it('should return a 409 status code if category update name already exists', async () => {
const existingCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
};
await request(app)
.post('/api/v1/category')
.set('Authorization', `Bearer ${token}`)
.send(existingCategoryData);

const updateCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
};
const response = await request(app)
.put(`/api/v1/category/${categoryId}`)
.set('Authorization', `Bearer ${token}`)
.send(updateCategoryData);

expect(response.status).toBe(409);
expect(response.body.message).toBe('Category name already exists');
});

it('should return 404 if category is not found', async () => {
const response = await request(app)
.put('/api/v1/category/9999')
.set('Authorization', `Bearer ${token}`)
.send({
name: 'Updated Category Name',
description: 'Updated category description',
});

expect(response.status).toBe(404);
expect(response.body.message).toBe('Category Not Found');
});

it('should delete the category with status 200', async () => {
const response = await request(app)
.delete(`/api/v1/category/${categoryId}`)
.set('Authorization', `Bearer ${token}`);

expect(response.status).toBe(200);
expect(response.body.message).toBe('Category deleted successfully');
});

it('should return 404 if category is not found', async () => {
const response = await request(app)
.delete('/api/v1/category/9999')
.set('Authorization', `Bearer ${token}`);

expect(response.status).toBe(404);
expect(response.body.message).toBe('Category Not Found');
});
});
Loading

0 comments on commit 6f7d9fb

Please sign in to comment.