From 153e2a63e0faa1023451db173052b740ec34119a Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 15 Jun 2018 11:26:00 +0300 Subject: [PATCH] test(rest) coverage --- server/rest/index.js | 9 +++-- server/rest/index.spec.js | 83 +++++++++++++++++++++++++++++++++++++++ test/server/rest.js | 48 ---------------------- 3 files changed, 88 insertions(+), 52 deletions(-) create mode 100644 server/rest/index.spec.js delete mode 100644 test/server/rest.js diff --git a/server/rest/index.js b/server/rest/index.js index a954bbe3..56404b4b 100644 --- a/server/rest/index.js +++ b/server/rest/index.js @@ -162,6 +162,7 @@ const getMoveMsg = (files) => { return msg; }; +module.exports._onPUT = onPUT; function onPUT(name, body, callback) { checkPut(name, body, callback); @@ -343,13 +344,13 @@ function formatMsg(msg, data, status) { function check(request, response, next) { if (typeof request !== 'object') - throw Error('request should be an object'); + throw Error('request should be an object!'); if (typeof response !== 'object') - throw Error('response should be an object'); + throw Error('response should be an object!'); if (typeof next !== 'function') - throw Error('next should be a function'); + throw Error('next should be a function!'); } function checkPut(name, body, callback) { @@ -360,6 +361,6 @@ function checkPut(name, body, callback) { throw Error('body should be a string!'); if (typeof callback !== 'function') - throw Error('callback should be a function'); + throw Error('callback should be a function!'); } diff --git a/server/rest/index.spec.js b/server/rest/index.spec.js new file mode 100644 index 00000000..b47344aa --- /dev/null +++ b/server/rest/index.spec.js @@ -0,0 +1,83 @@ +'use strict'; + +const test = require('tape'); +const rest = require('.'); +const { + _formatMsg, + _getWin32RootMsg, + _isRootWin32, + _isRootAll, + _onPUT, +} = rest; + +test('rest: formatMsg', (t) => { + const result = _formatMsg('hello', 'world'); + + t.equal(result, 'hello: ok("world")', 'should be equal'); + t.end(); +}); + +test('rest: formatMsg: json', (t) => { + const result = _formatMsg('hello', { + name: 'world', + }); + + t.equal(result, 'hello: ok("{"name":"world"}")', 'should parse json'); + t.end(); +}); + +test('rest: getWin32RootMsg', (t) => { + const {message} = _getWin32RootMsg(); + + t.equal(message,'Could not copy from/to root on windows!', 'should return error'); + t.end(); +}); + +test('rest: isRootWin32', (t) => { + const result = _isRootWin32('/'); + + t.notOk(result, 'should equal'); + t.end(); +}); + +test('rest: isRootAll', (t) => { + const result = _isRootAll(['/', '/h']); + + t.notOk(result, 'should equal'); + t.end(); +}); + +test('rest: onPUT: no args', (t) => { + t.throws(_onPUT, /name should be a string!/, 'should throw when no args'); + t.end(); +}); + +test('rest: onPUT: no body', (t) => { + const fn = () => _onPUT('hello'); + t.throws(fn, /body should be a string!/, 'should throw when no body'); + t.end(); +}); + +test('rest: onPUT: no callback', (t) => { + const fn = () => _onPUT('hello', 'world'); + t.throws(fn, /callback should be a function!/, 'should throw when no callback'); + t.end(); +}); + +test('rest: no args', (t) => { + t.throws(rest, /request should be an object!/, 'should throw when no args'); + t.end(); +}); + +test('rest: no response', (t) => { + const fn = () => rest({}); + t.throws(fn, /response should be an object!/, 'should throw when no response'); + t.end(); +}); + +test('rest: no next', (t) => { + const fn = () => rest({}, {}); + t.throws(fn, /next should be a function!/, 'should throw when no response'); + t.end(); +}); + diff --git a/test/server/rest.js b/test/server/rest.js deleted file mode 100644 index db29618e..00000000 --- a/test/server/rest.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict'; - -const test = require('tape'); -const rest = require('../../server/rest'); -const { - _formatMsg, - _getWin32RootMsg, - _isRootWin32, - _isRootAll, -} = rest; - -test('rest: formatMsg', (t) => { - const result = _formatMsg('hello', 'world'); - - t.equal(result, 'hello: ok("world")', 'should be equal'); - t.end(); -}); - -test('rest: formatMsg: json', (t) => { - const result = _formatMsg('hello', { - name: 'world', - }); - - t.equal(result, 'hello: ok("{"name":"world"}")', 'should parse json'); - t.end(); -}); - -test('rest: getWin32RootMsg', (t) => { - const {message} = _getWin32RootMsg(); - - t.equal(message,'Could not copy from/to root on windows!', 'should return error'); - t.end(); -}); - -test('rest: isRootWin32', (t) => { - const result = _isRootWin32('/'); - - t.notOk(result, 'should equal'); - t.end(); -}); - -test('rest: isRootAll', (t) => { - const result = _isRootAll(['/', '/h']); - - t.notOk(result, 'should equal'); - t.end(); -}); -