forked from legastero/stanza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespacedAttribute.ts
92 lines (82 loc) · 2.51 KB
/
namespacedAttribute.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import expect from 'expect';
import { namespacedAttribute, parse, Registry } from '../../../src/jxt';
interface Example {
foo?: string;
baz?: string;
child?: {
foo?: string;
};
}
const registry = new Registry();
registry.define([
{
element: 'x',
fields: {
foo: namespacedAttribute('p', 'ns', 'foo')
},
namespace: '',
path: 'example'
},
{
element: 'x2',
fields: {
foo: namespacedAttribute('p', 'ns', 'foo', 'bar')
},
namespace: '',
path: 'example2'
},
{
element: 'x3',
fields: {
baz: namespacedAttribute('p', 'ns', 'baz')
},
namespace: '',
optionalNamespaces: {
other: 'ns'
},
path: 'example3'
},
{
element: 'c',
fields: {
foo: namespacedAttribute('p', 'ns', 'foo')
},
namespace: '',
path: 'example3.child'
}
]);
test('[Type: namespacedAttribute] Basic import', () => {
const ex = registry.import(parse('<x xmlns:p="ns" p:foo="bar" />')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({ foo: 'bar' });
});
test('[Type: namespacedAttribute] Basic export', () => {
const ex = registry.export<Example>('example', { foo: 'bar' });
expect(ex).toBeTruthy();
expect(ex!.toString()).toBe('<x xmlns:p="ns" p:foo="bar"/>');
});
test('[Type: namespacedAttribute] Parent defined namespace import', () => {
const ex = registry.import(parse('<x3 xmlns:p="ns"><c p:foo="bar" /></x3>')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({ child: { foo: 'bar' } });
});
test('[Type: namespacedAttribute] Parent defined namespace export', () => {
const ex = registry.export<Example>('example3', { baz: 'qux', child: { foo: 'bar' } });
expect(ex).toBeTruthy();
expect(ex!.toString()).toBe('<x3 xmlns:other="ns" other:baz="qux"><c other:foo="bar"/></x3>');
});
test('[Type: namespacedAttribute] Empty import', () => {
const ex = registry.import(parse('<x />')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({});
});
test('[Type: namespacedAttribute] Empty export', () => {
const ex = registry.export<Example>('example', {});
expect(ex).toBeTruthy();
expect(ex!.toString()).toBe('<x/>');
});
test('[Type: namespacedAttribute] Default value import', () => {
const ex = registry.import(parse('<x2 />')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({ foo: 'bar' });
});