Skip to content

Commit

Permalink
API change for being more testable and standard compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
jormaechea committed Jun 14, 2019
1 parent e0d00d2 commit 2da42cd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/api-schema",
"version": "0.1.0",
"version": "1.0.0",
"description": "A package to handle JANIS Views Schema APIs",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 7 additions & 4 deletions src/api-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ const YAML = require('yamljs');

class ApiSchema {

constructor(entity, action) {
this.entity = entity;
this.action = action;
get pathParameters() {
return this._pathParameters;
}

set pathParameters(pathParameters) {
this._pathParameters = pathParameters;
}

get schemaPath() {
return `./view-schemas/${this.entity}/${this.action}.yml`;
return `./view-schemas/${this.pathParameters.entity}/${this.pathParameters.action}.yml`;
}

validate() {
Expand Down
24 changes: 20 additions & 4 deletions tests/api-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ describe('Schema API', function() {

const fsAccessStub = sandbox.stub(fs, 'access');
fsAccessStub.callsFake((filePath, callback) => callback(new Error()));
const schemaApi = new ApiSchema('someEntity', 'someAction');
const schemaApi = new ApiSchema();
schemaApi.pathParameters = {
entity: 'someEntity',
action: 'someAction'
};

await assert.rejects(() => schemaApi.validate());

Expand All @@ -36,7 +40,11 @@ describe('Schema API', function() {

const fsAccessStub = sandbox.stub(fs, 'access');
fsAccessStub.callsFake((filePath, callback) => callback());
const schemaApi = new ApiSchema('someEntity', 'someAction');
const schemaApi = new ApiSchema();
schemaApi.pathParameters = {
entity: 'someEntity',
action: 'someAction'
};

const validationResult = await schemaApi.validate();

Expand All @@ -55,7 +63,11 @@ describe('Schema API', function() {
const yamlLoadStub = sandbox.stub(YAML, 'load');
yamlLoadStub.throws(new Error('Some fake error'));

const schemaApi = new ApiSchema('someEntity', 'someAction');
const schemaApi = new ApiSchema();
schemaApi.pathParameters = {
entity: 'someEntity',
action: 'someAction'
};
const response = await schemaApi.process();

assert.deepStrictEqual(response, {
Expand All @@ -73,7 +85,11 @@ describe('Schema API', function() {
const yamlLoadStub = sandbox.stub(YAML, 'load');
yamlLoadStub.returns(schema);

const schemaApi = new ApiSchema('someEntity', 'someAction');
const schemaApi = new ApiSchema();
schemaApi.pathParameters = {
entity: 'someEntity',
action: 'someAction'
};
const response = await schemaApi.process();

assert.deepStrictEqual(response, { body: schema });
Expand Down

0 comments on commit 2da42cd

Please sign in to comment.