From b97b78337d9eecf09fb8074be9f0b9064c77bd3f Mon Sep 17 00:00:00 2001 From: ronaldsg Date: Mon, 23 Sep 2024 15:29:44 -0500 Subject: [PATCH] Fixing model cloning and tests --- src/__tests__/unit/features.controller.unit.ts | 12 ++++++------ src/controllers/features.controller.ts | 1 - src/models/features-data.model.ts | 18 +++++++----------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/__tests__/unit/features.controller.unit.ts b/src/__tests__/unit/features.controller.unit.ts index 451f7841..c306639f 100644 --- a/src/__tests__/unit/features.controller.unit.ts +++ b/src/__tests__/unit/features.controller.unit.ts @@ -7,26 +7,26 @@ import { FeaturesDataService } from '../../services'; import { TermsDataService } from '../../services/terms-data.service'; import { StubbedInstanceWithSinonAccessor} from "@loopback/testlab"; import { FeaturesMongoDbDataService } from '../../services/features-mongo.service'; -import { FeaturesDbDataModel } from '../../models/features-data.model'; import { TermsMongoDbDataService } from '../../services/terms-mongo.service'; describe('FeaturesController (unit)', () => { let mockedService: StubbedInstanceWithSinonAccessor; let mockedTermsService:StubbedInstanceWithSinonAccessor; + let featuresGetAllStub: sinon.SinonStub; let context = stubExpressContext(); mockedService = createStubInstance(FeaturesMongoDbDataService); mockedTermsService = createStubInstance(TermsMongoDbDataService); - + featuresGetAllStub = mockedService.getAll as sinon.SinonStub; describe('get()',() => { it('retrieves the features flags Information', async() => { - mockedService.stubs.getAll.resolves([FeaturesDbDataModel.clone({ + featuresGetAllStub.resolves([{ name: 'feature1', value: true, - creationDate: undefined, - lastUpdateDate: undefined, + creationDate: new Date(), + lastUpdateDate: new Date(), enabled: false, version: 0, - })]); + }]); mockedTermsService.stubs.getVersion.resolves(); const controller = new FeaturesController(context.response, mockedService, mockedTermsService); const response = await controller.get(); diff --git a/src/controllers/features.controller.ts b/src/controllers/features.controller.ts index a22538df..cde4eb7a 100644 --- a/src/controllers/features.controller.ts +++ b/src/controllers/features.controller.ts @@ -69,7 +69,6 @@ export class FeaturesController { .send(features); resolve(this.response); } - this.logger.info(`[get] Retrieved terms idx: ${termsIdx}`); return Promise.all([this.termsDatService.getVersion(features[termsIdx].version), termsIdx]); }) .then(([terms, termsIdx]) => { diff --git a/src/models/features-data.model.ts b/src/models/features-data.model.ts index b6987cdf..2c3cd335 100644 --- a/src/models/features-data.model.ts +++ b/src/models/features-data.model.ts @@ -61,18 +61,14 @@ export class FeaturesDbDataModel implements SearchableModel, FeaturesDataModel { return 'name'; } - public static clone(other: Partial): FeaturesDbDataModel { - const sanitizedData: Partial = {}; + public static clone(other:FeaturesDbDataModel): FeaturesDbDataModel { const features: FeaturesDbDataModel = new FeaturesDbDataModel(); - Object.entries(other).forEach(([key, value]) => { - const theKey = key as keyof FeaturesDbDataModel; - if (value !== undefined) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - sanitizedData[theKey] = value; - } - }); - Object.assign(features, sanitizedData); + features.creationDate = other.creationDate; + features.lastUpdateDate = other.lastUpdateDate; + features.name = other.name; + features.value = other.value; + features.version = other.version; + features.enabled = other.enabled; return features; }