@lightningjs/blits 0.7.0 → 0.7.2

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