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';
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,
});

View file

@ -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();