From 4bb7d704b40ed37b4c31f328284e7d0a110d5f7e Mon Sep 17 00:00:00 2001 From: coderiaser Date: Tue, 13 Jan 2026 13:34:56 +0200 Subject: [PATCH] feature: client: modules: view: get rid of mock-require --- client/modules/view/index.js | 31 ++++++++------ client/modules/view/index.spec.js | 68 +++++++++---------------------- 2 files changed, 38 insertions(+), 61 deletions(-) diff --git a/client/modules/view/index.js b/client/modules/view/index.js index 3959e21d..997103d7 100644 --- a/client/modules/view/index.js +++ b/client/modules/view/index.js @@ -2,6 +2,9 @@ 'use strict'; +const CloudCmd = globalThis.CloudCmd || {}; +const DOM = globalThis.DOM || {}; + require('../../../css/view.css'); const rendy = require('rendy'); @@ -10,8 +13,8 @@ const wraptile = require('wraptile'); const {tryToCatch} = require('try-to-catch'); const load = require('load.js'); -const modal = require('@cloudcmd/modal'); -const createElement = require('@cloudcmd/create-element'); +const _modal = require('@cloudcmd/modal'); +const _createElement = require('@cloudcmd/create-element'); const {time} = require('../../../common/util'); const {FS} = require('../../../common/cloudfunc'); @@ -113,7 +116,7 @@ async function show(data, options = {}) { if (!options || options.bindKeys !== false) Events.addKey(listener); - El = createElement('div', { + El = _createElement('div', { className: 'view', notAppend: true, }); @@ -126,7 +129,7 @@ async function show(data, options = {}) { else El.append(data); - modal.open(El, initConfig(options)); + _modal.open(El, initConfig(options)); return; } @@ -157,7 +160,10 @@ async function show(data, options = {}) { } module.exports._createIframe = createIframe; -function createIframe(src) { +function createIframe(src, overrides = {}) { + const { + createElement = _createElement, + } = overrides; const element = createElement('iframe', { src, width: '100%', @@ -172,7 +178,8 @@ function createIframe(src) { } module.exports._viewHtml = viewHtml; -function viewHtml(src) { +function viewHtml(src, overrides = {}) { + const {modal = _modal} = overrides; modal.open(createIframe(src), Config); } @@ -184,7 +191,7 @@ function viewPDF(src) { if (CloudCmd.config('showFileName')) options.title = Info.name; - modal.open(element, options); + _modal.open(element, options); } async function viewMedia(path) { @@ -205,7 +212,7 @@ async function viewMedia(path) { }, }; - modal.open(element, allConfig); + _modal.open(element, allConfig); } async function viewFile() { @@ -221,7 +228,7 @@ async function viewFile() { options.title = Info.name; El.append(element); - modal.open(El, options); + _modal.open(El, options); } const copy = (a) => assign({}, a); @@ -253,7 +260,7 @@ function initConfig(options) { } function hide() { - modal.close(); + _modal.close(); } function viewImage(path, prefixURL) { @@ -286,7 +293,7 @@ function viewImage(path, prefixURL) { ...imageConfig, }; - modal.open(titles, config); + _modal.open(titles, config); } async function getMediaElement(src) { @@ -311,7 +318,7 @@ async function getMediaElement(src) { name, }); - const element = createElement('div', { + const element = _createElement('div', { innerHTML, }); diff --git a/client/modules/view/index.spec.js b/client/modules/view/index.spec.js index ee698e17..cc46d07d 100644 --- a/client/modules/view/index.spec.js +++ b/client/modules/view/index.spec.js @@ -4,21 +4,19 @@ require('css-modules-require-hook/preset'); const autoGlobals = require('auto-globals'); const {stub} = require('@cloudcmd/stub'); -const mockRequire = require('mock-require'); + const test = autoGlobals(require('supertape')); -const {reRequire, stopAll} = mockRequire; +const { + _initConfig, + _viewHtml, + _Config, + _createIframe, +} = require('.'); test('cloudcmd: client: view: initConfig', (t) => { let config; let i = 0; - const {CloudCmd, DOM} = global; - - global.CloudCmd = {}; - global.DOM = {}; - - const {_initConfig} = reRequire('.'); - const afterClose = () => ++i; const options = { afterClose, @@ -30,54 +28,32 @@ test('cloudcmd: client: view: initConfig', (t) => { 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('.'); const config = _initConfig(); - global.CloudCmd = CloudCmd; - global.DOM = DOM; - t.equal(typeof config, 'object'); t.end(); }); test('cloudcmd: client: view: html', (t) => { - const {CloudCmd, DOM} = global; - - global.CloudCmd = {}; - global.DOM = {}; const open = stub(); - - mockRequire('@cloudcmd/modal', { + const modal = { open, - }); - - const {_viewHtml, _Config} = reRequire('.'); + }; const src = '/hello.html'; - _viewHtml(src); - - global.CloudCmd = CloudCmd; - global.DOM = DOM; + _viewHtml(src, { + modal, + }); const [first] = open.args; const [arg] = first; - stopAll(); - t.deepEqual(first, [arg, _Config]); t.end(); }); @@ -89,12 +65,11 @@ test('cloudcmd: client: view: createIframe', (t) => { }; const createElement = stub().returns(el); - - mockRequire('@cloudcmd/create-element', createElement); - const {_createIframe} = reRequire('.'); - const src = '/hello.html'; - _createIframe(src); + + _createIframe(src, { + createElement, + }); const expected = { src, @@ -102,8 +77,6 @@ test('cloudcmd: client: view: createIframe', (t) => { width: '100%', }; - stopAll(); - t.calledWith(createElement, ['iframe', expected]); t.end(); }); @@ -116,13 +89,10 @@ test('cloudcmd: client: view: createIframe: returns', (t) => { const createElement = stub().returns(el); - mockRequire('@cloudcmd/create-element', createElement); - const {_createIframe} = reRequire('.'); - const src = '/hello.html'; - const result = _createIframe(src); - - stopAll(); + const result = _createIframe(src, { + createElement, + }); t.equal(result, el); t.end();