@lightningjs/blits 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/.editorconfig +7 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.cjs +60 -0
  4. package/.vscode/extensions.json +6 -0
  5. package/.vscode/settings.json +9 -0
  6. package/LICENSE +202 -0
  7. package/NOTICE +3 -0
  8. package/README.md +11 -0
  9. package/bin/index.cjs +25 -0
  10. package/boilerplate/.editorconfig +7 -0
  11. package/boilerplate/.eslintignore +3 -0
  12. package/boilerplate/.eslintrc.js +38 -0
  13. package/boilerplate/.vscode/extensions.json +7 -0
  14. package/boilerplate/.vscode/launch.json +12 -0
  15. package/boilerplate/.vscode/settings.json +9 -0
  16. package/boilerplate/index.html +11 -0
  17. package/boilerplate/package.json +36 -0
  18. package/boilerplate/public/assets/logo.png +0 -0
  19. package/boilerplate/src/App.js +12 -0
  20. package/boilerplate/src/components/Loader.js +23 -0
  21. package/boilerplate/src/index.js +11 -0
  22. package/boilerplate/src/pages/Home.js +15 -0
  23. package/boilerplate/src/renderer.js +3 -0
  24. package/boilerplate/vite.config.js +23 -0
  25. package/docs/.nojekyll +0 -0
  26. package/docs/README.md +30 -0
  27. package/docs/_sidebar.md +9 -0
  28. package/docs/component_state.md +21 -0
  29. package/docs/components.md +27 -0
  30. package/docs/computed_properties.md +18 -0
  31. package/docs/getting_started.md +40 -0
  32. package/docs/index.html +47 -0
  33. package/docs/lifecycle_events.md +46 -0
  34. package/docs/props.md +44 -0
  35. package/docs/template_syntax.md +31 -0
  36. package/docs/user_input.md +64 -0
  37. package/docs/watchers.md +38 -0
  38. package/index.js +26 -0
  39. package/package.json +44 -0
  40. package/src/application.js +55 -0
  41. package/src/component.js +192 -0
  42. package/src/component.test.js +51 -0
  43. package/src/components/Circle.js +31 -0
  44. package/src/components/Image.js +33 -0
  45. package/src/components/RouterView.js +38 -0
  46. package/src/components/Sprite.js +56 -0
  47. package/src/components/Text.js +61 -0
  48. package/src/element.js +236 -0
  49. package/src/focus.js +65 -0
  50. package/src/helpers/sequence.js +26 -0
  51. package/src/launch.js +61 -0
  52. package/src/lib/codegenerator/generator.js +276 -0
  53. package/src/lib/codegenerator/generator.test.js +970 -0
  54. package/src/lib/colors/colors.js +70 -0
  55. package/src/lib/colors/colors.test.js +190 -0
  56. package/src/lib/colors/htmlColors.js +168 -0
  57. package/src/lib/componentId.js +27 -0
  58. package/src/lib/componentId.test.js +83 -0
  59. package/src/lib/eventListeners.js +55 -0
  60. package/src/lib/hooks.js +29 -0
  61. package/src/lib/log.js +106 -0
  62. package/src/lib/reactivity/effect.js +54 -0
  63. package/src/lib/reactivity/effect.test.js +35 -0
  64. package/src/lib/reactivity/reactive.js +113 -0
  65. package/src/lib/reactivity/reactive.test.js +27 -0
  66. package/src/lib/setup/base.js +218 -0
  67. package/src/lib/setup/computed.js +43 -0
  68. package/src/lib/setup/input.js +28 -0
  69. package/src/lib/setup/methods.js +35 -0
  70. package/src/lib/setup/props.js +53 -0
  71. package/src/lib/setup/props.test.js +205 -0
  72. package/src/lib/setup/routes.js +24 -0
  73. package/src/lib/setup/state.js +45 -0
  74. package/src/lib/setup/watch.js +33 -0
  75. package/src/lib/templateparser/parser.js +103 -0
  76. package/src/lib/templateparser/parser.test.js +769 -0
  77. package/src/router.js +67 -0
  78. package/src/settings.js +34 -0
  79. package/vite/index.js +1 -0
