|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +const repoRoot = path.resolve(process.cwd()); |
| 7 | +const dockerJs = fs.readFileSync( |
| 8 | + path.join(repoRoot, 'src/folderview.plus/usr/local/emhttp/plugins/folderview.plus/scripts/docker.js'), |
| 9 | + 'utf8' |
| 10 | +); |
| 11 | + |
| 12 | +const extractArrowFunctionBody = (source, signature) => { |
| 13 | + const startIndex = source.indexOf(signature); |
| 14 | + assert.ok(startIndex >= 0, `Missing function signature: ${signature}`); |
| 15 | + const braceStart = source.indexOf('{', startIndex + signature.length); |
| 16 | + assert.ok(braceStart >= 0, `Missing opening brace for: ${signature}`); |
| 17 | + let depth = 0; |
| 18 | + for (let index = braceStart; index < source.length; index += 1) { |
| 19 | + const char = source[index]; |
| 20 | + if (char === '{') { |
| 21 | + depth += 1; |
| 22 | + } else if (char === '}') { |
| 23 | + depth -= 1; |
| 24 | + if (depth === 0) { |
| 25 | + return source.slice(braceStart + 1, index); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + throw new Error(`Failed to extract function body for: ${signature}`); |
| 30 | +}; |
| 31 | + |
| 32 | +const initializeDockerTooltipOnDemandBody = extractArrowFunctionBody( |
| 33 | + dockerJs, |
| 34 | + 'const initializeDockerTooltipOnDemand = ($target, init, options = {}) => ' |
| 35 | +); |
| 36 | + |
| 37 | +const initializeDockerTooltipOnDemand = new Function( |
| 38 | + '$target', |
| 39 | + 'init', |
| 40 | + 'options', |
| 41 | + 'DOCKER_PREVIEW_POPUP_ENABLED', |
| 42 | + 'setTimeout', |
| 43 | + initializeDockerTooltipOnDemandBody |
| 44 | +); |
| 45 | + |
| 46 | +const createLazyTooltipTarget = () => { |
| 47 | + const dataStore = new Map(); |
| 48 | + const handlers = new Map(); |
| 49 | + const tooltipsterCalls = []; |
| 50 | + const target = { |
| 51 | + length: 1, |
| 52 | + data(key, value) { |
| 53 | + if (arguments.length === 1) { |
| 54 | + return dataStore.get(key); |
| 55 | + } |
| 56 | + dataStore.set(key, value); |
| 57 | + return this; |
| 58 | + }, |
| 59 | + one(events, handler) { |
| 60 | + String(events || '') |
| 61 | + .trim() |
| 62 | + .split(/\s+/) |
| 63 | + .filter(Boolean) |
| 64 | + .forEach((eventName) => { |
| 65 | + handlers.set(eventName.split('.')[0], handler); |
| 66 | + }); |
| 67 | + return this; |
| 68 | + }, |
| 69 | + tooltipster(action) { |
| 70 | + tooltipsterCalls.push(action); |
| 71 | + return this; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + return { |
| 76 | + target, |
| 77 | + emit(type) { |
| 78 | + const handler = handlers.get(type); |
| 79 | + handlers.clear(); |
| 80 | + if (handler) { |
| 81 | + handler({ type }); |
| 82 | + } |
| 83 | + }, |
| 84 | + getTooltipsterCalls: () => [...tooltipsterCalls], |
| 85 | + getData: (key) => dataStore.get(key) |
| 86 | + }; |
| 87 | +}; |
| 88 | + |
| 89 | +const runImmediateTimeout = (callback) => { |
| 90 | + callback(); |
| 91 | + return 0; |
| 92 | +}; |
| 93 | + |
| 94 | +test('docker lazy tooltip init does not eager-open click-trigger tooltips on first hover', () => { |
| 95 | + const lazyTarget = createLazyTooltipTarget(); |
| 96 | + let initCount = 0; |
| 97 | + |
| 98 | + initializeDockerTooltipOnDemand( |
| 99 | + lazyTarget.target, |
| 100 | + () => { |
| 101 | + initCount += 1; |
| 102 | + }, |
| 103 | + { openOnEventTypes: ['click', 'touchstart'] }, |
| 104 | + true, |
| 105 | + runImmediateTimeout |
| 106 | + ); |
| 107 | + |
| 108 | + lazyTarget.emit('mouseenter'); |
| 109 | + |
| 110 | + assert.equal(initCount, 1); |
| 111 | + assert.equal(lazyTarget.getData('fvTooltipsterInitialized'), true); |
| 112 | + assert.deepEqual(lazyTarget.getTooltipsterCalls(), []); |
| 113 | +}); |
| 114 | + |
| 115 | +test('docker lazy tooltip init still eager-opens hover-trigger tooltips on first hover', () => { |
| 116 | + const lazyTarget = createLazyTooltipTarget(); |
| 117 | + let initCount = 0; |
| 118 | + |
| 119 | + initializeDockerTooltipOnDemand( |
| 120 | + lazyTarget.target, |
| 121 | + () => { |
| 122 | + initCount += 1; |
| 123 | + }, |
| 124 | + { openOnEventTypes: ['mouseenter', 'click', 'touchstart'] }, |
| 125 | + true, |
| 126 | + runImmediateTimeout |
| 127 | + ); |
| 128 | + |
| 129 | + lazyTarget.emit('mouseenter'); |
| 130 | + |
| 131 | + assert.equal(initCount, 1); |
| 132 | + assert.deepEqual(lazyTarget.getTooltipsterCalls(), ['open']); |
| 133 | +}); |
| 134 | + |
| 135 | +test('docker lazy tooltip init eager-opens click-trigger tooltips on first click', () => { |
| 136 | + const lazyTarget = createLazyTooltipTarget(); |
| 137 | + let initCount = 0; |
| 138 | + |
| 139 | + initializeDockerTooltipOnDemand( |
| 140 | + lazyTarget.target, |
| 141 | + () => { |
| 142 | + initCount += 1; |
| 143 | + }, |
| 144 | + { openOnEventTypes: ['click', 'touchstart'] }, |
| 145 | + true, |
| 146 | + runImmediateTimeout |
| 147 | + ); |
| 148 | + |
| 149 | + lazyTarget.emit('click'); |
| 150 | + |
| 151 | + assert.equal(initCount, 1); |
| 152 | + assert.deepEqual(lazyTarget.getTooltipsterCalls(), ['open']); |
| 153 | +}); |
0 commit comments