Skip to content

Commit

Permalink
Fixing model cloning and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldsg20 committed Sep 23, 2024
1 parent 4a5ca88 commit b97b783
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/__tests__/unit/features.controller.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeaturesDataService>;
let mockedTermsService:StubbedInstanceWithSinonAccessor<TermsDataService>;
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();
Expand Down
1 change: 0 additions & 1 deletion src/controllers/features.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand Down
18 changes: 7 additions & 11 deletions src/models/features-data.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,14 @@ export class FeaturesDbDataModel implements SearchableModel, FeaturesDataModel {
return 'name';
}

public static clone(other: Partial<FeaturesDbDataModel>): FeaturesDbDataModel {
const sanitizedData: Partial<FeaturesDbDataModel> = {};
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;
}

Expand Down

0 comments on commit b97b783

Please sign in to comment.