From 4118f90b6b5eb73a3e9756bab70fdb761b77842b Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 22 May 2018 21:06:09 +0300 Subject: [PATCH] fix(current-file) getParrentDirPath: path contains "+++" --- client/dom/current-file.js | 6 ++--- client/dom/current-file.spec.js | 41 +++++++++++++++++++++++++++++---- client/dom/index.js | 3 ++- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/client/dom/current-file.js b/client/dom/current-file.js index 1c865f43..ed2aa790 100644 --- a/client/dom/current-file.js +++ b/client/dom/current-file.js @@ -112,10 +112,10 @@ module.exports.getParentDirPath = (panel) => { const dirName = DOM.getCurrentDirName() + '/'; const index = path.lastIndexOf(dirName); - if (path !== '/') - return path.slice(0, index); + if (path === '/') + return path; - return path; + return path.slice(0, index); }; /** diff --git a/client/dom/current-file.spec.js b/client/dom/current-file.spec.js index c18748c9..0b25c589 100644 --- a/client/dom/current-file.spec.js +++ b/client/dom/current-file.spec.js @@ -3,6 +3,9 @@ const test = require('tape'); const diff = require('sinon-called-with-diff'); const sinon = diff(require('sinon')); +const id = (a) => a; +const wraptile = require('wraptile'); +const returns = wraptile(id); const currentFile = require('./current-file'); @@ -82,7 +85,7 @@ test('current-file: emit', (t) => { t.end(); }); -test('current-file: return', (t) => { +test('current-file: setCurrentName: return', (t) => { const { DOM, CloudCmd, @@ -111,6 +114,28 @@ test('current-file: return', (t) => { t.end(); }); +test('current-file: getParentDirPath: result', (t) => { + const { + DOM, + } = global; + + const getCurrentDirPath = returns('/D/Films/+++favorite films/'); + const getCurrentDirName = returns('+++favorite films'); + + global.DOM = getDOM({ + getCurrentDirPath, + getCurrentDirName, + }); + + const result = currentFile.getParentDirPath(); + const expected = '/D/Films/'; + + global.DOM = DOM; + + t.equal(result, expected, 'should return parent dir path'); + t.end(); +}); + function getCloudCmd({emit} = {}) { return { PREFIX: '', @@ -118,14 +143,20 @@ function getCloudCmd({emit} = {}) { }; } - -function getDOM({link} = {}) { - link = link || {}; - +function getDOM({ + link = {}, + getCurrentDirPath = sinon.stub(), + getCurrentDirName = sinon.stub(), + getByDataName = sinon.stub(), +} = {}) { return { + getCurrentDirPath, + getCurrentDirName, + getByDataName, CurrentInfo: { link, dirPath: '/', } }; } + diff --git a/client/dom/index.js b/client/dom/index.js index ddfcee0c..09a3d8b8 100644 --- a/client/dom/index.js +++ b/client/dom/index.js @@ -152,9 +152,10 @@ function CmdProto() { this.getParentDirPath = (panel) => { const path = DOM.getCurrentDirPath(panel); const dirName = DOM.getCurrentDirName() + '/'; + const index = path.lastIndexOf(dirName); if (path !== '/') - return path.replace(RegExp(dirName + '$'), ''); + return path.slice(0, index); return path; };