Chat: Team-API und Laden des Chat-Bausteins vom Kundenportal
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fce2f41463
commit
717a3323fa
3 files changed
+109
-2
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { loadModule } from './load.mjs'
|
||||
|
||||
// Chat-Baustein (chat-composer.js/.css) wird zur Laufzeit vom Kundenportal geladen: eine Quelle fuer Portal und WOMS
|
||||
const { loadChatComposer } = await loadModule('src/lib/loadChatComposer.js')
|
||||
|
||||
function fakeDoc() {
|
||||
const appended = []
|
||||
return {
|
||||
appended,
|
||||
head: { appendChild: (node) => { appended.push(node); return node } },
|
||||
createElement: (tag) => ({ tag, attrs: {}, setAttribute(k, v) { this.attrs[k] = v } }),
|
||||
}
|
||||
}
|
||||
|
||||
test('haengt CSS und Skript einmal an und liefert window.ChatComposer', async () => {
|
||||
const doc = fakeDoc()
|
||||
const win = {}
|
||||
const first = loadChatComposer({ baseUrl: 'https://project.webklar.com', doc, win })
|
||||
const second = loadChatComposer({ baseUrl: 'https://project.webklar.com', doc, win })
|
||||
assert.equal(doc.appended.length, 2)
|
||||
const link = doc.appended.find((n) => n.tag === 'link')
|
||||
const script = doc.appended.find((n) => n.tag === 'script')
|
||||
assert.equal(link.href || link.attrs.href, 'https://project.webklar.com/chat-composer.css')
|
||||
assert.equal(link.rel || link.attrs.rel, 'stylesheet')
|
||||
assert.equal(script.src || script.attrs.src, 'https://project.webklar.com/chat-composer.js')
|
||||
win.ChatComposer = { create() {} }
|
||||
script.onload()
|
||||
assert.equal(await first, win.ChatComposer)
|
||||
assert.equal(await second, win.ChatComposer)
|
||||
})
|
||||
|
||||
test('Skript laedt nicht -> deutsche Meldung, naechster Aufruf versucht es neu', async () => {
|
||||
const doc = fakeDoc()
|
||||
const win = {}
|
||||
const p = loadChatComposer({ baseUrl: 'https://p', doc, win })
|
||||
doc.appended.find((n) => n.tag === 'script').onerror()
|
||||
await assert.rejects(p, { message: 'Der Chat-Baustein konnte nicht geladen werden.' })
|
||||
loadChatComposer({ baseUrl: 'https://p', doc, win })
|
||||
assert.equal(doc.appended.filter((n) => n.tag === 'script').length, 2)
|
||||
})
|
||||
|
||||
test('schon geladen (win.ChatComposer vorhanden) -> nichts anhaengen', async () => {
|
||||
const doc = fakeDoc()
|
||||
const win = { ChatComposer: { create() {} } }
|
||||
assert.equal(await loadChatComposer({ baseUrl: 'https://p', doc, win }), win.ChatComposer)
|
||||
assert.equal(doc.appended.length, 0)
|
||||
})
|
||||
Reference in new issue
Block a user