-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Yazmyn-S/Company
WIP Company: Red Tests
- Loading branch information
Showing
3 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const companyController = require('../controllers/companyController'); | ||
const Company = require('../models/Company'); | ||
|
||
// Mock the Company model | ||
jest.mock('../models/Company'); | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
app.use("/api/companies", require("../routes/companyRoutes")); | ||
|
||
describe('Company Controller', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('POST /api/companies', () => { | ||
it('should create a new company', async () => { | ||
const companyData = { | ||
companyId: 'new-company-id', | ||
CompanyName: 'New Test Company', | ||
CompanyType: 'corporation', // Assuming 'corporation' is one of the valid enum values | ||
RegisteredAddress: '456 New Avenue, New City, NC', | ||
TaxID: '987-65-4321', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
Company.prototype.save.mockResolvedValue(companyData); | ||
|
||
const response = await request(app) | ||
.post('/api/companies') | ||
.send(companyData); | ||
|
||
expect(response.status).toBe(201); | ||
expect(response.body).toEqual(companyData); | ||
}); | ||
|
||
it('should return 400 if required fields are missing', async () => { | ||
const response = await request(app) | ||
.post('/api/companies') | ||
.send({}); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body).toEqual({ message: 'Invalid company data' }); | ||
}); | ||
}); | ||
|
||
describe('GET /api/companies', () => { | ||
it('should get all companies', async () => { | ||
const companies = [ | ||
{ | ||
companyId: 'company-id-1', | ||
CompanyName: 'Company 1', | ||
CompanyType: 'startup', | ||
RegisteredAddress: '123 Test Street, Test City, TC', | ||
TaxID: '111-22-3333', | ||
corporationDate: new Date(), | ||
}, | ||
{ | ||
companyId: 'company-id-2', | ||
CompanyName: 'Company 2', | ||
CompanyType: 'corporation', | ||
RegisteredAddress: '456 Test Avenue, Test City, TC', | ||
TaxID: '444-55-6666', | ||
corporationDate: new Date(), | ||
}, | ||
]; | ||
|
||
Company.find.mockResolvedValue(companies); | ||
|
||
const response = await request(app).get('/api/companies'); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual(companies); | ||
}); | ||
|
||
it('should return 404 if no companies are found', async () => { | ||
Company.find.mockResolvedValue([]); | ||
|
||
const response = await request(app).get('/api/companies'); | ||
|
||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ message: 'No companies found' }); | ||
}); | ||
}); | ||
|
||
describe('GET /api/companies/:id', () => { | ||
it('should get a company by id', async () => { | ||
const company = { | ||
companyId: 'company-id-1', | ||
CompanyName: 'Company 1', | ||
CompanyType: 'startup', | ||
RegisteredAddress: '123 Test Street, Test City, TC', | ||
TaxID: '111-22-3333', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
Company.findById.mockResolvedValue(company); | ||
|
||
const response = await request(app).get(`/api/companies/${company.companyId}`); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual(company); | ||
}); | ||
|
||
it('should return 404 if company is not found', async () => { | ||
Company.findById.mockResolvedValue(null); | ||
|
||
const response = await request(app).get(`/api/companies/${mongoose.Types.ObjectId()}`); | ||
|
||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ message: 'Company not found' }); | ||
}); | ||
}); | ||
|
||
describe('PUT /api/companies/:id', () => { | ||
it('should update a company by id', async () => { | ||
const companyId = 'company-id-1'; | ||
const updatedCompany = { | ||
companyId, | ||
CompanyName: 'Updated Company', | ||
CompanyType: 'corporation', | ||
RegisteredAddress: '789 Updated Road, Updated City, UC', | ||
TaxID: '111-22-3333', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
Company.findByIdAndUpdate.mockResolvedValue(updatedCompany); | ||
|
||
const response = await request(app) | ||
.put(`/api/companies/${companyId}`) | ||
.send(updatedCompany); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual(updatedCompany); | ||
}); | ||
|
||
it('should return 404 if company to update is not found', async () => { | ||
Company.findByIdAndUpdate.mockResolvedValue(null); | ||
|
||
const response = await request(app) | ||
.put(`/api/companies/${mongoose.Types.ObjectId()}`) | ||
.send({ CompanyName: 'Updated Company' }); | ||
|
||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ message: 'Company not found' }); | ||
}); | ||
}); | ||
|
||
describe('DELETE /api/companies/:id', () => { | ||
it('should delete a company by id', async () => { | ||
const companyId = 'company-id-1'; | ||
Company.findByIdAndDelete.mockResolvedValue({ companyId }); | ||
|
||
const response = await request(app).delete(`/api/companies/${companyId}`); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ message: 'Company deleted' }); | ||
}); | ||
|
||
it('should return 404 if company to delete is not found', async () => { | ||
Company.findByIdAndDelete.mockResolvedValue(null); | ||
|
||
const response = await request(app).delete(`/api/companies/${mongoose.Types.ObjectId()}`); | ||
|
||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ message: 'Company not found' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const mongoose = require('mongoose'); | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
const Company = require('../models/Company'); | ||
const { connectDB, disconnectDB } = require('../db'); // Adjust according to your database connection setup | ||
|
||
beforeAll(async function () { | ||
await connectDB(); // Ensure this matches your database connection logic | ||
}); | ||
|
||
afterAll(async function () { | ||
await mongoose.connection.db.dropDatabase(); | ||
await mongoose.connection.close(); | ||
}); | ||
|
||
describe('Company Model', function () { | ||
it('should create a company with valid fields', async function () { | ||
const companyData = { | ||
companyId: 'valid-company-id', | ||
CompanyName: 'Test Company', | ||
CompanyType: 'startup', // Assuming 'startup' is a valid enum value | ||
RegisteredAddress: '123 Test Street, Test City, TC', | ||
TaxID: '123-45-6789', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
const company = new Company(companyData); | ||
const savedCompany = await company.save(); | ||
|
||
expect(savedCompany.companyId).to.equal(companyData.companyId); | ||
expect(savedCompany.CompanyName).to.equal(companyData.CompanyName); | ||
expect(savedCompany.CompanyType).to.equal(companyData.CompanyType); | ||
expect(savedCompany.RegisteredAddress).to.equal(companyData.RegisteredAddress); | ||
expect(savedCompany.TaxID).to.equal(companyData.TaxID); | ||
expect(new Date(savedCompany.corporationDate).toISOString()).to.equal(new Date(companyData.corporationDate).toISOString()); | ||
}); | ||
|
||
it('should not create a company without required fields', async function () { | ||
const companyData = { | ||
CompanyName: 'Test Company', | ||
}; | ||
|
||
const company = new Company(companyData); | ||
|
||
try { | ||
await company.save(); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error.errors.companyId).to.exist; | ||
expect(error.errors.CompanyType).to.exist; | ||
expect(error.errors.RegisteredAddress).to.exist; | ||
expect(error.errors.corporationDate).to.exist; | ||
} | ||
}); | ||
|
||
it('should not create a company with duplicate companyId', async function () { | ||
const companyData1 = { | ||
companyId: 'duplicate-company-id', | ||
CompanyName: 'First Company', | ||
CompanyType: 'startup', | ||
RegisteredAddress: '123 First Street, First City, FC', | ||
TaxID: '111-22-3333', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
const companyData2 = { | ||
companyId: 'duplicate-company-id', | ||
CompanyName: 'Second Company', | ||
CompanyType: 'corporation', | ||
RegisteredAddress: '456 Second Avenue, Second City, SC', | ||
TaxID: '444-55-6666', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
const company1 = new Company(companyData1); | ||
await company1.save(); | ||
|
||
const company2 = new Company(companyData2); | ||
|
||
try { | ||
await company2.save(); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error.code).to.equal(11000); // Duplicate key error code | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const request = require('supertest'); | ||
const app = require('../app'); // Your Express app | ||
const mongoose = require('mongoose'); | ||
const Company = require('../models/Company'); | ||
|
||
beforeAll(async () => { | ||
await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true }); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await Company.deleteMany({}); // Clear companies collection | ||
const company = new Company({ | ||
companyId: 'test-company-id', | ||
CompanyName: 'Test Company', | ||
CompanyType: 'startup', // Assuming 'startup' is one of the valid enum values | ||
RegisteredAddress: '123 Test Street, Test City, TC', | ||
TaxID: '123-45-6789', | ||
corporationDate: new Date(), | ||
}); | ||
await company.save(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.connection.close(); | ||
}); | ||
|
||
describe('Company API Test', () => { | ||
it('should get all companies', async () => { | ||
const res = await request(app).get('/api/companies'); | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.body).toBeInstanceOf(Array); | ||
expect(res.body.length).toBeGreaterThan(0); // Ensure that the array is not empty | ||
}); | ||
|
||
it('should create a new company', async () => { | ||
const newCompany = { | ||
companyId: 'new-company-id', | ||
CompanyName: 'New Test Company', | ||
CompanyType: 'corporation', // Assuming 'corporation' is one of the valid enum values | ||
RegisteredAddress: '456 New Avenue, New City, NC', | ||
TaxID: '987-65-4321', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
const res = await request(app).post('/api/companies').send(newCompany); | ||
expect(res.statusCode).toEqual(201); | ||
expect(res.body.companyId).toEqual(newCompany.companyId); | ||
expect(res.body.CompanyName).toEqual(newCompany.CompanyName); | ||
expect(res.body.CompanyType).toEqual(newCompany.CompanyType); | ||
expect(res.body.RegisteredAddress).toEqual(newCompany.RegisteredAddress); | ||
expect(res.body.TaxID).toEqual(newCompany.TaxID); | ||
expect(new Date(res.body.corporationDate).toISOString()).toEqual(newCompany.corporationDate.toISOString()); | ||
}); | ||
|
||
it('should update an existing company', async () => { | ||
const company = await Company.findOne(); // Fetch the existing company | ||
|
||
const updatedData = { | ||
CompanyName: 'Updated Test Company', | ||
CompanyType: 'corporation', | ||
RegisteredAddress: '789 Updated Road, Updated City, UC', | ||
TaxID: '111-22-3333', | ||
corporationDate: new Date(), | ||
}; | ||
|
||
const res = await request(app).put(`/api/companies/${company._id}`).send(updatedData); | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.CompanyName).toEqual(updatedData.CompanyName); | ||
expect(res.body.CompanyType).toEqual(updatedData.CompanyType); | ||
expect(res.body.RegisteredAddress).toEqual(updatedData.RegisteredAddress); | ||
expect(res.body.TaxID).toEqual(updatedData.TaxID); | ||
expect(new Date(res.body.corporationDate).toISOString()).toEqual(updatedData.corporationDate.toISOString()); | ||
}); | ||
|
||
it('should delete a company', async () => { | ||
const company = await Company.findOne(); // Fetch the existing company | ||
|
||
const res = await request(app).delete(`/api/companies/${company._id}`); | ||
expect(res.statusCode).toEqual(200); | ||
|
||
const deletedCompany = await Company.findById(company._id); | ||
expect(deletedCompany).toBeNull(); | ||
}); | ||
}); |