feature: client: modules: view: get rid of mock-require

This commit is contained in:
coderiaser 2026-01-13 13:34:56 +02:00
parent feb5aad36b
commit 4bb7d704b4
2 changed files with 38 additions and 61 deletions

View file

@ -2,6 +2,9 @@
'use strict'; 'use strict';
const CloudCmd = globalThis.CloudCmd || {};
const DOM = globalThis.DOM || {};
require('../../../css/view.css'); require('../../../css/view.css');
const rendy = require('rendy'); const rendy = require('rendy');
@ -10,8 +13,8 @@ const wraptile = require('wraptile');
const {tryToCatch} = require('try-to-catch'); const {tryToCatch} = require('try-to-catch');
const load = require('load.js'); const load = require('load.js');
const modal = require('@cloudcmd/modal'); const _modal = require('@cloudcmd/modal');
const createElement = require('@cloudcmd/create-element'); const _createElement = require('@cloudcmd/create-element');
const {time} = require('../../../common/util'); const {time} = require('../../../common/util');
const {FS} = require('../../../common/cloudfunc'); const {FS} = require('../../../common/cloudfunc');
@ -113,7 +116,7 @@ async function show(data, options = {}) {
if (!options || options.bindKeys !== false) if (!options || options.bindKeys !== false)
Events.addKey(listener); Events.addKey(listener);
El = createElement('div', { El = _createElement('div', {
className: 'view', className: 'view',
notAppend: true, notAppend: true,
}); });
@ -126,7 +129,7 @@ async function show(data, options = {}) {
else else
El.append(data); El.append(data);
modal.open(El, initConfig(options)); _modal.open(El, initConfig(options));
return; return;
} }
@ -157,7 +160,10 @@ async function show(data, options = {}) {
} }
module.exports._createIframe = createIframe; module.exports._createIframe = createIframe;
function createIframe(src) { function createIframe(src, overrides = {}) {
const {
createElement = _createElement,
} = overrides;
const element = createElement('iframe', { const element = createElement('iframe', {
src, src,
width: '100%', width: '100%',
@ -172,7 +178,8 @@ function createIframe(src) {
} }
module.exports._viewHtml = viewHtml; module.exports._viewHtml = viewHtml;
function viewHtml(src) { function viewHtml(src, overrides = {}) {
const {modal = _modal} = overrides;
modal.open(createIframe(src), Config); modal.open(createIframe(src), Config);
} }
@ -184,7 +191,7 @@ function viewPDF(src) {
if (CloudCmd.config('showFileName')) if (CloudCmd.config('showFileName'))
options.title = Info.name; options.title = Info.name;
modal.open(element, options); _modal.open(element, options);
} }
async function viewMedia(path) { async function viewMedia(path) {
@ -205,7 +212,7 @@ async function viewMedia(path) {
}, },
}; };
modal.open(element, allConfig); _modal.open(element, allConfig);
} }
async function viewFile() { async function viewFile() {
@ -221,7 +228,7 @@ async function viewFile() {
options.title = Info.name; options.title = Info.name;
El.append(element); El.append(element);
modal.open(El, options); _modal.open(El, options);
} }
const copy = (a) => assign({}, a); const copy = (a) => assign({}, a);
@ -253,7 +260,7 @@ function initConfig(options) {
} }
function hide() { function hide() {
modal.close(); _modal.close();
} }
function viewImage(path, prefixURL) { function viewImage(path, prefixURL) {
@ -286,7 +293,7 @@ function viewImage(path, prefixURL) {
...imageConfig, ...imageConfig,
}; };
modal.open(titles, config); _modal.open(titles, config);
} }
async function getMediaElement(src) { async function getMediaElement(src) {
@ -311,7 +318,7 @@ async function getMediaElement(src) {
name, name,
}); });
const element = createElement('div', { const element = _createElement('div', {
innerHTML, innerHTML,
}); });

View file

