diff --git a/public/funktionen.js b/public/funktionen.js index 2d7a9c7..a792659 100644 --- a/public/funktionen.js +++ b/public/funktionen.js @@ -86,7 +86,6 @@ function newsTile(projects) { if (active) { pill = ['Aktiv', 'on']; cta = 'Beiträge schreiben' } else if (states.some(isSetup)) { pill = ['In Einrichtung', 'setup']; cta = 'Status ansehen' } else if (states.length && states.every((s) => s === 'disabled')) { pill = ['Abgeschaltet', 'off']; cta = 'Status ansehen' } const target = pickProject(projects) const count = projects.length > 1 && active ? `

aktiv für ${active} von ${projects.length} Websites

` : '' - const devHint = state === 'active' && a.devMode ? `

${DEV_HINT}

` : '' return `
${svg('news')}${pill[0]}
@@ -114,6 +113,7 @@ function analyseTile(projects) { const a = target && analyseOf(target) if (!target || a.comingSoon) return soonTile('chart', 'Analyse', ANALYSE_TEXT) const state = analyseState(a) + const devHint = state === 'active' && a.devMode ? `

${DEV_HINT}

` : '' const active = projects.filter((p) => analyseState(analyseOf(p)) === 'active').length const pill = state === 'active' ? ['Aktiv', 'on'] : state === 'disabled' ? ['Gesperrt', 'off'] : ['Aus', 'off'] let action diff --git a/test/funktionenTiles.test.js b/test/funktionenTiles.test.js new file mode 100644 index 0000000..edbe3a2 --- /dev/null +++ b/test/funktionenTiles.test.js @@ -0,0 +1,48 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { readFileSync, writeFileSync, mkdtempSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { pathToFileURL } from 'node:url' + +// public/funktionen.js wirklich laden (der absolute Import /analyse-view.js wird auf die Datei umgeleitet) +const dir = mkdtempSync(join(tmpdir(), 'fn-tiles-')) +const viewUrl = pathToFileURL(new URL('../public/analyse-view.js', import.meta.url).pathname).href +const src = readFileSync(new URL('../public/funktionen.js', import.meta.url), 'utf8').replace("'/analyse-view.js'", `'${viewUrl}'`) +const file = join(dir, 'funktionen.mjs') +writeFileSync(file, src) +const { initFunktionen } = await import(pathToFileURL(file).href) + +const news = { state: 'off' } +const project = (id, analyse) => ({ projectId: id, projectName: `P ${id}`, liveUrl: '', features: { news }, analyse }) +async function render(projects) { + globalThis.fetch = async () => ({ ok: true, status: 200, json: async () => ({ projects }) }) + const root = { innerHTML: '', querySelector: () => null } + await initFunktionen(root) + return root.innerHTML +} +const tile = (html, key) => html.slice(html.indexOf(`data-fn-tile="${key}"`), html.indexOf('
', html.indexOf(`data-fn-tile="${key}"`))) + +test('Kacheln rendern ohne Fehler: News, Analyse comingSoon/aus/aktiv/gesperrt', async () => { + const soon = await render([project('a', { comingSoon: true })]) + assert.match(tile(soon, 'news'), /News-Pinnwand/) + assert.match(soon, /Kommt bald/) + assert.doesNotMatch(soon, /data-fn-tile="analyse"/) + + const off = await render([project('a', { state: 'off', eligible: true, devMode: false })]) + assert.match(tile(off, 'analyse'), /Einrichten/) + assert.doesNotMatch(off, /Dev-Modus/) + + const dev = await render([project('a', { state: 'active', eligible: true, devMode: true })]) + assert.match(tile(dev, 'analyse'), /Auswertung ansehen/) + assert.match(tile(dev, 'analyse'), /Im Dev-Modus wird nicht gemessen/) + assert.doesNotMatch(tile(dev, 'news'), /Dev-Modus/) + + const live = await render([project('a', { state: 'active', eligible: true, devMode: false })]) + assert.match(tile(live, 'analyse'), /Auswertung ansehen/) + assert.doesNotMatch(live, /Dev-Modus/) + + const disabled = await render([project('a', { state: 'disabled' })]) + assert.match(tile(disabled, 'analyse'), /Bitte melde dich bei uns/) + assert.doesNotMatch(disabled, /Dev-Modus/) +})