diff --git a/client/dom/operations/rename-current.js b/client/dom/operations/rename-current.js index 5b2e0ff7..1293dc33 100644 --- a/client/dom/operations/rename-current.js +++ b/client/dom/operations/rename-current.js @@ -3,21 +3,29 @@ /* global CloudCmd */ const capitalize = require('just-capitalize'); -const Dialog = require('../dialog'); +const _Dialog = require('../dialog'); const Storage = require('../storage'); const RESTful = require('../rest'); -const { - isCurrentFile, - getCurrentName, - getCurrentFile, - getCurrentByName, - getCurrentType, - getCurrentDirPath, - setCurrentName, -} = require('../current-file'); +const _currentFile = require('../current-file'); -module.exports = async (current) => { +module.exports = async (current, overrides = {}) => { + const { + refresh = CloudCmd.refresh, + Dialog = _Dialog, + currentFile = _currentFile, + } = overrides; + + const { + isCurrentFile, + getCurrentName, + getCurrentFile, + getCurrentByName, + getCurrentType, + getCurrentDirPath, + setCurrentName, + } = currentFile; + if (!isCurrentFile(current)) current = getCurrentFile(); @@ -58,5 +66,5 @@ module.exports = async (current) => { setCurrentName(to, current); Storage.remove(dirPath); - CloudCmd.refresh(); + refresh(); }; diff --git a/client/dom/operations/rename-current.spec.js b/client/dom/operations/rename-current.spec.js index f39ff6bf..4a3c7fec 100644 --- a/client/dom/operations/rename-current.spec.js +++ b/client/dom/operations/rename-current.spec.js @@ -2,23 +2,20 @@ const {test, stub} = require('supertape'); -const mockRequire = require('mock-require'); - -const {reRequire, stopAll} = mockRequire; +const renameCurrent = require('./rename-current'); test('cloudcmd: client: dom: renameCurrent: isCurrentFile', async (t) => { const current = {}; const isCurrentFile = stub(); - mockRequire('../dialog', stubDialog()); - mockRequire('../current-file', stubCurrentFile({ + const currentFile = stubCurrentFile({ isCurrentFile, - })); + }); - const renameCurrent = reRequire('./rename-current'); - await renameCurrent(current); - - stopAll(); + await renameCurrent(current, { + Dialog: stubDialog(), + currentFile, + }); t.calledWith(isCurrentFile, [current], 'should call isCurrentFile'); t.end(); @@ -27,11 +24,6 @@ test('cloudcmd: client: dom: renameCurrent: isCurrentFile', async (t) => { test('cloudcmd: client: dom: renameCurrent: file exist', async (t) => { const current = {}; const name = 'hello'; - const {CloudCmd} = global; - - global.CloudCmd = { - refresh: stub(), - }; const prompt = stub().returns([null, name]); const confirm = stub().returns([true]); @@ -39,25 +31,23 @@ test('cloudcmd: client: dom: renameCurrent: file exist', async (t) => { const getCurrentByName = stub().returns(current); const getCurrentType = stub().returns('directory'); - mockRequire('../dialog', stubDialog({ + const Dialog = stubDialog({ confirm, prompt, - })); + }); - mockRequire('../current-file', stubCurrentFile({ + const currentFile = stubCurrentFile({ getCurrentByName, getCurrentType, - })); + }); - const renameCurrent = reRequire('./rename-current'); - await renameCurrent(); + await renameCurrent(null, { + Dialog, + currentFile, + }); const expected = 'Directory "hello" already exists. Proceed?'; - global.CloudCmd = CloudCmd; - - stopAll(); - t.calledWith(confirm, [expected], 'should call confirm'); t.end(); });