-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
module-analyzer.spec.ts
202 lines (175 loc) · 5.24 KB
/
module-analyzer.spec.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { Container } from 'aurelia-dependency-injection';
import { bindable, customElement, processContent } from '../src/decorators';
import { ModuleAnalyzer } from '../src/module-analyzer';
import { ViewResources } from '../src/view-resources';
describe('ModuleAnalyzer', () => {
let container: Container;
let resources: ViewResources;
let moduleAnalyzer: ModuleAnalyzer;
beforeEach(() => {
container = new Container();
resources = new ViewResources();
moduleAnalyzer = container.get(ModuleAnalyzer);
});
it('analyzes', () => {
let m = {
E: class E {
},
DCustomAttribute: class {
}
};
let result = moduleAnalyzer.analyze('a.js', m);
expect(result.mainResource.metadata.elementName).toBe('e');
expect(result.resources[0].metadata.attributeName).toBe('d');
});
it(`uses static resource convention,
which ignores export name and use class name / explicit config`, () => {
let m = {
E: class E {
static $resource() {
return 'e1';
}
},
DCustomAttribute: class {
static $resource() {
return {
type: 'attribute'
};
}
}
};
let analysis = moduleAnalyzer.analyze('a.js', m);
expect(analysis.mainResource.metadata.elementName).toBe('e1');
expect(analysis.resources[0].metadata.attributeName).toBe('d-custom-attribute');
});
it('combines metadata for HtmlBehaviorResource', () => {
@customElement('e1')
class E {
static $resource() {
return 'e1';
}
}
class D {
static $resource() {
return {
type: 'element'
};
}
@bindable() date = new Date()
}
class F {
static $resource() {
return { type: 'attribute' }
}
@bindable() f
}
let m = {
E: E,
DCustomAttribute: D,
FCustom: F
};
let analysis = moduleAnalyzer.analyze('a.js', m);
expect(analysis.mainResource.metadata.elementName).toBe('e1');
expect(analysis.resources[0].metadata.elementName).toBe('d');
expect(analysis.resources[1].metadata.attributeName).toBe('f');
});
describe('inheritance', () => {
it('analyzes', () => {
class BaseField {
static $resource() {
return {
bindables: [
'name',
{
name: 'value',
defaultBindingMode: 'twoWay'
}
]
};
}
}
class TextField extends BaseField {
@bindable()
label
}
class DateField extends TextField {
@bindable()
format;
}
let m1 = {
a: TextField,
};
let m2 = {
b: DateField
}
let analysis1 = moduleAnalyzer.analyze('a.js', m1);
let analysis2 = moduleAnalyzer.analyze('b.js', m2);
expect(analysis1.mainResource.metadata.elementName).toBe('text-field');
expect(analysis2.mainResource.metadata.elementName).toBe('date-field');
});
// aurelia modules that go through module analyzer will be searched for metadata
// through the whole class hierarchy. So it's always advised to defined own metadata
// for inheritance scenario
it('analyses with base using static config and derived using metadata', () => {
class BaseField {
@bindable() name
@bindable({
primaryProperty: true,
defaultBindingMode: 'twoWay'
})
value;
}
@customElement('text-field')
class TextField extends BaseField {
static $resource = {
bindables: ['label']
};
}
class DateField extends TextField {
@bindable()
format;
}
@processContent(false)
class Field extends BaseField {
static $resource = {
bindables: ['fieldName']
}
}
class Readonly {
static $resource = {
type: 'attribute'
}
}
let m1 = {
a: TextField,
};
let m2 = {
b: DateField
};
let m3 = {
c: Field,
r: Readonly
};
let analysis1 = moduleAnalyzer.analyze('a.js', m1);
let analysis2 = moduleAnalyzer.analyze('b.js', m2);
let analysis3 = moduleAnalyzer.analyze('c.js', m3);
expect(analysis1.mainResource.metadata.elementName).toBe('text-field');
expect(analysis2.mainResource.metadata.elementName).toBe('date-field');
expect(analysis3.mainResource.metadata.elementName).toBe('field');
expect(analysis3.resources[0].metadata.attributeName).toBe('readonly');
// intergration with ViewResources test
// ensure that initialization / registration work with view resources
// -------------
analysis1.initialize(container);
analysis1.register(resources);
expect(analysis1.mainResource.metadata.properties.length).toBe(3);
analysis2.initialize(container);
analysis2.register(resources);
expect(analysis2.mainResource.metadata.properties.length).toBe(4);
analysis3.initialize(container);
analysis3.register(resources);
expect(analysis3.mainResource.metadata.properties.length).toBe(3);
expect(analysis3.resources[0].metadata.properties.length).toBe(1);
});
});
});