Skip to content

Commit

Permalink
remove unnecessary conditionals & add test if API has modelName
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Oruezabal committed Dec 6, 2021
1 parent da25a05 commit 7db4c9b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/api-get.js
Expand Up @@ -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
};
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/api-get.js
Expand Up @@ -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);
});
});

});

0 comments on commit 7db4c9b

Please sign in to comment.