From 19239655ed2b6bb7173a80181a23d2cb5781781d Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 22 May 2019 11:32:39 +0300 Subject: [PATCH] test(menu) defaultMenu: add --- client/modules/user-menu/default-menu.js | 5 +- client/modules/user-menu/default-menu.spec.js | 121 ++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 client/modules/user-menu/default-menu.spec.js diff --git a/client/modules/user-menu/default-menu.js b/client/modules/user-menu/default-menu.js index ceaf37d2..8504fded 100644 --- a/client/modules/user-menu/default-menu.js +++ b/client/modules/user-menu/default-menu.js @@ -21,7 +21,7 @@ module.exports = { } = DOM; const {dirPath} = CurrentInfo; - const path = `${dirPath}/.cloudcmd.menu.js`; + const path = `${dirPath}.cloudcmd.menu.js`; const [e] = await tryToCatch(RESTful.write, path, data); @@ -30,8 +30,9 @@ module.exports = { await CloudCmd.refresh(); DOM.setCurrentByName('.cloudcmd.menu.js'); - await CloudCmd.EditFile.show(); }, }; +module.exports._data = data; + diff --git a/client/modules/user-menu/default-menu.spec.js b/client/modules/user-menu/default-menu.spec.js new file mode 100644 index 00000000..731ab5f0 --- /dev/null +++ b/client/modules/user-menu/default-menu.spec.js @@ -0,0 +1,121 @@ +'use strict'; + +const test = require('supertape'); +const stub = require('@cloudcmd/stub'); +const wraptile = require('wraptile'); +const defaultMenu = require('./default-menu'); +const tryToCatch = require('try-to-catch'); +const {_data} = defaultMenu; +const reject = wraptile(async (a) => { + throw Error(a); +}); + +test('cloudcmd: client: user menu: RESTful.write', async (t) => { + const name = 'C - Create User Menu File'; + const DOM = getDOM(); + const CloudCmd = getCloudCmd(); + const {write} = DOM.RESTful; + + await defaultMenu[name]({ + DOM, + CloudCmd, + tryToCatch, + }); + + const path = '/.cloudcmd.menu.js'; + t.ok(write.calledWith(path, _data), 'should call RESTful.write'); + t.end(); +}); + +test('cloudcmd: client: user menu: refresh', async (t) => { + const name = 'C - Create User Menu File'; + const DOM = getDOM(); + const CloudCmd = getCloudCmd(); + const {refresh} = CloudCmd; + + await defaultMenu[name]({ + DOM, + CloudCmd, + tryToCatch, + }); + + t.ok(refresh.calledWith(), 'should call CloudCmd.refresh'); + t.end(); +}); + +test('cloudcmd: client: user menu: setCurrentByName', async (t) => { + const name = 'C - Create User Menu File'; + const DOM = getDOM(); + const CloudCmd = getCloudCmd(); + const {setCurrentByName} = DOM; + + await defaultMenu[name]({ + DOM, + CloudCmd, + tryToCatch, + }); + + const fileName = '.cloudcmd.menu.js'; + t.ok(setCurrentByName.calledWith(fileName), 'should call DOM.setCurrentByName'); + t.end(); +}); + +test('cloudcmd: client: user menu: EditFile.show', async (t) => { + const name = 'C - Create User Menu File'; + const DOM = getDOM(); + const CloudCmd = getCloudCmd(); + const {EditFile} = CloudCmd; + + await defaultMenu[name]({ + DOM, + CloudCmd, + tryToCatch, + }); + + t.ok(EditFile.show.called, 'should call EditFile.show'); + t.end(); +}); + +test('cloudcmd: client: user menu: no EditFile.show', async (t) => { + const name = 'C - Create User Menu File'; + const DOM = getDOM(); + const CloudCmd = getCloudCmd(); + const {RESTful} = DOM; + const {EditFile} = CloudCmd; + + RESTful.write = stub(reject('Error')); + + await defaultMenu[name]({ + DOM, + CloudCmd, + tryToCatch, + }); + + t.notOk(EditFile.show.called, 'should not call EditFile.show'); + t.end(); +}); + +function getDOM() { + const RESTful = { + write: stub(), + }; + + const CurrentInfo = { + dirPath: '/', + }; + + return { + RESTful, + CurrentInfo, + setCurrentByName: stub(), + }; +} + +function getCloudCmd() { + return { + refresh: stub(), + EditFile: { + show: stub(), + }, + }; +}