@ -4,21 +4,19 @@ require('css-modules-require-hook/preset');
const autoGlobals = require('auto-globals'); const autoGlobals = require('auto-globals');
const {stub} = require('@cloudcmd/stub'); const {stub} = require('@cloudcmd/stub');
const mockRequire = require('mock-require');
const test = autoGlobals(require('supertape')); const test = autoGlobals(require('supertape'));
const {reRequire, stopAll} = mockRequire; const {
_initConfig,
_viewHtml,
_Config,
_createIframe,
} = require('.');
test('cloudcmd: client: view: initConfig', (t) => { test('cloudcmd: client: view: initConfig', (t) => {
let config; let config;
let i = 0; let i = 0;
const {CloudCmd, DOM} = global;
global.CloudCmd = {};
global.DOM = {};
const {_initConfig} = reRequire('.');
const afterClose = () => ++i; const afterClose = () => ++i;
const options = { const options = {
afterClose, afterClose,
@ -30,54 +28,32 @@ test('cloudcmd: client: view: initConfig', (t) => {
config = _initConfig(options); config = _initConfig(options);
config.afterClose(); config.afterClose();
global.CloudCmd = CloudCmd;
global.DOM = DOM;
t.equal(i, 2, 'should not change default config'); t.equal(i, 2, 'should not change default config');
t.end(); t.end();
}); });
test('cloudcmd: client: view: initConfig: no options', (t) => { test('cloudcmd: client: view: initConfig: no options', (t) => {
const {CloudCmd, DOM} = global;
global.CloudCmd = {};
global.DOM = {};
const {_initConfig} = reRequire('.');
const config = _initConfig(); const config = _initConfig();
global.CloudCmd = CloudCmd;
global.DOM = DOM;
t.equal(typeof config, 'object'); t.equal(typeof config, 'object');
t.end(); t.end();
}); });
test('cloudcmd: client: view: html', (t) => { test('cloudcmd: client: view: html', (t) => {
const {CloudCmd, DOM} = global;
global.CloudCmd = {};
global.DOM = {};
const open = stub(); const open = stub();
const modal = {
mockRequire('@cloudcmd/modal', {
open, open,
}); };
const {_viewHtml, _Config} = reRequire('.');
const src = '/hello.html'; const src = '/hello.html';
_viewHtml(src); _viewHtml(src, {
modal,
global.CloudCmd = CloudCmd; });
global.DOM = DOM;
const [first] = open.args; const [first] = open.args;
const [arg] = first; const [arg] = first;
stopAll();
t.deepEqual(first, [arg, _Config]); t.deepEqual(first, [arg, _Config]);
t.end(); t.end();
}); });
@ -89,12 +65,11 @@ test('cloudcmd: client: view: createIframe', (t) => {
}; };
const createElement = stub().returns(el); const createElement = stub().returns(el);
mockRequire('@cloudcmd/create-element', createElement);
const {_createIframe} = reRequire('.');
const src = '/hello.html'; const src = '/hello.html';
_createIframe(src);
_createIframe(src, {
createElement,
});
const expected = { const expected = {
src, src,
@ -102,8 +77,6 @@ test('cloudcmd: client: view: createIframe', (t) => {
width: '100%', width: '100%',
}; };
stopAll();
t.calledWith(createElement, ['iframe', expected]); t.calledWith(createElement, ['iframe', expected]);
t.end(); t.end();
}); });
@ -116,13 +89,10 @@ test('cloudcmd: client: view: createIframe: returns', (t) => {
const createElement = stub().returns(el); const createElement = stub().returns(el);
mockRequire('@cloudcmd/create-element', createElement);
const {_createIframe} = reRequire('.');
const src = '/hello.html'; const src = '/hello.html';
const result = _createIframe(src); const result = _createIframe(src, {
createElement,
stopAll(); });
t.equal(result, el); t.equal(result, el);
t.end(); t.end();