fix: add unit tests

This commit is contained in:
abdou6666 2024-12-15 14:43:59 +01:00
parent c7b9434310
commit c326c453cf

View File

@ -6,6 +6,22 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Model, Types } from 'mongoose';
@ -162,6 +178,111 @@ describe('BaseRepository', () => {
expect.objectContaining({ dummy: 'updated dummy text' }),
);
});
it('should updateOne by id and trigger preUpdateValidate and postUpdateValidate methods', async () => {
const created = await dummyRepository.create({ dummy: 'initial text' });
const mockGetFilterValue = { _id: created.id };
const mockedGetFilter = jest.fn().mockReturnValue(mockGetFilterValue);
const mockGetUpdateValue = {
$set: {
value: 'updated dummy text',
},
};
const mockedGetUpdate = jest.fn().mockReturnValue(mockGetUpdateValue);
const mockQueryValue = {
getFilter: mockedGetFilter,
getUpdate: mockedGetUpdate,
lean: jest.fn(() => {
return {
exec: jest.fn(),
};
}),
};
jest
.spyOn(dummyModel, 'findOneAndUpdate')
.mockReturnValue(mockQueryValue as any);
const mockUpdate = { dummy: 'updated dummy text' };
const spyPreUpdateValidate = jest
.spyOn(dummyRepository, 'preUpdateValidate')
.mockResolvedValue();
const spyPostUpdateValidate = jest
.spyOn(dummyRepository, 'postUpdateValidate')
.mockResolvedValue();
const spyExecutoneOne = jest
.spyOn(
dummyRepository as DummyRepository & {
executeOne: () => Promise<{ dummy: string }>;
},
'executeOne',
)
.mockResolvedValue({ dummy: 'updated dummy text' });
await dummyRepository.updateOne(created.id, mockUpdate);
expect(spyPreUpdateValidate).toHaveBeenCalledWith(
created.id,
mockUpdate,
mockGetFilterValue,
mockGetUpdateValue,
);
expect(spyPostUpdateValidate).toHaveBeenCalledWith(
created.id,
mockUpdate,
mockGetFilterValue,
mockGetUpdateValue,
);
expect(spyExecutoneOne).toHaveBeenCalledWith(mockQueryValue, Dummy);
});
it('should throw an error while trying to updateOne when calling preUpdateValidate', async () => {
const created = await dummyRepository.create({ dummy: 'initial text' });
const mockGetFilterValue = { _id: created.id };
const mockedGetFilter = jest.fn().mockReturnValue(mockGetFilterValue);
const mockGetUpdateValue = {
$set: {
value: 10,
},
};
const mockedGetUpdate = jest.fn().mockReturnValue(mockGetUpdateValue);
const mockQueryValue = {
getFilter: mockedGetFilter,
getUpdate: mockedGetUpdate,
lean: jest.fn(() => {
return {
exec: jest.fn(),
};
}),
};
jest
.spyOn(dummyModel, 'findOneAndUpdate')
.mockReturnValue(mockQueryValue as any);
const mockUpdate = { dummy: 10 };
const spyPreUpdateValidate = jest
.spyOn(dummyRepository, 'preUpdateValidate')
.mockImplementation(() => {
throw new Error('Mocked error while validating dummy');
});
await expect(
dummyRepository.updateOne(created.id, mockUpdate),
).rejects.toThrow('Mocked error while validating dummy');
expect(spyPreUpdateValidate).toHaveBeenCalledWith(
created.id,
mockUpdate,
mockGetFilterValue,
mockGetUpdateValue,
);
});
});
describe('deleteOne', () => {