Skip to content

Commit

Permalink
feat(DTFS2-7793): update service unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Roberts committed Jan 31, 2025
1 parent bbab6eb commit f98e29e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class PortalFacilityAmendmentService {
}

/**
* Updates a portal facility amendment with the provided details.
* Updates a portal facility amendment
*
* @param params
* @param params.amendmentId - The amendment id
Expand Down Expand Up @@ -114,7 +114,7 @@ export class PortalFacilityAmendmentService {
}

/**
* Updates a portal facility amendment with the provided details.
* Updates a portal facility amendment with user provided values.
*
* @param params
* @param params.amendmentId - The amendment id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TfmFacilitiesRepo } from '../../repositories/tfm-facilities-repo';

const mockUpdatePortalFacilityAmendmentByAmendmentId = jest.fn();
const mockFindOneAmendmentByFacilityIdAndAmendmentId = jest.fn();
const mockValidateAmendmentIsComplete = jest.fn();

const amendmentId = new ObjectId().toString();
const facilityId = new ObjectId().toString();
Expand All @@ -27,6 +28,8 @@ describe('PortalFacilityAmendmentService', () => {
jest.spyOn(TfmFacilitiesRepo, 'updatePortalFacilityAmendmentByAmendmentId').mockImplementation(mockUpdatePortalFacilityAmendmentByAmendmentId);
jest.spyOn(TfmFacilitiesRepo, 'findOneAmendmentByFacilityIdAndAmendmentId').mockImplementation(mockFindOneAmendmentByFacilityIdAndAmendmentId);

jest.spyOn(PortalFacilityAmendmentService, 'validateAmendmentIsComplete').mockImplementation(mockValidateAmendmentIsComplete);

mockUpdatePortalFacilityAmendmentByAmendmentId.mockResolvedValue({});
mockFindOneAmendmentByFacilityIdAndAmendmentId.mockResolvedValue(updatedAmendment);
});
Expand All @@ -36,6 +39,23 @@ describe('PortalFacilityAmendmentService', () => {
});

describe('submitPortalFacilityAmendmentToChecker', () => {
it('should call PortalFacilityAmendmentService.validateAmendmentIsComplete', async () => {
// Act
await PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
});

// Assert
expect(mockValidateAmendmentIsComplete).toHaveBeenCalledTimes(1);
expect(mockValidateAmendmentIsComplete).toHaveBeenCalledWith({
facilityId,
amendmentId,
});
});

it('should call TfmFacilitiesRepo.updatePortalFacilityAmendmentByAmendmentId with the correct params', async () => {
// Act
await PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
Expand All @@ -56,66 +76,67 @@ describe('PortalFacilityAmendmentService', () => {
facilityId: new ObjectId(facilityId),
amendmentId: new ObjectId(amendmentId),
auditDetails,
allowedStatuses: [PORTAL_AMENDMENT_STATUS.DRAFT],
});
});
});

it('should call TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId with the correct params', async () => {
// Act
await PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
it('should call TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId with the correct params', async () => {
// Act
await PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
});

// Assert
expect(mockFindOneAmendmentByFacilityIdAndAmendmentId).toHaveBeenCalledTimes(1);
expect(mockFindOneAmendmentByFacilityIdAndAmendmentId).toHaveBeenCalledWith(new ObjectId(facilityId), new ObjectId(amendmentId));
});

// Assert
expect(mockFindOneAmendmentByFacilityIdAndAmendmentId).toHaveBeenCalledTimes(1);
expect(mockFindOneAmendmentByFacilityIdAndAmendmentId).toHaveBeenCalledWith(new ObjectId(facilityId), new ObjectId(amendmentId));
});
it('should return the result of TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId', async () => {
// Act
const expected = await PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
});

it('should return the result of TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId', async () => {
// Act
const expected = await PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
// Assert
expect(expected).toEqual(updatedAmendment);
});

// Assert
expect(expected).toEqual(updatedAmendment);
});
it('should throw an error if no amendment is found when calling TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId', async () => {
// Arrange
mockFindOneAmendmentByFacilityIdAndAmendmentId.mockResolvedValueOnce(null);

it('should throw an error if no amendment is found when calling TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId', async () => {
// Arrange
mockFindOneAmendmentByFacilityIdAndAmendmentId.mockResolvedValueOnce(null);
// Act
const returned = PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
});

// Act
const returned = PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
// Assert
await expect(returned).rejects.toThrow(new Error('Could not find amendment to return'));
});

// Assert
await expect(returned).rejects.toThrow(new Error('Could not find amendment to return'));
});
it(`should throw an error if an amendment without a ${AMENDMENT_TYPES.PORTAL} amendment type is returned from TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId`, async () => {
// Arrange
mockFindOneAmendmentByFacilityIdAndAmendmentId.mockResolvedValueOnce({ ...updatedAmendment, type: AMENDMENT_TYPES.TFM });

it(`should throw an error if an amendment without a ${AMENDMENT_TYPES.PORTAL} amendment type is returned from TfmFacilitiesRepo.findOneAmendmentByFacilityIdAndAmendmentId`, async () => {
// Arrange
mockFindOneAmendmentByFacilityIdAndAmendmentId.mockResolvedValueOnce({ ...updatedAmendment, type: AMENDMENT_TYPES.TFM });
// Act
const returned = PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
});

// Act
const returned = PortalFacilityAmendmentService.submitPortalFacilityAmendmentToChecker({
amendmentId,
facilityId,
dealId,
auditDetails,
// Assert
await expect(returned).rejects.toThrow(new Error('Could not find amendment to return'));
});

// Assert
await expect(returned).rejects.toThrow(new Error('Could not find amendment to return'));
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AMENDMENT_TYPES } from '@ukef/dtfs2-common';
import { AMENDMENT_TYPES, PORTAL_AMENDMENT_STATUS } from '@ukef/dtfs2-common';
import { ObjectId } from 'mongodb';
import { getUnixTime } from 'date-fns';
import { generatePortalAuditDetails } from '@ukef/dtfs2-common/change-stream';
Expand All @@ -13,9 +13,10 @@ const mockFindOneAmendmentByFacilityIdAndAmendmentId = jest.fn();
const amendmentId = new ObjectId().toString();
const facilityId = new ObjectId().toString();
const update = {
changeCoverStartDate: true,
isUsingFacilityEndDate: true,
facilityEndDate: new Date(),
bankReviewDate: null,
changeCoverEndDate: true,
};
const updatedAmendment = { ...aPortalFacilityAmendment(), ...update };
const auditDetails = generatePortalAuditDetails(aPortalUser()._id);
Expand Down Expand Up @@ -61,6 +62,7 @@ describe('PortalFacilityAmendmentService', () => {
facilityId: new ObjectId(facilityId),
amendmentId: new ObjectId(amendmentId),
auditDetails,
allowedStatuses: [PORTAL_AMENDMENT_STATUS.DRAFT],
});
});
});
Expand Down

0 comments on commit f98e29e

Please sign in to comment.