Open gurindercoderabbitai[bot] opened 3 months ago
@coderabbitai can you help me write a unit test for this file: https://github.com/GurinderRawala/server-permiles/blob/main/src/driver/driver-operations.js
[!TIP] For best results, initiate chat on the files or code changes.
@GurinderRawala, here are some example unit tests for the addDriver
and getDriverByEmail
functions from the driver-operations.js
file. These tests use the jest
framework and mock the dependencies:
const { addDriver, getDriverByEmail } = require('./driver-operations');
describe('Driver Operations', () => {
let mockDriverRepo;
let mockLog;
let callback;
beforeEach(() => {
mockDriverRepo = {
create: jest.fn(),
findOne: jest.fn(),
};
mockLog = {
info: jest.fn(),
error: jest.fn(),
};
callback = jest.fn();
});
describe('addDriver', () => {
it('should add a driver successfully', async () => {
const driver = { firstname: 'John' };
mockDriverRepo.create.mockResolvedValue({ id: 1 });
await addDriver(mockDriverRepo, mockLog, driver, callback);
expect(mockDriverRepo.create).toHaveBeenCalledWith(driver);
expect(mockLog.info).toHaveBeenCalledWith({ driver }, 'adding driver');
expect(callback).toHaveBeenCalledWith(null, {
msg: 'John has been added as a driver',
});
});
it('should handle errors when adding a driver', async () => {
const driver = { firstname: 'John' };
const error = new Error('Failed to add driver');
mockDriverRepo.create.mockRejectedValue(error);
await addDriver(mockDriverRepo, mockLog, driver, callback);
expect(mockLog.error).toHaveBeenCalledWith(error);
expect(callback).toHaveBeenCalledWith(error);
});
});
describe('getDriverByEmail', () => {
it('should retrieve driver by email successfully', async () => {
const driver = { email: 'john@example.com' };
const driverData = { id: 1, email: 'john@example.com' };
mockDriverRepo.findOne.mockResolvedValue(driverData);
await getDriverByEmail(mockDriverRepo, mockLog, driver, callback);
expect(mockDriverRepo.findOne).toHaveBeenCalledWith({
where: { email: driver.email },
});
expect(mockLog.info).toHaveBeenCalledWith(
{ res: driverData },
'driver retrived successfully'
);
expect(callback).toHaveBeenCalledWith(null, driverData);
});
it('should handle errors when retrieving driver by email', async () => {
const driver = { email: 'john@example.com' };
const error = new Error('Driver not found');
mockDriverRepo.findOne.mockRejectedValue(error);
await getDriverByEmail(mockDriverRepo, mockLog, driver, callback);
expect(mockLog.error).toHaveBeenCalledWith(
{ err: error },
'Error finding driver'
);
expect(callback).toHaveBeenCalledWith(error);
});
});
});
These tests cover both successful and error scenarios for the addDriver
and getDriverByEmail
functions. You can expand these tests to cover other functions in the file similarly. Let me know if you need further assistance!
@coderabbitai remove the learning you have used earlier it is not valid anymore
This issue is to track the task of adding unit tests for the driver-operations.js file as discussed in this comment on PR #37. Adding unit tests will help ensure the reliability and correctness of the code.