Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
refactor: adds "validateStatusCode" unit
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed May 10, 2019
1 parent 69f9a93 commit f8a47c2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
49 changes: 49 additions & 0 deletions lib/api/test/unit/validateStatusCode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { assert } = require('chai');
const validateStatusCode = require('../../units/validateStatusCode');

describe('validateStatusCode', () => {
describe('given matching status codes', () => {
const res = validateStatusCode(200, 200);

it('has proper validator', () => {
assert.propertyVal(res, 'validator', 'TextDiff');
});

it('has proper expected type', () => {
assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code');
});

it('has proper real type', () => {
assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code');
});

it('has no errors', () => {
assert.deepPropertyVal(res, 'results', []);
});
});

describe('given unmatching status codes', () => {
const res = validateStatusCode(200, 400);

it('has proper validator', () => {
assert.propertyVal(res, 'validator', 'TextDiff');
});

it('has proper expected type', () => {
assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code');
});

it('has proper real type', () => {
assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code');
});

it('has errors', () => {
assert.deepPropertyVal(res, 'results', [
{
message: 'Real and expected data does not match.',
severity: 'error'
}
]);
});
});
});
30 changes: 30 additions & 0 deletions lib/api/units/validateStatusCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { TextDiff } = require('../../validators/text-diff');

const APIARY_STATUS_CODE_TYPE = 'text/vnd.apiary.status-code';

function normalizeStatusCode(statusCode) {
return String(statusCode).trim();
}

/**
* Validates given real and expected status codes.
* @param {number} realStatusCode
* @param {number} expectedStatusCode
*/
function validateStatusCode(realStatusCode, expectedStatusCode) {
const validator = new TextDiff(
normalizeStatusCode(realStatusCode),
normalizeStatusCode(expectedStatusCode)
);
const rawData = validator.validate();

return {
validator: 'TextDiff',
realType: APIARY_STATUS_CODE_TYPE,
expectedType: APIARY_STATUS_CODE_TYPE,
rawData,
results: validator.evaluateOutputToResults()
};
}

module.exports = validateStatusCode;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
"scripts": {
"lint": "eslint lib/**/*.js test/**/*.js",
"test": "npm run test:server && npm run test:browser && npm run test:features",
"test:new": "mocha \"lib/api/test/**/*.test.js\"",
"test:server": "mocha \"test/unit/**/*-test.js\"",
"test:server:coverage": "nyc npm run test:server",
"test:browser": "mochify \"test/unit/**/*.js\"",
"test:features": "node scripts/cucumber.js",
"coveralls": "nyc --reporter=text-lcov npm run test:server | coveralls",
"ci:lint": "npm run lint",
"ci:test": "npm run coveralls && npm run test:browser && npm run test:features",
"ci:test": "npm run coveralls && npm run test:new && npm run test:browser && npm run test:features",
"ci:release": "semantic-release",
"precommit": "lint-staged"
},
Expand Down

0 comments on commit f8a47c2

Please sign in to comment.