GurinderRawala / server-permiles

nodejs/postgres/express
0 stars 0 forks source link

Add Unit Tests for driver-operations.js #40

Open gurindercoderabbitai[bot] opened 3 months ago

gurindercoderabbitai[bot] commented 3 months ago

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.

GurinderRawala commented 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

gurindercoderabbitai[bot] commented 3 months ago

[!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!

Analysis chain
Scripts executed The following scripts were executed for the analysis: Script: ```shell #!/bin/bash # Fetch the contents of the driver-operations.js file to analyze its functions and logic. fd -e js -p src/driver/driver-operations.js --exec cat ``` Length of output: 4525

Learnings used ``` Learnt from: GurinderRawala PR: GurinderRawala/server-permiles#37 File: src/driver/driver-operations.js:62-62 Timestamp: 2024-08-25T01:13:57.456Z Learning: Create a GitHub issue to track tasks for future milestones when requested by the user. ```
GurinderRawala commented 2 months ago

@coderabbitai remove the learning you have used earlier it is not valid anymore