From 846f4aa78fd146ccfdf1105d5d9c3458bf0839c9 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 11 Oct 2018 13:53:22 +0300 Subject: [PATCH] feature(cloudcmd) show size as for links --- client/dom/dom-tree.js | 14 ++++++--- client/dom/dom-tree.spec.js | 57 +++++++++++++++++++++++++++++++++ client/dom/index.js | 6 ++-- common/cloudfunc.js | 4 +++ common/cloudfunc.spec.js | 63 +++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 client/dom/dom-tree.spec.js create mode 100644 common/cloudfunc.spec.js diff --git a/client/dom/dom-tree.js b/client/dom/dom-tree.js index 0c468116..c96dc88d 100644 --- a/client/dom/dom-tree.js +++ b/client/dom/dom-tree.js @@ -1,5 +1,7 @@ 'use strict'; +const currify = require('currify/legacy'); + const DOM = module.exports; /** @@ -8,19 +10,23 @@ const DOM = module.exports; * @param element * @param className */ -module.exports.isContainClass = (element, className) => { +const isContainClass = (element, className) => { if (!element) throw Error('element could not be empty!'); if (!className) throw Error('className could not be empty!'); - const classList = element.classList; - const ret = classList.contains(className); + if (Array.isArray(className)) + return className.some(currify(isContainClass, element)); - return ret; + const classList = element.classList; + + return classList.contains(className); }; +module.exports.isContainClass = isContainClass; + /** * Function search element by tag * @param tag - className diff --git a/client/dom/dom-tree.spec.js b/client/dom/dom-tree.spec.js new file mode 100644 index 00000000..b9226166 --- /dev/null +++ b/client/dom/dom-tree.spec.js @@ -0,0 +1,57 @@ +'use strict'; + +const test = require('tape'); +const diff = require('sinon-called-with-diff'); +const sinon = diff(require('sinon')); +const tryCatch = require('try-catch'); + +const { + isContainClass, +} = require('./dom-tree'); + +test('dom: isContainClass: no element', (t) => { + const [e] = tryCatch(isContainClass); + t.equal(e.message, 'element could not be empty!', 'should throw when no element'); + t.end(); +}); + +test('dom: isContainClass: no className', (t) => { + const [e] = tryCatch(isContainClass, {}); + t.equal(e.message, 'className could not be empty!', 'should throw when no element'); + t.end(); +}); + +test('dom: isContainClass: contains', (t) => { + const contains = sinon.stub(); + const el = { + classList: { + contains, + } + }; + + const className = 'hello'; + isContainClass(el, className); + + t.ok(contains.calledWith(className), 'should call contains'); + t.end(); +}); + +test('dom: isContainClass: contains: array', (t) => { + const contains = sinon.stub(); + const el = { + classList: { + contains, + } + }; + + const className = 'hello'; + isContainClass(el, [ + 'world', + className, + 'hello', + ]); + + t.ok(contains.calledWith(className), 'should call contains'); + t.end(); +}); + diff --git a/client/dom/index.js b/client/dom/index.js index af69bee8..255250c5 100644 --- a/client/dom/index.js +++ b/client/dom/index.js @@ -497,9 +497,11 @@ function CmdProto() { this.isCurrentIsDir = (currentFile) => { const current = currentFile || DOM.getCurrentFile(); const fileType = DOM.getByDataName('js-type', current); - const ret = DOM.isContainClass(fileType, 'directory'); - return ret; + return DOM.isContainClass(fileType, [ + 'directory', + 'directory-link', + ]); }; /** diff --git a/common/cloudfunc.js b/common/cloudfunc.js index cfbf42c2..254d46f3 100644 --- a/common/cloudfunc.js +++ b/common/cloudfunc.js @@ -230,6 +230,7 @@ function getAttribute(type) { return 'target="_blank" '; } +module.exports._getSize = getSize; function getSize(file) { const { size, @@ -239,6 +240,9 @@ function getSize(file) { if (type === 'directory') return '<dir>'; + if (/link/.test(type)) + return '<link>'; + return size; } diff --git a/common/cloudfunc.spec.js b/common/cloudfunc.spec.js new file mode 100644 index 00000000..e25d93d5 --- /dev/null +++ b/common/cloudfunc.spec.js @@ -0,0 +1,63 @@ +'use strict'; + +const test = require('tape'); +const cloudfunc = require('./cloudfunc'); +const { + _getSize, +} = cloudfunc; + +test('cloudfunc: getSize: dir', (t) => { + const type = 'directory'; + const size = 0; + const result = _getSize({ + type, + size, + }); + + const expected = '<dir>'; + + t.equal(result, expected, 'should equal'); + t.end(); +}); + +test('cloudfunc: getSize: link: dir', (t) => { + const type = 'directory-link'; + const size = 0; + const result = _getSize({ + type, + size, + }); + + const expected = '<link>'; + + t.equal(result, expected, 'should equal'); + t.end(); +}); + +test('cloudfunc: getSize: link: file', (t) => { + const type = 'file-link'; + const size = 0; + const result = _getSize({ + type, + size, + }); + + const expected = '<link>'; + + t.equal(result, expected, 'should equal'); + t.end(); +}); + +test('cloudfunc: getSize: file', (t) => { + const type = 'file'; + const size = '100.00kb'; + const result = _getSize({ + type, + size, + }); + + const expected = '100.00kb'; + + t.equal(result, expected, 'should equal'); + t.end(); +});