-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathbuilder.test.ts
More file actions
53 lines (48 loc) · 2.34 KB
/
Copy pathbuilder.test.ts
File metadata and controls
53 lines (48 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Strophe, $build, $pres } from '../dist/strophe.node.esm.js';
import { isEqualNode } from './helpers.js';
import { describe, it, expect, vi } from 'vitest';
describe('Builder', () => {
it('The root() method', () => {
const builder = new Strophe.Builder('root');
const el = builder.c('child').c('grandchild').c('greatgrandchild').root();
expect(el.node.nodeName).toBe('root');
});
it('The h() method parses XHTML-IM markup and drops disallowed tags', () => {
// h() relies on the innerHTML setter to parse markup; in the Node build
// that is backed by the @xmldom/xmldom-based DOM shim. Well-formed XHTML
// is kept and filtered to the XHTML-IM whitelist, so <p>/<strong> survive
// while <script> is dropped.
const el = new Strophe.Builder('body', { xmlns: Strophe.NS.XHTML_IM })
.h('<p>Hello <strong>world</strong></p><script>alert(1)</script>')
.tree();
const p = el.getElementsByTagName('p')[0];
expect(p).toBeTruthy();
expect(p.textContent).toBe('Hello world');
expect(p.getElementsByTagName('strong')[0].textContent).toBe('world');
expect(el.getElementsByTagName('script').length).toBe(0);
});
it('Correct namespace (#32)', () => {
const stanzas = [
new Strophe.Builder('message', { foo: 'asdf' }).tree(),
$build('iq', {}).tree(),
$pres().tree(),
];
stanzas.forEach((s) => expect(s.getAttribute('xmlns')).toBe(Strophe.NS.CLIENT));
});
it('Strophe.Connection.prototype.send() accepts Builders (#27)', () => {
const stanza = $pres();
const conn = new Strophe.Connection('');
const sendStub = vi.spyOn(XMLHttpRequest.prototype, 'send').mockImplementation(() => {});
const timeoutStub = vi.spyOn(globalThis as any, 'setTimeout').mockImplementation((func: () => void) => func());
conn.send(stanza);
expect(sendStub).toHaveBeenCalled();
sendStub.mockRestore();
timeoutStub.mockRestore();
});
it('The fromString static method', () => {
const stanza = Strophe.Builder.fromString(
'<presence from="juliet@example.com/chamber" xmlns="jabber:client"></presence>',
);
expect(isEqualNode(stanza, $pres({ from: 'juliet@example.com/chamber' }))).toBe(true);
});
});