From 7db4c9bd63e0faae293e95ec980418eeafae1423 Mon Sep 17 00:00:00 2001 From: Mauricio Oruezabal Date: Mon, 6 Dec 2021 19:29:57 -0300 Subject: [PATCH] remove unnecessary conditionals & add test if API has modelName --- lib/api-get.js | 8 +++----- tests/api-get.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/api-get.js b/lib/api-get.js index 5e87b2f..d8e8ce5 100644 --- a/lib/api-get.js +++ b/lib/api-get.js @@ -47,7 +47,7 @@ module.exports = class ApiGet extends API { }; const getParams = { - filters: this._parseFilters ? this._parseFilters(filters) : filters, + filters: this._parseFilters(filters), page: 1, limit: 1 }; @@ -67,10 +67,9 @@ module.exports = class ApiGet extends API { [this.record] = record; - if(this.postGetValidate) - await this.postGetValidate(this.record); + await this.postGetValidate(this.record); - const response = this.format ? await this.format(this.record) : this.record; + const response = await this.format(this.record); this.setBody(response); } @@ -113,7 +112,6 @@ module.exports = class ApiGet extends API { if(!this.modelName) this.modelName = modelName; - this.modelName = modelName; this.recordId = recordId; this.parents = parents; } diff --git a/tests/api-get.js b/tests/api-get.js index 8a0f3f2..5ca77ad 100644 --- a/tests/api-get.js +++ b/tests/api-get.js @@ -561,6 +561,34 @@ describe('ApiGet', () => { mockRequire.stop(modelPath); }); + + it('Should validate APIs modelName if it has modelName', async () => { + + class MyApiGet extends ApiGet { + get modelName() { + return 'other-entity'; + } + } + + class OtherEntityModel { + async get() { + return []; + } + } + + const pathOtherEntity = path.join(process.cwd(), process.env.MS_PATH || '', 'models', 'other-entity'); + + mockRequire(pathOtherEntity, OtherEntityModel); + + const apiGet = new MyApiGet(); + apiGet.endpoint = '/some-entity/10'; + apiGet.pathParameters = ['10']; + + const validation = await apiGet.validate(); + assert.strictEqual(validation, true); + + mockRequire.stop(modelPath); + }); }); });