@@ -0,0 +1,970 @@
1
+ /*
2
+ * Copyright 2023 Comcast Cable Communications Management, LLC
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ *
15
+ * SPDX-License-Identifier: Apache-2.0
16
+ */
17
+
18
+ import test from 'tape'
19
+ import generator from './generator.js'
20
+
21
+ const normalize = (str) => {
22
+ return str.replace(/[\n\s\t]/gi, '')
23
+ }
24
+
25
+ test('Type', (assert) => {
26
+ const expected = 'function'
27
+ const actual = typeof generator
28
+
29
+ assert.equal(actual, expected, 'Generator should be a function')
30
+ assert.end()
31
+ })
32
+
33
+ test('Returns an object with a render function and a context object', (assert) => {
34
+ const result = generator()
35
+ const expected = 'object'
36
+ const actual = typeof result
37
+
38
+ assert.equal(actual, expected, 'Generator should return an object')
39
+ assert.ok('render' in result, 'Generated object should have a render key')
40
+ assert.ok('effects' in result, 'Generated object should have an effects key')
41
+ assert.ok('context' in result, 'Generated object should have a context key')
42
+ assert.end()
43
+ })
44
+
45
+ test('The render key is a function', (assert) => {
46
+ const result = generator()
47
+ const expected = 'function'
48
+ const actual = typeof result.render
49
+
50
+ assert.equal(actual, expected, 'Render key should return a function')
51
+ assert.end()
52
+ })
53
+
54
+ test('The effects key is an array (of functions)', (assert) => {
55
+ const result = generator()
56
+ const actual = Array.isArray(result.effects)
57
+
58
+ assert.ok(actual, 'Update key should return an Array')
59
+ assert.end()
60
+ })
61
+
62
+ test('The contex key is an object', (assert) => {
63
+ const result = generator()
64
+ const expected = 'object'
65
+ const actual = typeof result.context
66
+
67
+ assert.equal(actual, expected, 'Context key should return an object')
68
+ assert.end()
69
+ })
70
+
71
+ test('Generate render and effect code for an empty template', (assert) => {
72
+ const expectedRender = `
73
+ function anonymous(parent,component,context) {
74
+ const elms = []
75
+ return elms
76
+ }
77
+ `
78
+
79
+ const actual = generator()
80
+
81
+ assert.equal(
82
+ normalize(actual.render.toString()),
83
+ normalize(expectedRender),
84
+ 'Generator should return a render function with the correct code'
85
+ )
86
+ assert.ok(
87
+ Array.isArray(actual.effects) && actual.effects.length === 0,
88
+ 'Generator should return an empty effects array'
89
+ )
90
+ assert.end()
91
+ })
92
+
93
+ test('Generate render and effect code for a template with a single simple element', (assert) => {
94
+ const templateObject = {
95
+ children: [
96
+ {
97
+ type: 'Component',
98
+ },
99
+ ],
100
+ }
101
+
102
+ const expectedRender = `
103
+ function anonymous(parent,component,context) {
104
+ const elms = []
105
+
106
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
107
+ const elementConfig1 = {}
108
+
109
+ elementConfig1['type'] = "Component"
110
+
111
+ elms[1].populate(elementConfig1)
112
+ return elms
113
+ }
114
+ `
115
+
116
+ const actual = generator(templateObject)
117
+
118
+ assert.equal(
119
+ normalize(actual.render.toString()),
120
+ normalize(expectedRender),
121
+ 'Generator should return a render function with the correct code'
122
+ )
123
+ assert.ok(
124
+ Array.isArray(actual.effects) && actual.effects.length === 0,
125
+ 'Generator should return an empty effects array'
126
+ )
127
+ assert.end()
128
+ })
129
+
130
+ test('Generate code for a template with a simple element and a simple nested element', (assert) => {
131
+ const templateObject = {
132
+ children: [
133
+ {
134
+ type: 'Component',
135
+ children: [
136
+ {
137
+ type: 'Element',
138
+ },
139
+ ],
140
+ },
141
+ ],
142
+ }
143
+
144
+ const expectedRender = `
145
+ function anonymous(parent,component,context) {
146
+ const elms = []
147
+
148
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
149
+ const elementConfig1 = {}
150
+
151
+ elementConfig1['type'] = "Component"
152
+
153
+ elms[1].populate(elementConfig1)
154
+
155
+ parent = elms[1]
156
+
157
+ elms[2] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
158
+ const elementConfig2 = {}
159
+
160
+ elementConfig2['type'] = "Element"
161
+
162
+ elms[2].populate(elementConfig2)
163
+
164
+ return elms
165
+ }
166
+ `
167
+
168
+ const actual = generator(templateObject)
169
+
170
+ assert.equal(
171
+ normalize(actual.render.toString()),
172
+ normalize(expectedRender),
173
+ 'Generator should return a render function with the correct code'
174
+ )
175
+ assert.ok(
176
+ Array.isArray(actual.effects) && actual.effects.length === 0,
177
+ 'Generator should return an empty effects array'
178
+ )
179
+ assert.end()
180
+ })
181
+
182
+ test('Generate code for a template with a single element with attributes', (assert) => {
183
+ const templateObject = {
184
+ children: [
185
+ {
186
+ type: 'Component',
187
+ x: 10,
188
+ y: 20,
189
+ },
190
+ ],
191
+ }
192
+
193
+ const expectedRender = `
194
+ function anonymous(parent,component,context) {
195
+ const elms = []
196
+
197
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
198
+ const elementConfig1 = {}
199
+
200
+ elementConfig1['type'] = "Component"
201
+ elementConfig1['x'] = 10
202
+ elementConfig1['y'] = 20
203
+
204
+ elms[1].populate(elementConfig1)
205
+ return elms
206
+ }
207
+ `
208
+
209
+ const actual = generator(templateObject)
210
+
211
+ assert.equal(
212
+ normalize(actual.render.toString()),
213
+ normalize(expectedRender),
214
+ 'Generator should return a render function with the correct code'
215
+ )
216
+ assert.ok(
217
+ Array.isArray(actual.effects) && actual.effects.length === 0,
218
+ 'Generator should return an empty effects array'
219
+ )
220
+ assert.end()
221
+ })
222
+
223
+ test('Generate code for a template with attributes and a nested element with attributes', (assert) => {
224
+ const templateObject = {
225
+ children: [
226
+ {
227
+ type: 'Component',
228
+ x: 10,
229
+ y: 20,
230
+ children: [
231
+ {
232
+ type: 'Element',
233
+ w: 100,
234
+ h: 300,
235
+ },
236
+ ],
237
+ },
238
+ ],
239
+ }
240
+
241
+ const expectedRender = `
242
+ function anonymous(parent,component,context) {
243
+ const elms = []
244
+
245
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
246
+ const elementConfig1 = {}
247
+
248
+ elementConfig1['type'] = "Component"
249
+ elementConfig1['x'] = 10
250
+ elementConfig1['y'] = 20
251
+
252
+ elms[1].populate(elementConfig1)
253
+
254
+ parent = elms[1]
255
+
256
+ elms[2] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
257
+ const elementConfig2 = {}
258
+
259
+ elementConfig2['type'] = "Element"
260
+ elementConfig2['w'] = 100
261
+ elementConfig2['h'] = 300
262
+
263
+ elms[2].populate(elementConfig2)
264
+
265
+ return elms
266
+ }
267
+ `
268
+
269
+ const actual = generator(templateObject)
270
+
271
+ assert.equal(
272
+ normalize(actual.render.toString()),
273
+ normalize(expectedRender),
274
+ 'Generator should return a render function with the correct code'
275
+ )
276
+ assert.ok(
277
+ Array.isArray(actual.effects) && actual.effects.length === 0,
278
+ 'Generator should return an empty effects array'
279
+ )
280
+ assert.end()
281
+ })
282
+
283
+ test('Generate code for a template with attributes and 2 nested elements with attributes', (assert) => {
284
+ const templateObject = {
285
+ children: [
286
+ {
287
+ type: 'Component',
288
+ x: 10,
289
+ y: 20,
290
+ children: [
291
+ {
292
+ type: 'Element',
293
+ w: 100,
294
+ h: 300,
295
+ x: 0,
296
+ },
297
+ {
298
+ type: 'Element',
299
+ w: 100,
300
+ h: 300,
301
+ x: 50,
302
+ },
303
+ ],
304
+ },
305
+ ],
306
+ }
307
+
308
+ const expectedRender = `
309
+ function anonymous(parent,component,context) {
310
+ const elms = []
311
+
312
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
313
+ const elementConfig1 = {}
314
+
315
+ elementConfig1['type'] = "Component"
316
+ elementConfig1['x'] = 10
317
+ elementConfig1['y'] = 20
318
+
319
+ elms[1].populate(elementConfig1)
320
+
321
+ parent = elms[1]
322
+
323
+ elms[2] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
324
+ const elementConfig2 = {}
325
+
326
+ elementConfig2['type'] = "Element"
327
+ elementConfig2['w'] = 100
328
+ elementConfig2['h'] = 300
329
+ elementConfig2['x'] = 0
330
+
331
+ elms[2].populate(elementConfig2)
332
+
333
+ parent = elms[1]
334
+
335
+ elms[3] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
336
+ const elementConfig3 = {}
337
+
338
+ elementConfig3['type'] = "Element"
339
+ elementConfig3['w'] = 100
340
+ elementConfig3['h'] = 300
341
+ elementConfig3['x'] = 50
342
+
343
+ elms[3].populate(elementConfig3)
344
+
345
+ return elms
346
+ }
347
+ `
348
+
349
+ const actual = generator(templateObject)
350
+
351
+ assert.equal(
352
+ normalize(actual.render.toString()),
353
+ normalize(expectedRender),
354
+ 'Generator should return a render function with the correct code'
355
+ )
356
+ assert.ok(
357
+ Array.isArray(actual.effects) && actual.effects.length === 0,
358
+ 'Generator should return an empty effects array'
359
+ )
360
+ assert.end()
361
+ })
362
+
363
+ test('Generate code for a template with attributes and deep nested elements with attributes', (assert) => {
364
+ const templateObject = {
365
+ children: [
366
+ {
367
+ type: 'Component',
368
+ x: 10,
369
+ y: 20,
370
+ children: [
371
+ {
372
+ type: 'Element',
373
+ w: 100,
374
+ h: 300,
375
+ x: 0,
376
+ },
377
+ {
378
+ type: 'Element',
379
+ w: 100,
380
+ h: 300,
381
+ x: 50,
382
+ children: [
383
+ {
384
+ type: 'Element',
385
+ label: 'Hello',
386
+ },
387
+ {
388
+ type: 'Element',
389
+ label: 'World',
390
+ },
391
+ ],
392
+ },
393
+ ],
394
+ },
395
+ ],
396
+ }
397
+
398
+ const expectedRender = `
399
+ function anonymous(parent,component,context) {
400
+ const elms = []
401
+
402
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
403
+ const elementConfig1 = {}
404
+
405
+ elementConfig1['type'] = "Component"
406
+ elementConfig1['x'] = 10
407
+ elementConfig1['y'] = 20
408
+
409
+ elms[1].populate(elementConfig1)
410
+
411
+ parent = elms[1]
412
+
413
+ elms[2] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
414
+ const elementConfig2 = {}
415
+
416
+ elementConfig2['type'] = "Element"
417
+ elementConfig2['w'] = 100
418
+ elementConfig2['h'] = 300
419
+ elementConfig2['x'] = 0
420
+
421
+ elms[2].populate(elementConfig2)
422
+
423
+ parent = elms[1]
424
+
425
+ elms[3] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
426
+ const elementConfig3 = {}
427
+
428
+ elementConfig3['type'] = "Element"
429
+ elementConfig3['w'] = 100
430
+ elementConfig3['h'] = 300
431
+ elementConfig3['x'] = 50
432
+
433
+ elms[3].populate(elementConfig3)
434
+
435
+ parent = elms[3]
436
+
437
+ elms[4] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
438
+ const elementConfig4 = {}
439
+
440
+ elementConfig4['type'] = "Element"
441
+ elementConfig4['label'] = "Hello"
442
+
443
+ elms[4].populate(elementConfig4)
444
+
445
+ parent = elms[3]
446
+
447
+ elms[5] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
448
+ const elementConfig5 = {}
449
+
450
+ elementConfig5['type'] = "Element"
451
+ elementConfig5['label'] = "World"
452
+
453
+ elms[5].populate(elementConfig5)
454
+
455
+ return elms
456
+ }
457
+ `
458
+
459
+ const actual = generator(templateObject)
460
+
461
+ assert.equal(
462
+ normalize(actual.render.toString()),
463
+ normalize(expectedRender),
464
+ 'Generator should return a render function with the correct code'
465
+ )
466
+ assert.ok(
467
+ Array.isArray(actual.effects) && actual.effects.length === 0,
468
+ 'Generator should return an empty effects array'
469
+ )
470
+ assert.end()
471
+ })
472
+
473
+ test('Generate code for a template with simple dynamic attributes', (assert) => {
474
+ const templateObject = {
475
+ children: [
476
+ {
477
+ type: 'Component',
478
+ x: 10,
479
+ y: 20,
480
+ ':w': '$foo',
481
+ ':h': '$test',
482
+ test: 'ok',
483
+ },
484
+ ],
485
+ }
486
+
487
+ const expectedRender = `
488
+ function anonymous(parent,component,context) {
489
+ const elms = []
490
+
491
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
492
+ const elementConfig1 = {}
493
+
494
+ elementConfig1['type'] = "Component"
495
+ elementConfig1['x'] = 10
496
+ elementConfig1['y'] = 20
497
+ elementConfig1['test'] = "ok"
498
+
499
+ elms[1].populate(elementConfig1)
500
+
501
+ return elms
502
+ }
503
+ `
504
+
505
+ const expectedEffect1 = `
506
+ function anonymous(component,elms,context) {
507
+ elms[1].set('w', component.foo)
508
+ }
509
+ `
510
+
511
+ const expectedEffect2 = `
512
+ function anonymous(component,elms,context) {
513
+ elms[1].set('h', component.test)
514
+ }
515
+ `
516
+
517
+ const actual = generator(templateObject)
518
+
519
+ assert.equal(
520
+ normalize(actual.render.toString()),
521
+ normalize(expectedRender),
522
+ 'Generator should return a render function with the correct code'
523
+ )
524
+ assert.ok(
525
+ Array.isArray(actual.effects) && actual.effects.length === 2,
526
+ 'Generator should return an effects array with 2 items'
527
+ )
528
+ assert.equal(
529
+ normalize(actual.effects[0].toString()),
530
+ normalize(expectedEffect1),
531
+ 'Generator should return first render function with the correct code'
532
+ )
533
+ assert.equal(
534
+ normalize(actual.effects[1].toString()),
535
+ normalize(expectedEffect2),
536
+ 'Generator should return second render function with the correct code'
537
+ )
538
+
539
+ assert.end()
540
+ })
541
+
542
+ test('Generate code for a template with an attribute with a dash', (assert) => {
543
+ const templateObject = {
544
+ children: [
545
+ {
546
+ type: 'Component',
547
+ 'my-Attribute': 'does it work?',
548
+ x: 10,
549
+ y: 20,
550
+ },
551
+ ],
552
+ }
553
+
554
+ const expectedRender = `
555
+ function anonymous(parent,component,context) {
556
+ const elms = []
557
+
558
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
559
+ const elementConfig1 = {}
560
+
561
+ elementConfig1['type'] = "Component"
562
+ elementConfig1['my-Attribute'] = "does it work?"
563
+ elementConfig1['x'] = 10
564
+ elementConfig1['y'] = 20
565
+
566
+ elms[1].populate(elementConfig1)
567
+
568
+ return elms
569
+ }
570
+ `
571
+
572
+ const actual = generator(templateObject)
573
+
574
+ assert.equal(
575
+ normalize(actual.render.toString()),
576
+ normalize(expectedRender),
577
+ 'Generator should return a render function with the correct code'
578
+ )
579
+ assert.ok(
580
+ Array.isArray(actual.effects) && actual.effects.length === 0,
581
+ 'Generator should return an empty effects array'
582
+ )
583
+ assert.end()
584
+ })
585
+
586
+ test('Generate code for a template with dynamic attributes with code to be evaluated', (assert) => {
587
+ const templateObject = {
588
+ children: [
589
+ {
590
+ type: 'Component',
591
+ ':attribute1': '$foo * 2',
592
+ ':attribute2': "$ok ? 'Yes' : 'No'",
593
+ ':attribute3': "$text.split('').reverse().join('')",
594
+ // ':attribute4': '$bar.foo * $blah.hello' => todo
595
+ },
596
+ ],
597
+ }
598
+
599
+ const expectedRender = `
600
+ function anonymous(parent,component,context) {
601
+ const elms = []
602
+
603
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
604
+ const elementConfig1 = {}
605
+
606
+ elementConfig1['type'] = "Component"
607
+
608
+ elms[1].populate(elementConfig1)
609
+
610
+ return elms
611
+ }
612
+ `
613
+
614
+ const expectedEffect1 = `
615
+ function anonymous(component,elms,context) {
616
+ elms[1].set('attribute1', component.foo * 2)
617
+ }
618
+ `
619
+
620
+ const expectedEffect2 = `
621
+ function anonymous(component,elms,context) {
622
+ elms[1].set('attribute2', component.ok ? 'Yes' : 'No')
623
+ }
624
+ `
625
+
626
+ const expectedEffect3 = `
627
+ function anonymous(component,elms,context) {
628
+ elms[1].set('attribute3', component.text.split('').reverse().join(''))
629
+ }
630
+ `
631
+
632
+ const actual = generator(templateObject)
633
+
634
+ assert.equal(
635
+ normalize(actual.render.toString()),
636
+ normalize(expectedRender),
637
+ 'Generator should return a render function with the correct code'
638
+ )
639
+ assert.ok(
640
+ Array.isArray(actual.effects) && actual.effects.length === 3,
641
+ 'Generator should return an effects array with 3 items'
642
+ )
643
+ assert.equal(
644
+ normalize(actual.effects[0].toString()),
645
+ normalize(expectedEffect1),
646
+ 'Generator should return first render function with the correct code'
647
+ )
648
+ assert.equal(
649
+ normalize(actual.effects[1].toString()),
650
+ normalize(expectedEffect2),
651
+ 'Generator should return second render function with the correct code'
652
+ )
653
+ assert.equal(
654
+ normalize(actual.effects[2].toString()),
655
+ normalize(expectedEffect3),
656
+ 'Generator should return third render function with the correct code'
657
+ )
658
+
659
+ assert.end()
660
+ })
661
+
662
+ test('Generate code for a template with custom components', (assert) => {
663
+ const templateObject = {
664
+ children: [
665
+ {
666
+ type: 'Component',
667
+ children: [
668
+ {
669
+ type: 'Poster',
670
+ },
671
+ {
672
+ type: 'Poster',
673
+ },
674
+ ],
675
+ },
676
+ ],
677
+ }
678
+
679
+ const scope = {
680
+ components: {
681
+ Poster: () => {},
682
+ },
683
+ }
684
+
685
+ const expectedRender = `
686
+ function anonymous(parent,component,context) {
687
+ const elms = []
688
+
689
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
690
+ const elementConfig1 = {}
691
+
692
+ elementConfig1['type'] = "Component"
693
+
694
+ elms[1].populate(elementConfig1)
695
+
696
+ elms[2] = context['Poster'].call(null, context.props[0],elms[1], component)
697
+ elms[3] = context['Poster'].call(null, context.props[1],elms[1], component)
698
+
699
+ return elms
700
+ }
701
+ `
702
+
703
+ const actual = generator.call(scope, templateObject)
704
+
705
+ assert.equal(
706
+ normalize(actual.render.toString()),
707
+ normalize(expectedRender),
708
+ 'Generator should return a render function with the correct code'
709
+ )
710
+ assert.ok(
711
+ Array.isArray(actual.effects) && actual.effects.length === 0,
712
+ 'Generator should return an empty effects array'
713
+ )
714
+
715
+ assert.end()
716
+ })
717
+
718
+ test('Generate code for a template with an unregistered custom component', (assert) => {
719
+ const templateObject = {
720
+ children: [
721
+ {
722
+ type: 'Component',
723
+ children: [
724
+ {
725
+ type: 'Poster',
726
+ },
727
+ {
728
+ type: 'Poster2',
729
+ },
730
+ ],
731
+ },
732
+ ],
733
+ }
734
+
735
+ const scope = {
736
+ components: {
737
+ Poster: () => {},
738
+ },
739
+ }
740
+
741
+ const expectedRender = `
742
+ function anonymous(parent,component,context) {
743
+ const elms = []
744
+
745
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
746
+ const elementConfig1 = {}
747
+
748
+ elementConfig1['type'] = "Component"
749
+
750
+ elms[1].populate(elementConfig1)
751
+
752
+ elms[2] = context['Poster'].call(null, context.props[0],elms[1], component)
753
+
754
+ parent = elms[1]
755
+
756
+ elms[3] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
757
+ const elementConfig3 = {}
758
+
759
+ elementConfig3['type'] = "Poster2"
760
+
761
+ elms[3].populate(elementConfig3)
762
+
763
+ return elms
764
+ }
765
+ `
766
+
767
+ const actual = generator.call(scope, templateObject)
768
+
769
+ assert.equal(
770
+ normalize(actual.render.toString()),
771
+ normalize(expectedRender),
772
+ 'Generator should return a render function with the correct code'
773
+ )
774
+ assert.ok(
775
+ Array.isArray(actual.effects) && actual.effects.length === 0,
776
+ 'Generator should return an empty effects array'
777
+ )
778
+
779
+ assert.end()
780
+ })
781
+
782
+ test('Generate code for a template with custom components with arguments', (assert) => {
783
+ const templateObject = {
784
+ children: [
785
+ {
786
+ type: 'Component',
787
+ children: [
788
+ {
789
+ type: 'Poster',
790
+ x: 10,
791
+ },
792
+ {
793
+ type: 'Poster',
794
+ x: 100,
795
+ img: '$img',
796
+ },
797
+ ],
798
+ },
799
+ ],
800
+ }
801
+
802
+ const scope = {
803
+ components: {
804
+ Poster: () => {},
805
+ },
806
+ }
807
+
808
+ const expectedRender = `
809
+ function anonymous(parent,component,context) {
810
+ const elms = []
811
+
812
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
813
+ const elementConfig1 = {}
814
+
815
+ elementConfig1['type'] = "Component"
816
+
817
+ elms[1].populate(elementConfig1)
818
+
819
+ elms[2] = context['Poster'].call(null, context.props[0],elms[1], component)
820
+
821
+ context.props[1].props.img = component.img
822
+
823
+ elms[3] = context['Poster'].call(null, context.props[1],elms[1], component)
824
+
825
+ return elms
826
+ }
827
+ `
828
+
829
+ const actual = generator.call(scope, templateObject)
830
+
831
+ assert.equal(
832
+ normalize(actual.render.toString()),
833
+ normalize(expectedRender),
834
+ 'Generator should return a render function with the correct code'
835
+ )
836
+ assert.ok(
837
+ Array.isArray(actual.effects) && actual.effects.length === 0,
838
+ 'Generator should return an empty effects array'
839
+ )
840
+
841
+ assert.end()
842
+ })
843
+
844
+ test('Generate code for a template with custom components with reactive props', (assert) => {
845
+ const templateObject = {
846
+ children: [
847
+ {
848
+ type: 'Component',
849
+ children: [
850
+ {
851
+ type: 'Poster',
852
+ x: 10,
853
+ ':img': '$image',
854
+ },
855
+ {
856
+ type: 'Poster',
857
+ x: 100,
858
+ ':img': '$image',
859
+ },
860
+ ],
861
+ },
862
+ ],
863
+ }
864
+
865
+ const scope = {
866
+ components: {
867
+ Poster: () => {},
868
+ },
869
+ }
870
+
871
+ const expectedRender = `
872
+ function anonymous(parent,component,context) {
873
+ const elms = []
874
+
875
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
876
+ const elementConfig1 = {}
877
+
878
+ elementConfig1['type'] = "Component"
879
+
880
+ elms[1].populate(elementConfig1)
881
+
882
+ context.props[0].props.img = component.image
883
+ elms[2] = context['Poster'].call(null, context.props[0],elms[1], component)
884
+
885
+ context.props[1].props.img = component.image
886
+ elms[3] = context['Poster'].call(null, context.props[1],elms[1], component)
887
+
888
+ return elms
889
+ }
890
+ `
891
+
892
+ const expectedEffect1 = `
893
+ function anonymous(component,elms,context) {
894
+ elms[2].___props.img = component.image
895
+ }
896
+ `
897
+
898
+ const expectedEffect2 = `
899
+ function anonymous(component,elms,context) {
900
+ elms[3].___props.img = component.image
901
+ }
902
+ `
903
+
904
+ const actual = generator.call(scope, templateObject)
905
+
906
+ assert.equal(
907
+ normalize(actual.render.toString()),
908
+ normalize(expectedRender),
909
+ 'Generator should return a render function with the correct code'
910
+ )
911
+ assert.ok(
912
+ Array.isArray(actual.effects) && actual.effects.length === 2,
913
+ 'Generator should return an effects array with 2 function'
914
+ )
915
+ assert.equal(
916
+ normalize(actual.effects[0].toString()),
917
+ normalize(expectedEffect1),
918
+ 'Generator should return an effect function that updates a prop on the first custom component'
919
+ )
920
+
921
+ assert.equal(
922
+ normalize(actual.effects[1].toString()),
923
+ normalize(expectedEffect2),
924
+ 'Generator should return an effect function that updates a prop on the second custom component'
925
+ )
926
+
927
+ assert.end()
928
+ })
929
+
930
+ test('Generate code for a template with a transition attributes', (assert) => {
931
+ const templateObject = {
932
+ children: [
933
+ {
934
+ type: 'Component',
935
+ x: '{transition: $myX}',
936
+ y: '{transition: {v: $myY, d: 600, p: 100}}',
937
+ },
938
+ ],
939
+ }
940
+
941
+ const expectedRender = `
942
+ function anonymous(parent,component,context) {
943
+ const elms = []
944
+
945
+ elms[1] = this.element({componentId: component.___id, parentId: parent && parent.nodeId || 'root'})
946
+ const elementConfig1 = {}
947
+
948
+ elementConfig1['type'] = "Component"
949
+ elementConfig1['x'] = "{transition: $myX}"
950
+ elementConfig1['y'] = "{transition: {v: $myY, d: 600, p: 100}}"
951
+
952
+ elms[1].populate(elementConfig1)
953
+
954
+ return elms
955
+ }
956
+ `
957
+
958
+ const actual = generator(templateObject)
959
+
960
+ assert.equal(
961
+ normalize(actual.render.toString()),
962
+ normalize(expectedRender),
963
+ 'Generator should return a render function with the correct code'
964
+ )
965
+ assert.ok(
966
+ Array.isArray(actual.effects) && actual.effects.length === 0,
967
+ 'Generator should return an empty effects array'
968
+ )
969
+ assert.end()
970
+ })