-
Notifications
You must be signed in to change notification settings - Fork 173
/
JSX.test.js
273 lines (228 loc) · 8.03 KB
/
JSX.test.js
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import {expect, mockTabris, spy} from '../test';
import ClientStub from './ClientStub';
import {createElement, jsxFactory} from '../../src/tabris/JSX';
import WidgetCollection from '../../src/tabris/WidgetCollection';
import Composite from '../../src/tabris/widgets/Composite';
import Button from '../../src/tabris/widgets/Button';
import CheckBox from '../../src/tabris/widgets/CheckBox';
import Switch from '../../src/tabris/widgets/Switch';
import AlertDialog from '../../src/tabris/AlertDialog';
describe('JSX', function() {
beforeEach(function() {
mockTabris(new ClientStub());
// main.js does this usually, but is not executed in tests:
global.tabris.CheckBox = CheckBox;
global.tabris.WidgetCollection = WidgetCollection;
});
describe('createElement', function() {
it('creates widget by Constructor', function() {
expect(createElement(CheckBox, null)).to.be.an.instanceof(CheckBox);
});
it('sets properties', function() {
expect(createElement(CheckBox, {text: 'foo'}).text).to.equal('foo');
});
it('attaches camelCase listeners', function() {
let selectSpy = spy();
let fooSpyA = spy();
let fooSpyZ = spy();
let widget = createElement(CheckBox, {onSelect: selectSpy, onAFoo: fooSpyA, onZFoo: fooSpyZ});
widget.trigger('select', {data: 1});
widget.trigger('aFoo', {data: 2});
widget.trigger('zFoo', {data: 3});
expect(selectSpy).to.have.been.calledOnce;
expect(selectSpy).to.have.been.calledWithMatch({data: 1});
expect(fooSpyA).to.have.been.calledOnce;
expect(fooSpyA).to.have.been.calledWithMatch({data: 2});
expect(fooSpyZ).to.have.been.calledOnce;
expect(fooSpyZ).to.have.been.calledWithMatch({data: 3});
});
it('does not attach non-camelCase listeners', function() {
let selectSpy = spy();
let fooSpyA = spy();
let fooSpyZ = spy();
let widget = createElement(CheckBox, {onselect: selectSpy, onafoo: fooSpyA, onzfoo: fooSpyZ});
widget.trigger('select', {data: 1});
widget.trigger('foo', {data: 2});
expect(selectSpy).not.to.have.been.called;
expect(fooSpyA).not.to.have.been.called;
expect(fooSpyZ).not.to.have.been.called;
});
it('appends children', function() {
let children = createElement(
Composite,
{},
createElement(Button),
createElement(CheckBox),
createElement(Switch)
).children();
expect(children[0]).to.be.instanceof(Button);
expect(children[1]).to.be.instanceof(CheckBox);
expect(children[2]).to.be.instanceof(Switch);
});
it('appends children', function() {
let children = createElement(
Composite,
null,
createElement(Button),
createElement(CheckBox),
createElement(Switch)
).children();
expect(children[0]).to.be.instanceof(Button);
expect(children[1]).to.be.instanceof(CheckBox);
expect(children[2]).to.be.instanceof(Switch);
});
it('appends children given as array', function() {
let children = createElement(
Composite,
null,
[createElement(Button), createElement(CheckBox)],
createElement(Switch)
).children();
expect(children[0]).to.be.instanceof(Button);
expect(children[1]).to.be.instanceof(CheckBox);
expect(children[2]).to.be.instanceof(Switch);
});
it('appends widgetCollection', function() {
let children = createElement(
Composite,
null,
new WidgetCollection([
createElement(Button),
createElement(CheckBox),
createElement(Switch)
])
).children();
expect(children[0]).to.be.instanceof(Button);
expect(children[1]).to.be.instanceof(CheckBox);
expect(children[2]).to.be.instanceof(Switch);
});
it('appends widgetCollection and children mixed', function() {
let children = createElement(
Composite,
null,
createElement(Button),
new WidgetCollection([
createElement(CheckBox),
new WidgetCollection([
createElement(Switch)
])
])
).children();
expect(children[0]).to.be.instanceof(Button);
expect(children[1]).to.be.instanceof(CheckBox);
expect(children[2]).to.be.instanceof(Switch);
});
it('appends text on widget supporting text', function() {
let button = createElement(
Button,
null,
'Hello',
'World!'
);
expect(button.text).to.equal('Hello World!');
});
it('appends no text on widget not accepting text', function() {
let composite = createElement(
Composite,
null,
'Hello',
'World!'
);
expect(composite.text).to.be.undefined;
});
it('executes given function', function() {
let fn = spy(() => new WidgetCollection([new Button()]));
let collection = createElement(
fn,
{foo: 'bar'},
{child: 1},
{child: 2}
);
expect(collection).to.be.instanceof(WidgetCollection);
expect(collection.length).to.equal(1);
expect(fn).to.have.been.calledWith({foo: 'bar'}, [{child: 1}, {child: 2}]);
});
it('creates widgetCollection with children', function() {
let collection = createElement(
WidgetCollection,
null,
createElement(Button),
createElement(CheckBox),
createElement(Switch)
);
expect(collection).to.be.instanceOf(WidgetCollection);
expect(collection.length).to.equal(3);
expect(collection[0]).to.be.instanceof(Button);
expect(collection[1]).to.be.instanceof(CheckBox);
expect(collection[2]).to.be.instanceof(Switch);
});
it('creates widgetCollection from array', function() {
let collection = createElement(
WidgetCollection,
null,
[createElement(Button), createElement(CheckBox)],
createElement(Switch)
);
expect(collection).to.be.instanceOf(WidgetCollection);
expect(collection.length).to.equal(3);
expect(collection[0]).to.be.instanceof(Button);
expect(collection[1]).to.be.instanceof(CheckBox);
expect(collection[2]).to.be.instanceof(Switch);
});
it('creates empty widgetCollection', function() {
let collection = createElement(
WidgetCollection,
null
);
expect(collection).to.be.instanceOf(WidgetCollection);
expect(collection.length).to.equal(0);
});
it('fails for widgetCollection with attributes', function() {
expect(() => createElement('widgetCollection',{foo: 'bar'}).to.throw());
});
it('fails for non-widget native type', function() {
expect(() => createElement(AlertDialog, null)).to.throw();
});
it('fails for non-widget named custom type', function() {
expect(() => createElement(class Foo {
set() {}
append() {}
}), null).to.throw(Error, 'JSX: Unsupported type Foo');
});
it('fails for non-widget named custom type', function() {
expect(() => createElement(class {
set() {}
append() {}
}, null)).to.throw(Error, 'JSX: Unsupported type');
});
it('fails for string pointing to non-function', function() {
global.tabris.Foo = 'bar';
expect(() => createElement('foo', null)).to.throw(Error, 'JSX: Unsupported type');
});
it('fails for unrecognized string', function() {
expect(() => createElement('unknownWidget', null)).to.throw();
});
it('calls factory of custom type', function() {
class Foo {
constructor() {
this.isFoo = true;
}
[jsxFactory](type, props, children) {
let result = new Foo();
result.jsxType = type;
result.that = this;
result.props = props;
result.children = children;
return result;
}
}
expect(createElement(Foo, {foo: 'bar'}, 'a', 'b')).to.deep.equal({
isFoo: true,
jsxType: Foo,
that: null,
props: {foo: 'bar'},
children: ['a', 'b']
});
});
});
});