From 09de19b46e39d9e147eea7c19f91b1b710411bf9 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 17 Jan 2023 21:53:47 +0200 Subject: [PATCH] test: static: user-menu: coverage --- static/user-menu.js | 15 ++++--- static/user-menu.spec.js | 93 ++++++++++++++++++++++++++++++++++------ 2 files changed, 89 insertions(+), 19 deletions(-) diff --git a/static/user-menu.js b/static/user-menu.js index c9126895..6e4032ca 100644 --- a/static/user-menu.js +++ b/static/user-menu.js @@ -37,22 +37,23 @@ module.exports = { }, 'C - Create User Menu File': async ({DOM, CloudCmd}) => { - const {Dialog} = DOM; - const currentFile = DOM.getCurrentByName('.cloudcmd.menu.js') - + const { + Dialog, + CurrentInfo, + } = DOM; + const currentFile = DOM.getCurrentByName('.cloudcmd.menu.js'); + if (currentFile) { const [cancel] = await Dialog.confirm(`Looks like file '.cloudcmd.menu.js' already exists. Overwrite?`); if (cancel) return; } - - const {CurrentInfo} = DOM; + const {dirPath} = CurrentInfo; const path = `${dirPath}.cloudcmd.menu.js`; const {prefix} = CloudCmd; - - + const data = await readDefaultMenu({prefix}); await createDefaultMenu({ path, diff --git a/static/user-menu.spec.js b/static/user-menu.spec.js index ea5e0a69..782afed1 100644 --- a/static/user-menu.spec.js +++ b/static/user-menu.spec.js @@ -45,39 +45,55 @@ test('cloudcmd: static: user menu: R', (t) => { t.end(); }); -test('cloudcmd: static: user menu: F6', (t) => { +test('cloudcmd: static: user menu: F6', async (t) => { const name = 'F6 - Copy URL to current file'; + const DOM = {}; const fn = defaultMenu[name]; - - t.ok(fn); + const [error] = await tryToCatch(fn, { + DOM, + }); + + t.equal(error.code, 'ERR_UNSUPPORTED_ESM_URL_SCHEME'); t.end(); }); -test('cloudcmd: static: user menu: Y', (t) => { +test('cloudcmd: static: user menu: Y', async (t) => { const name = 'Y - Convert YouTube to MP3'; + const DOM = {}; const fn = defaultMenu[name]; - - t.ok(fn); + const [error] = await tryToCatch(fn, { + DOM, + }); + + t.equal(error.code, 'ERR_UNSUPPORTED_ESM_URL_SCHEME'); t.end(); }); -test('cloudcmd: static: user menu: F', (t) => { +test('cloudcmd: static: user menu: F', async (t) => { const name = 'F - Convert flac to mp3 [ffmpeg]'; + const DOM = {}; const fn = defaultMenu[name]; - - t.ok(fn); + const [error] = await tryToCatch(fn, { + DOM, + }); + + t.equal(error.code, 'ERR_UNSUPPORTED_ESM_URL_SCHEME'); t.end(); }); -test('cloudcmd: static: user menu: M', (t) => { +test('cloudcmd: static: user menu: M', async (t) => { const name = 'M - Convert mpeg to mp3 [ffmpeg]'; + const DOM = {}; const fn = defaultMenu[name]; - - t.ok(fn); + const [error] = await tryToCatch(fn, { + DOM, + }); + + t.equal(error.code, 'ERR_UNSUPPORTED_ESM_URL_SCHEME'); t.end(); }); @@ -98,6 +114,54 @@ test('cloudcmd: static: user menu: IO.write', async (t) => { t.end(); }); +test('cloudcmd: static: user menu: C: exists: ok', async (t) => { + const name = 'C - Create User Menu File'; + const DOM = getDOM(); + const CloudCmd = getCloudCmd(); + const { + Dialog, + getCurrentByName, + } = DOM; + const {confirm} = Dialog; + const {write} = DOM.IO; + + getCurrentByName.returns({}); + confirm.resolves([]); + + await defaultMenu[name]({ + DOM, + CloudCmd, + }); + + const path = '/.cloudcmd.menu.js'; + + t.calledWith(write, [path, _data], 'should call IO.write'); + t.end(); +}); + +test('cloudcmd: static: user menu: C: exists: cancel', async (t) => { + const name = 'C - Create User Menu File'; + const DOM = getDOM(); + const CloudCmd = getCloudCmd(); + const { + Dialog, + getCurrentByName, + } = DOM; + const {confirm} = Dialog; + const {write} = DOM.IO; + + getCurrentByName.returns({}); + confirm.resolves([Error('cancel')]); + + await defaultMenu[name]({ + DOM, + CloudCmd, + }); + + t.notCalled(write); + t.end(); +}); + test('cloudcmd: static: user menu: refresh', async (t) => { const name = 'C - Create User Menu File'; const DOM = getDOM(); @@ -234,6 +298,10 @@ function getDOM() { write: stub(), }; + const Dialog = { + confirm: stub(), + }; + const CurrentInfo = { dirPath: '/', files: [], @@ -244,6 +312,7 @@ function getDOM() { return { IO, + Dialog, CurrentInfo, setCurrentByName: stub(), getFilenames: stub().returns([]),