diff --git a/.travis.yml b/.travis.yml index 6c667d5..33fee8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,9 +8,11 @@ node_js: - '9' - '8' script: - - yarn test + - yarn cover + - yarn check-coverage - yarn build after_success: + - yarn report-coverage - yarn travis-deploy-once "yarn semantic-release" branches: only: diff --git a/README.md b/README.md index 7d21efe..8d95814 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,9 @@ Definir a função callback para os pacotes de mensagens recebidos. ## Estrutura da Mensagem -As mensagens publicadas e recebidas são no formato seguem o modelo/esquema abaixo, onde essas chaves são as requeridas, podendo haver outras. +As mensagens publicadas e recebidas seguem o modelo/esquema abaixo, onde essas chaves são as requeridas, podendo haver outras. + +> Outras chaves e seus respectivos valores não são verificados para biblioteca. ```javascript { diff --git a/package.json b/package.json index 614ac49..ed0ab34 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,10 @@ "scripts": { "add-contributor": "kcd-scripts contributors add", "commit": "git-cz", - "test": "jest src/index.test.js", + "test": "jest --coverage src/__tests__/**.test.js", + "check-coverage": "nyc check-coverage --statements 75 --branches 75 --functions 75 --lines 75", + "report-coverage": "cat ./coverage/lcov.info | codecov", + "cover": "nyc yarn test", "prebuild": "rimraf dist", "build": "yarn build:main", "build:main": "babel --copy-files --out-dir dist --ignore *.test.js src", @@ -36,6 +39,7 @@ "babel-plugin-transform-class-properties": "6.24.1", "babel-plugin-transform-object-rest-spread": "6.26.0", "babel-preset-env": "1.7.0", + "codecov": "3.0.2", "commitizen": "2.10.1", "cz-conventional-changelog": "2.1.0", "eslint": "5.0.1", @@ -44,6 +48,7 @@ "eslint-plugin-import": "2.13.0", "jest": "23.2.0", "kcd-scripts": "0.39.2", + "nyc": "12.0.2", "rimraf": "2.6.2", "semantic-release": "^15.6.0", "travis-deploy-once": "5.0.0" diff --git a/src/index.test.js b/src/__tests__/utils.test.js similarity index 82% rename from src/index.test.js rename to src/__tests__/utils.test.js index 1e7f677..2891560 100644 --- a/src/index.test.js +++ b/src/__tests__/utils.test.js @@ -1,13 +1,15 @@ -import { checkMessage } from './index'; +import { hasClient, checkMessage } from 'utils'; describe('sigaa-egressos', () => { describe('checkMessage', () => { test('should be a invalid message: must be an object', () => { expect(() => checkMessage(5)).toThrowError('The message must be an object'); }); + test('should be a invalid message: does not have all the expected keys', () => { expect(() => checkMessage({ id: 2 })).toThrowError('The message does not have all the expected keys'); }); + test('should be a invalid message: contains invalid keys', () => { expect(() => checkMessage({ id: 1, @@ -16,6 +18,7 @@ describe('sigaa-egressos', () => { testo: 'Olá', })).toThrowError('The message contains invalid keys'); }); + test('should be a invalid message: wrong value for keys', () => { expect(() => checkMessage({ id: 1, @@ -25,4 +28,10 @@ describe('sigaa-egressos', () => { })).toThrowError('The value for the title must be a string'); }); }); + + describe('hasClient', () => { + test('client is undefined', () => { + expect(() => hasClient()).toThrowError('Connection undefined'); + }); + }); }); diff --git a/src/index.js b/src/index.js index f8bf212..1eb5694 100644 --- a/src/index.js +++ b/src/index.js @@ -1,15 +1,10 @@ import * as emitter from 'emitter-io'; +import { hasClient, checkMessage } from './utils'; // Env for tests const EMITTER_CHANNEL = 'news'; const EMITTER_CHANNEL_KEY = 'Yr2-O0G1QT3TQ70aPIwFeQk264wLw02n'; -/** - * The allowed keys on the message object. - * @constant {array} - */ -const MESSAGE_KEYS = ['id', 'title', 'type', 'description']; - /** * An Emitter instance. * @global @@ -17,58 +12,6 @@ const MESSAGE_KEYS = ['id', 'title', 'type', 'description']; */ let client; -/** - * Checks for an established connection. - * @function - * @throws {Error} Connection does not exists. - */ -const hasClient = () => { - if (client === undefined || null) { - throw new Error('Connection undefined'); - } -}; - -/** - * Checks if the message is valid. - * @function - * @param {!object} message A javascript object - * @throws {Error} The message must be an object. - * @throws {Error} The message does not have all the expected keys. - * @throws {Error} The message contains invalid keys. - * @throws {Error} The value for the ${key} must be a number|string. - */ -export const checkMessage = (message) => { - if (typeof message !== 'object') { - throw new Error('The message must be an object'); - } - - const messageKeys = Object.keys(message); - - if (messageKeys.length < 4) { - throw new Error('The message does not have all the expected keys'); - } - - const unMatchedKeys = messageKeys.filter(key => !MESSAGE_KEYS.includes(key)); - - if (unMatchedKeys.length > 0) { - throw new Error(`The message contains invalid keys:\n ${unMatchedKeys}`); - } - - let value; - - MESSAGE_KEYS.forEach((key) => { - value = message[key]; - - if (key !== 'id') { - if (typeof value !== 'string') { - throw new Error(`The value for the ${key} must be a string`); - } - } else if (typeof value !== 'number') { - throw new Error('The value for the id must be a number'); - } - }); -}; - /** * Connects to the emitter api broker * and returns an Emitter instance. @@ -98,7 +41,7 @@ const subscribe = (last) => { throw new Error('The parameter last must be a number'); } - hasClient(); + hasClient(client); client.subscribe({ key: EMITTER_CHANNEL_KEY, @@ -112,7 +55,7 @@ const subscribe = (last) => { * @function */ const unsubscribe = () => { - hasClient(); + hasClient(client); client.unsubscribe({ key: EMITTER_CHANNEL_KEY, @@ -134,7 +77,7 @@ const publish = (message, ttl) => { throw new Error('The ttl parameter must be a number'); } - hasClient(); + hasClient(client); client.publish({ key: EMITTER_CHANNEL_KEY, @@ -160,7 +103,7 @@ const onMessage = (callback) => { throw new Error('The parameter is not a function'); } - hasClient(); + hasClient(client); client.on('message', (message) => { callback(message.asObject()); diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..9326e6a --- /dev/null +++ b/src/utils.js @@ -0,0 +1,57 @@ +/** + * Checks for an established connection. + * @function + * @throws {Error} Connection does not exists. + */ +export const hasClient = (client) => { + if (client === undefined || null) { + throw new Error('Connection undefined'); + } +}; + +/** + * The allowed keys on the message object. + * @constant {array} + */ +const MESSAGE_KEYS = ['id', 'title', 'type', 'description']; + +/** + * Checks if the message is valid. + * @function + * @param {!object} message A javascript object + * @throws {Error} The message must be an object. + * @throws {Error} The message does not have all the expected keys. + * @throws {Error} The message contains invalid keys. + * @throws {Error} The value for the ${key} must be a number|string. + */ +export const checkMessage = (message) => { + if (typeof message !== 'object') { + throw new Error('The message must be an object'); + } + + const messageKeys = Object.keys(message); + + if (messageKeys.length < 4) { + throw new Error('The message does not have all the expected keys'); + } + + const unMatchedKeys = messageKeys.filter(key => !MESSAGE_KEYS.includes(key)); + + if (unMatchedKeys.length > 0) { + throw new Error(`The message contains invalid keys:\n ${unMatchedKeys}`); + } + + let value; + + MESSAGE_KEYS.forEach((key) => { + value = message[key]; + + if (key !== 'id') { + if (typeof value !== 'string') { + throw new Error(`The value for the ${key} must be a string`); + } + } else if (typeof value !== 'number') { + throw new Error('The value for the id must be a number'); + } + }); +}; diff --git a/yarn.lock b/yarn.lock index d35769e..4076941 100644 --- a/yarn.lock +++ b/yarn.lock @@ -497,6 +497,10 @@ argv-formatter@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/argv-formatter/-/argv-formatter-1.0.0.tgz#a0ca0cbc29a5b73e836eebe1cbf6c5e0e4eb82f9" +argv@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/argv/-/argv-0.0.2.tgz#ecbd16f8949b157183711b1bda334f37840185ab" + aria-query@^0.7.0: version "0.7.1" resolved "https://registry.yarnpkg.com/aria-query/-/aria-query-0.7.1.tgz#26cbb5aff64144b0a825be1846e0b16cfa00b11e" @@ -1708,12 +1712,6 @@ chai@^4.1.2: pathval "^1.0.0" type-detect "^4.0.0" -"chainsaw@>=0.0.7 <0.1": - version "0.0.9" - resolved "https://registry.yarnpkg.com/chainsaw/-/chainsaw-0.0.9.tgz#11a05102d1c4c785b6d0415d336d5a3a1612913e" - dependencies: - traverse ">=0.3.0 <0.4" - chalk@1.1.3, chalk@^1.0.0, chalk@^1.1.1, chalk@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" @@ -1905,6 +1903,14 @@ code-point-at@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" +codecov@3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/codecov/-/codecov-3.0.2.tgz#aea43843a5cd2fb6b7e488b2eff25d367ab70b12" + dependencies: + argv "0.0.2" + request "^2.81.0" + urlgrey "0.4.4" + collapse-white-space@^1.0.0: version "1.0.4" resolved "https://registry.yarnpkg.com/collapse-white-space/-/collapse-white-space-1.0.4.tgz#ce05cf49e54c3277ae573036a26851ba430a0091" @@ -3822,12 +3828,6 @@ hash.js@^1.0.0, hash.js@^1.0.3: inherits "^2.0.3" minimalistic-assert "^1.0.0" -"hashish@>=0.0.2 <0.1": - version "0.0.4" - resolved "https://registry.yarnpkg.com/hashish/-/hashish-0.0.4.tgz#6d60bc6ffaf711b6afd60e426d077988014e6554" - dependencies: - traverse ">=0.2.4" - he@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" @@ -7229,12 +7229,6 @@ remove-trailing-separator@^1.0.1: version "1.1.0" resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef" -remove@0.1.5: - version "0.1.5" - resolved "https://registry.yarnpkg.com/remove/-/remove-0.1.5.tgz#095ffd827d65c9f41ad97d33e416a75811079955" - dependencies: - seq ">= 0.3.5" - repeat-element@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/repeat-element/-/repeat-element-1.1.2.tgz#ef089a178d1483baae4d93eb98b4f9e4e11d990a" @@ -7263,7 +7257,7 @@ request-promise-native@^1.0.5: stealthy-require "^1.1.0" tough-cookie ">=2.3.3" -request@^2.72.0, request@^2.83.0: +request@^2.72.0, request@^2.81.0, request@^2.83.0: version "2.87.0" resolved "https://registry.yarnpkg.com/request/-/request-2.87.0.tgz#32f00235cd08d482b4d0d68db93a829c0ed5756e" dependencies: @@ -7628,13 +7622,6 @@ semver@~2.3.1: version "2.3.2" resolved "https://registry.yarnpkg.com/semver/-/semver-2.3.2.tgz#b9848f25d6cf36333073ec9ef8856d42f1233e52" -"seq@>= 0.3.5": - version "0.3.5" - resolved "https://registry.yarnpkg.com/seq/-/seq-0.3.5.tgz#ae02af3a424793d8ccbf212d69174e0c54dffe38" - dependencies: - chainsaw ">=0.0.7 <0.1" - hashish ">=0.0.2 <0.1" - set-blocking@^2.0.0, set-blocking@~2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" @@ -8262,14 +8249,10 @@ tr46@^1.0.1: dependencies: punycode "^2.1.0" -traverse@>=0.2.4, traverse@^0.6.6, traverse@~0.6.6: +traverse@^0.6.6, traverse@~0.6.6: version "0.6.6" resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.6.6.tgz#cbdf560fd7b9af632502fed40f918c157ea97137" -"traverse@>=0.3.0 <0.4": - version "0.3.9" - resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.3.9.tgz#717b8f220cc0bb7b44e40514c22b2e8bbc70d8b9" - travis-deploy-once@5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/travis-deploy-once/-/travis-deploy-once-5.0.0.tgz#5fd23709db3104d18f48778df969409340b583d1" @@ -8518,6 +8501,10 @@ url@^0.11.0: punycode "1.3.2" querystring "0.2.0" +urlgrey@0.4.4: + version "0.4.4" + resolved "https://registry.yarnpkg.com/urlgrey/-/urlgrey-0.4.4.tgz#892fe95960805e85519f1cd4389f2cb4cbb7652f" + use@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/use/-/use-3.1.0.tgz#14716bf03fdfefd03040aef58d8b4b85f3a7c544"