From 2da42cd5be38408f2225668c6b9816d859082eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Ormaechea?= Date: Fri, 14 Jun 2019 13:13:14 -0300 Subject: [PATCH] API change for being more testable and standard compliant --- package-lock.json | 2 +- package.json | 2 +- src/api-schema.js | 11 +++++++---- tests/api-schema.js | 24 ++++++++++++++++++++---- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5c9c3fc..b440fd2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@janiscommerce/api-schema", - "version": "0.1.0", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 973c6f3..47325fc 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/api-schema.js b/src/api-schema.js index 81b7b35..99c1d66 100644 --- a/src/api-schema.js +++ b/src/api-schema.js @@ -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() { diff --git a/tests/api-schema.js b/tests/api-schema.js index dda1f2f..3370268 100644 --- a/tests/api-schema.js +++ b/tests/api-schema.js @@ -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()); @@ -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(); @@ -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, { @@ -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 });