Files
Webklar-Kundenbereich/test/knHuelleDomApi.test.js
T

43 lines
2.7 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { installFakeDom } from './support/fakeDom.js'
import { h, leeren, text } from '../public/neu/dom.js'
import { api, ApiFehler } from '../public/neu/api.js'
test('h: Kundentext bleibt Text; on, boolesche und fehlende Attribute; leeren, text', async () => {
const fx = installFakeDom()
try {
let klicks = 0
const k = h('button', { class: 'kn-knopf', hidden: false, 'data-x': true, on: { click: () => { klicks += 1 } } }, '<b>x</b>', [null, 'y'])
await fx.klick(k)
assert.deepEqual([klicks, k.className, k.hasAttribute('hidden'), k.getAttribute('data-x')], [1, 'kn-knopf', false, ''])
assert.deepEqual([fx.text(k), k.children.length], ['<b>x</b>y', 0])
assert.equal(leeren(k).childNodes.length, 0); assert.equal(text(k, null).textContent, '')
} finally { fx.restore() }
})
test('api: JSON, ApiFehler, Netzfehler und roh (Review Focus 5)', async () => {
const fx = installFakeDom({ antworten: [{ body: { ok: 1 } }, { status: 409, body: { error: 'Schon da', code: 'bereits_eingeschaltet', field: 'x', extra: 1 } }, { status: 502, body: {} }] })
try {
assert.deepEqual(await api('/api/x', { method: 'POST', body: { a: 1 } }), { ok: 1 })
assert.deepEqual(fx.fetchLog[0], { method: 'POST', url: '/api/x', body: { a: 1 } })
const e = await api('/api/x').catch((x) => x)
assert.ok(e instanceof ApiFehler); assert.deepEqual([e.status, e.code, e.message, e.field, e.body.extra], [409, 'bereits_eingeschaltet', 'Schon da', 'x', 1])
assert.equal((await api('/api/x').catch((x) => x)).message, 'Server nicht erreichbar. Bitte kurz warten und noch einmal versuchen.')
globalThis.fetch = async () => { throw new TypeError('Failed to fetch') }
const n = await api('/api/x').catch((x) => x); assert.deepEqual([n.status, n.message], [0, 'Keine Verbindung. Bitte versuch es gleich noch einmal.'])
let init; globalThis.fetch = async (_u, i) => { init = i; return { ok: true, status: 200, json: async () => ({}) } }
const blob = new Blob(['abc']); await api('/api/up', { method: 'POST', roh: blob })
assert.deepEqual([init.headers['Content-Type'], init.body], ['application/octet-stream', blob])
} finally { fx.restore() }
})
test('api: session_revoked leitet genau einmal zur Anmeldung (letzter Test der Datei)', async () => {
const weg = { status: 401, body: { error: 'Sitzung beendet', code: 'session_revoked' } }
const fx = installFakeDom({ antworten: [weg, weg] })
try {
await api('/api/x').catch(() => {}); assert.equal(fx.window.location.href, '/login.html?error=Sitzung%20beendet')
fx.window.location.href = 'anderswo'; await api('/api/x').catch(() => {}); assert.equal(fx.window.location.href, 'anderswo')
} finally { fx.restore() }
})