diff --git a/api/src/utils/generics/base-repository.spec.ts b/api/src/utils/generics/base-repository.spec.ts index 95d19fdb..e26e8a10 100644 --- a/api/src/utils/generics/base-repository.spec.ts +++ b/api/src/utils/generics/base-repository.spec.ts @@ -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; + describe('BaseRepository', () => { let dummyModel: Model; 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' }; diff --git a/api/src/utils/test/dummy/schemas/dummy.schema.ts b/api/src/utils/test/dummy/schemas/dummy.schema.ts index edab8598..fe8acc06 100644 --- a/api/src/utils/test/dummy/schemas/dummy.schema.ts +++ b/api/src/utils/test/dummy/schemas/dummy.schema.ts @@ -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 | undefined; } export type DummyDocument = THydratedDocument;