forked from legastero/stanza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattribute.ts
89 lines (77 loc) · 2.34 KB
/
attribute.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
import expect from 'expect';
import { attribute, parse, Registry } from '../../../src/jxt';
interface Example {
foo?: string;
}
const registry = new Registry();
registry.define([
{
element: 'x',
fields: {
foo: attribute('foo')
},
namespace: '',
path: 'example'
},
{
element: 'x2',
fields: {
foo: attribute('foo', 'bar')
},
namespace: '',
path: 'example2'
},
{
element: 'x3',
fields: {
foo: attribute('foo', undefined, { emitEmpty: true })
},
namespace: '',
path: 'example3'
}
]);
test('[Type: attribute] Basic import', () => {
const ex = registry.import(parse('<x foo="bar" />')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({ foo: 'bar' });
});
test('[Type: attribute] Basic export', () => {
const ex = registry.export<Example>('example', { foo: 'bar' });
expect(ex).toBeTruthy();
expect(ex!.toString()).toBe('<x foo="bar"/>');
});
test('[Type: attribute] Empty import', () => {
const ex = registry.import(parse('<x />')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({});
});
test('[Type: attribute] Empty export', () => {
const ex = registry.export<Example>('example', {});
expect(ex).toBeTruthy();
expect(ex!.toString()).toBe('<x/>');
});
test('[Type: attribute] Default value import', () => {
const ex = registry.import(parse('<x2 />')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({ foo: 'bar' });
});
test('[Type: attribute] Emit when not allowing empty', () => {
const ex = registry.export<Example>('example', { foo: '' });
expect(ex).toBeTruthy();
expect(ex!.toString()).toBe('<x/>');
});
test('[Type: attribute] Emit when allowing empty', () => {
const ex = registry.export<Example>('example3', { foo: '' });
expect(ex).toBeTruthy();
expect(ex!.toString()).toBe('<x3 foo=""/>');
});
test('[Type: attribute] Import when not allowing empty', () => {
const ex = registry.import(parse('<x foo=""/>')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({});
});
test('[Type: attribute] Import when allowing empty', () => {
const ex = registry.import(parse('<x3 foo=""/>')) as Example;
expect(ex).toBeTruthy();
expect(ex).toEqual({ foo: '' });
});