From b2fa10f2de550418fbb9b781540c48b5f8291f8f Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 11 Aug 2020 12:54:23 +0300 Subject: [PATCH] test(view) initConfig: add --- .putout.json | 3 +++ client/modules/view.js | 45 ++++++++++++++++++-------------- client/modules/view.spec.js | 52 +++++++++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 client/modules/view.spec.js diff --git a/.putout.json b/.putout.json index e8ed3cf0..f078d724 100644 --- a/.putout.json +++ b/.putout.json @@ -20,6 +20,9 @@ ] }] }, + "view.js": { + "apply-shorthand-properties": "off" + }, "test/common/cloudfunc.js": { "remove-console": false } diff --git a/client/modules/view.js b/client/modules/view.js index 1e2f170d..f9124a80 100644 --- a/client/modules/view.js +++ b/client/modules/view.js @@ -4,10 +4,9 @@ require('../../css/view.css'); -const itype = require('itype'); const rendy = require('rendy'); -const exec = require('execon'); const currify = require('currify'); +const wraptile = require('wraptile'); const tryToCatch = require('try-to-catch'); const modal = require('@cloudcmd/modal'); @@ -28,7 +27,14 @@ const {isArray} = Array; const testRegExp = currify((name, reg) => reg.test(name)); const lifo = currify((fn, el, cb, name) => fn(name, el, cb)); +const series = wraptile((...a) => { + for (const f of a) + f(); +}); +const isFn = (a) => typeof a === 'function'; + +const noop = () => {}; const addEvent = lifo(Events.add); const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i'); @@ -51,23 +57,24 @@ let TemplateAudio; let Overlay; const Config = { - beforeShow: (callback) => { + beforeShow: () => { Images.hide(); Key.unsetBind(); - exec(callback); }, - beforeClose: (callback) => { + + beforeClose: () => { Events.rmKey(listener); Key.setBind(); - exec(callback); }, - afterShow: (callback) => { + + afterShow: () => { El.focus(); - exec(callback); }, + onOverlayClick, - afterClose : exec, - autoSize : false, + afterClose: noop, + autoSize: false, + helpers: { title: {}, }, @@ -106,7 +113,7 @@ async function show(data, options) { else El.append(data); - modal.open(El, initConfig(Config, options)); + modal.open(El, initConfig(options)); return; } @@ -141,7 +148,7 @@ function viewPDF(src) { element.contentWindow.addEventListener('keydown', listener); }); - const options = Config; + const options = assign({}, Config); if (CloudCmd.config('showFileName')) options.title = Info.name; @@ -188,8 +195,9 @@ function viewFile() { const copy = (a) => assign({}, a); -function initConfig(config, options) { - config = copy(config); +module.exports._initConfig = initConfig; +function initConfig(options) { + const config = copy(Config); if (!options) return config; @@ -198,17 +206,14 @@ function initConfig(config, options) { for (const name of names) { const isConfig = Boolean(config[name]); const item = options[name]; - const isFunc = itype.function(item); - if (!isFunc || !isConfig) { + if (!isFn(item) || !isConfig) { config[name] = options[name]; continue; } - const func = config[name]; - config[name] = () => { - exec.series([func, item]); - }; + const fn = config[name]; + config[name] = series(fn, item); } return config; diff --git a/client/modules/view.spec.js b/client/modules/view.spec.js new file mode 100644 index 00000000..027b1461 --- /dev/null +++ b/client/modules/view.spec.js @@ -0,0 +1,52 @@ +'use strict'; + +require('css-modules-require-hook/preset'); + +const test = require('supertape'); +const {reRequire} = require('mock-require'); + +test('cloudcmd: client: view: initConfig', (t) => { + let config; + let i = 0; + + const {CloudCmd, DOM} = global; + + global.CloudCmd = {}; + global.DOM = {}; + + const {_initConfig} = reRequire('./view'); + + const afterClose = () => ++i; + const options = { + afterClose, + }; + + config = _initConfig(options); + config.afterClose(); + + config = _initConfig(options); + config.afterClose(); + + global.CloudCmd = CloudCmd; + global.DOM = DOM; + + t.equal(i, 2, 'should not change default config'); + t.end(); +}); + +test('cloudcmd: client: view: initConfig: no options', (t) => { + const {CloudCmd, DOM} = global; + + global.CloudCmd = {}; + global.DOM = {}; + + const {_initConfig} = reRequire('./view'); + const config = _initConfig(); + + global.CloudCmd = CloudCmd; + global.DOM = DOM; + + t.equal(typeof config, 'object', 'should equal'); + t.end(); +}); + diff --git a/package.json b/package.json index 9649e953..15864bcc 100644 --- a/package.json +++ b/package.json @@ -177,6 +177,7 @@ "codegen.macro": "^4.0.0", "coveralls": "^3.0.0", "css-loader": "^3.0.0", + "css-modules-require-hook": "^4.2.3", "domtokenlist-shim": "^1.2.0", "emitify": "^4.0.1", "eslint": "^7.0.0-rc.0",