fix: update base-repository unit tests to support shouldFlatten

This commit is contained in:
yassinedorbozgithub
2025-05-06 15:37:01 +01:00
parent f5dec41b34
commit c1c92475a3
2 changed files with 40 additions and 1 deletions

View File

@@ -12,10 +12,18 @@ import { Model, Types } from 'mongoose';
import { DummyRepository } from '@/utils/test/dummy/repositories/dummy.repository';
import { closeInMongodConnection } from '@/utils/test/test';
import { flatten } from '../helpers/flatten';
import { DummyModule } from '../test/dummy/dummy.module';
import { Dummy } from '../test/dummy/schemas/dummy.schema';
import { buildTestingMocks } from '../test/utils';
import { BaseSchema } from './base-schema';
const FLATTEN_PAYLOAD = {
dummy: 'updated dummy text',
dynamicField: { field1: 'value1', field2: 'value2' },
} as const satisfies Omit<Dummy, keyof BaseSchema>;
describe('BaseRepository', () => {
let dummyModel: Model<Dummy>;
let dummyRepository: DummyRepository;
@@ -140,6 +148,32 @@ describe('BaseRepository', () => {
});
});
it('should updated and flatten by id and return one dummy data', async () => {
jest.spyOn(dummyModel, 'findOneAndUpdate');
const result = await dummyRepository.updateOne(
createdId,
FLATTEN_PAYLOAD,
{
new: true,
shouldFlatten: true,
},
);
expect(dummyModel.findOneAndUpdate).toHaveBeenCalledWith(
{ _id: createdId },
{
$set: flatten(FLATTEN_PAYLOAD),
},
{
new: true,
},
);
expect(result).toEqualPayload({
dummy: 'updated dummy text',
dynamicField: { field1: 'value1', field2: 'value2' },
});
});
it('should update by id and invoke lifecycle hooks', async () => {
const created = await dummyRepository.create({ dummy: 'initial text' });
const mockUpdate = { dummy: 'updated dummy text' };

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 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.
@@ -19,6 +19,11 @@ export class Dummy extends BaseSchema {
required: true,
})
dummy: string;
@Prop({
type: Object,
})
dynamicField?: Record<string, any> | undefined;
}
export type DummyDocument = THydratedDocument<Dummy>;