-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
standalone_spec.ts
1011 lines (848 loc) Β· 28.4 KB
/
standalone_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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {CommonModule, NgComponentOutlet} from '@angular/common';
import {Component, createEnvironmentInjector, Directive, EnvironmentInjector, forwardRef, inject, Injectable, Injector, Input, isStandalone, NgModule, NO_ERRORS_SCHEMA, OnInit, Pipe, PipeTransform, ViewChild, ViewContainerRef} from '@angular/core';
import {TestBed} from '@angular/core/testing';
describe('standalone components, directives, and pipes', () => {
it('should render a standalone component', () => {
@Component({
standalone: true,
template: 'Look at me, no NgModule!',
})
class StandaloneCmp {
}
const fixture = TestBed.createComponent(StandaloneCmp);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toEqual('Look at me, no NgModule!');
});
it('should render a recursive standalone component', () => {
@Component({
selector: 'tree',
standalone: true,
template:
`({{level}})<ng-template [ngIf]="level > 0"><tree [level]="level - 1"></tree></ng-template>`,
imports: [CommonModule]
})
class TreeCmp {
@Input() level = 0;
}
@Component({standalone: true, template: '<tree [level]="3"></tree>', imports: [TreeCmp]})
class StandaloneCmp {
}
const fixture = TestBed.createComponent(StandaloneCmp);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('(3)(2)(1)(0)');
});
it('should render a standalone component with a standalone dependency', () => {
@Component({
standalone: true,
selector: 'inner-cmp',
template: 'Look at me, no NgModule!',
})
class InnerCmp {
}
@Component({
standalone: true,
template: '<inner-cmp></inner-cmp>',
imports: [InnerCmp],
})
class StandaloneCmp {
}
const fixture = TestBed.createComponent(StandaloneCmp);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML)
.toEqual('<inner-cmp>Look at me, no NgModule!</inner-cmp>');
});
it('should render a standalone component with an NgModule-based dependency', () => {
@Component({
selector: 'inner-cmp',
template: 'Look at me, no NgModule (kinda)!',
})
class InnerCmp {
}
@NgModule({
declarations: [InnerCmp],
exports: [InnerCmp],
})
class Module {
}
@Component({
standalone: true,
template: '<inner-cmp></inner-cmp>',
imports: [Module],
})
class StandaloneCmp {
}
const fixture = TestBed.createComponent(StandaloneCmp);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML)
.toEqual('<inner-cmp>Look at me, no NgModule (kinda)!</inner-cmp>');
});
it('should allow exporting standalone components, directives, and pipes from NgModule', () => {
@Component({
selector: 'standalone-cmp',
standalone: true,
template: `standalone`,
})
class StandaloneCmp {
}
@Directive({
selector: '[standalone-dir]',
host: {
'[attr.id]': '"standalone"',
},
standalone: true
})
class StandaloneDir {
}
@Pipe({name: 'standalonePipe', standalone: true})
class StandalonePipe implements PipeTransform {
transform(value: any) {
return `|${value}`;
}
}
@NgModule({
imports: [StandaloneCmp, StandaloneDir, StandalonePipe],
exports: [StandaloneCmp, StandaloneDir, StandalonePipe],
})
class LibModule {
}
@Component({
selector: 'app-cmpt',
template: `<standalone-cmp standalone-dir></standalone-cmp>{{'standalone' | standalonePipe}}`,
})
class AppComponent {
}
TestBed.configureTestingModule({
imports: [LibModule],
declarations: [AppComponent],
});
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('standalone|standalone');
expect(fixture.nativeElement.querySelector('standalone-cmp').getAttribute('id'))
.toBe('standalone');
});
it('should render a standalone component with dependencies and ambient providers', () => {
@Component({
standalone: true,
template: 'Inner',
selector: 'inner-cmp',
})
class InnerCmp {
}
class Service {
value = 'Service';
}
@NgModule({providers: [Service]})
class ModuleWithAProvider {
}
@Component({
standalone: true,
template: 'Outer<inner-cmp></inner-cmp>{{service.value}}',
imports: [InnerCmp, ModuleWithAProvider],
})
class OuterCmp {
constructor(readonly service: Service) {}
}
const fixture = TestBed.createComponent(OuterCmp);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toEqual('Outer<inner-cmp>Inner</inner-cmp>Service');
});
it('should discover ambient providers from a standalone component', () => {
class Service {
value = 'Service';
}
@NgModule({providers: [Service]})
class ModuleWithAProvider {
}
@Component({
standalone: true,
template: 'Inner({{service.value}})',
selector: 'inner-cmp',
imports: [ModuleWithAProvider],
})
class InnerCmp {
constructor(readonly service: Service) {}
}
@Component({
standalone: true,
template: 'Outer<inner-cmp></inner-cmp>{{service.value}}',
imports: [InnerCmp],
})
class OuterCmp {
constructor(readonly service: Service) {}
}
const fixture = TestBed.createComponent(OuterCmp);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML)
.toEqual('Outer<inner-cmp>Inner(Service)</inner-cmp>Service');
});
it('should correctly associate an injector with a standalone component def', () => {
@Injectable()
class MyServiceA {
}
@Injectable()
class MyServiceB {
}
@NgModule({
providers: [MyServiceA],
})
class MyModuleA {
}
@NgModule({
providers: [MyServiceB],
})
class MyModuleB {
}
@Component({
selector: 'duplicate-selector',
template: `ComponentA: {{ service ? 'service found' : 'service not found' }}`,
standalone: true,
imports: [MyModuleA],
})
class ComponentA {
service = inject(MyServiceA, {optional: true});
}
@Component({
selector: 'duplicate-selector',
template: `ComponentB: {{ service ? 'service found' : 'service not found' }}`,
standalone: true,
imports: [MyModuleB],
})
class ComponentB {
service = inject(MyServiceB, {optional: true});
}
@Component({
selector: 'app-cmp',
template: `
<ng-container [ngComponentOutlet]="ComponentA" />
<ng-container [ngComponentOutlet]="ComponentB" />
`,
standalone: true,
imports: [NgComponentOutlet],
})
class AppCmp {
ComponentA = ComponentA;
ComponentB = ComponentB;
}
const fixture = TestBed.createComponent(AppCmp);
fixture.detectChanges();
const textContent = fixture.nativeElement.textContent;
expect(textContent).toContain('ComponentA: service found');
expect(textContent).toContain('ComponentB: service found');
});
it('should dynamically insert a standalone component', () => {
class Service {
value = 'Service';
}
@NgModule({providers: [Service]})
class Module {
}
@Component({
standalone: true,
template: 'Inner({{service.value}})',
selector: 'inner-cmp',
imports: [Module],
})
class InnerCmp {
constructor(readonly service: Service) {}
}
@Component({
standalone: true,
template: '<ng-template #insert></ng-template>',
imports: [InnerCmp],
})
class AppCmp implements OnInit {
@ViewChild('insert', {read: ViewContainerRef, static: true}) vcRef!: ViewContainerRef;
ngOnInit(): void {
this.vcRef.createComponent(InnerCmp);
}
}
const fixture = TestBed.createComponent(AppCmp);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('Inner(Service)');
});
it('should dynamically insert a standalone component with ambient providers override in the "left / node" injector',
() => {
class Service {
constructor(readonly value = 'Service') {}
}
class NodeOverrideService extends Service {
constructor() {
super('NodeOverrideService');
}
}
class EnvOverrideService extends Service {
constructor() {
super('EnvOverrideService');
}
}
@NgModule({providers: [Service]})
class Module {
}
@Component({
standalone: true,
template: 'Inner({{service.value}})',
selector: 'inner-cmp',
imports: [Module],
})
class InnerCmp {
constructor(readonly service: Service) {}
}
@Component({
standalone: true,
template: '<ng-template #insert></ng-template>',
imports: [InnerCmp],
})
class AppCmp implements OnInit {
@ViewChild('insert', {read: ViewContainerRef, static: true}) vcRef!: ViewContainerRef;
constructor(readonly inj: Injector, readonly envInj: EnvironmentInjector) {}
ngOnInit(): void {
const lhsInj = Injector.create({
providers: [{provide: Service, useClass: NodeOverrideService}],
parent: this.inj,
});
const rhsInj = createEnvironmentInjector(
[{provide: Service, useClass: EnvOverrideService}], this.envInj);
this.vcRef.createComponent(InnerCmp, {injector: lhsInj, environmentInjector: rhsInj});
}
}
const fixture = TestBed.createComponent(AppCmp);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('Inner(NodeOverrideService)');
});
it('should consult ambient providers before environement injector when inserting a component dynamically',
() => {
class Service {
constructor(readonly value = 'Service') {}
}
class EnvOverrideService extends Service {
constructor() {
super('EnvOverrideService');
}
}
@NgModule({providers: [Service]})
class Module {
}
@Component({
standalone: true,
template: 'Inner({{service.value}})',
selector: 'inner-cmp',
imports: [Module],
})
class InnerCmp {
constructor(readonly service: Service) {}
}
@Component({
standalone: true,
template: '<ng-template #insert></ng-template>',
imports: [InnerCmp],
})
class AppCmp implements OnInit {
@ViewChild('insert', {read: ViewContainerRef, static: true}) vcRef!: ViewContainerRef;
constructor(readonly envInj: EnvironmentInjector) {}
ngOnInit(): void {
const rhsInj = createEnvironmentInjector(
[{provide: Service, useClass: EnvOverrideService}], this.envInj);
this.vcRef.createComponent(InnerCmp, {environmentInjector: rhsInj});
}
}
const fixture = TestBed.createComponent(AppCmp);
fixture.detectChanges();
// The Service (an ambient provider) gets injected here as the standalone injector is a child
// of the user-created environement injector.
expect(fixture.nativeElement.textContent).toBe('Inner(Service)');
});
it('should render a recursive cycle of standalone components', () => {
@Component({
selector: 'cmp-a',
standalone: true,
template: '<ng-template [ngIf]="false"><cmp-c></cmp-c></ng-template>A',
imports: [forwardRef(() => StandaloneCmpC)],
})
class StandaloneCmpA {
}
@Component({
selector: 'cmp-b',
standalone: true,
template: '(<cmp-a></cmp-a>)B',
imports: [StandaloneCmpA],
})
class StandaloneCmpB {
}
@Component({
selector: 'cmp-c',
standalone: true,
template: '(<cmp-b></cmp-b>)C',
imports: [StandaloneCmpB],
})
class StandaloneCmpC {
}
const fixture = TestBed.createComponent(StandaloneCmpC);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('((A)B)C');
});
it('should collect ambient providers from exported NgModule', () => {
class Service {
value = 'service';
}
@NgModule({providers: [Service]})
class ModuleWithAService {
}
@NgModule({exports: [ModuleWithAService]})
class ExportingModule {
}
@Component({
selector: 'standalone',
standalone: true,
imports: [ExportingModule],
template: `({{service.value}})`
})
class TestComponent {
constructor(readonly service: Service) {}
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('(service)');
});
it('should support nested arrays in @Component.imports', () => {
@Directive({selector: '[red]', standalone: true, host: {'[attr.red]': 'true'}})
class RedIdDirective {
}
@Pipe({name: 'blue', pure: true, standalone: true})
class BluePipe implements PipeTransform {
transform() {
return 'blue';
}
}
@Component({
selector: 'standalone',
standalone: true,
template: `<div red>{{'' | blue}}</div>`,
imports: [[RedIdDirective, [BluePipe]]],
})
class TestComponent {
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe('<div red="true">blue</div>');
});
it('should support readonly arrays in @Component.imports', () => {
@Directive({selector: '[red]', standalone: true, host: {'[attr.red]': 'true'}})
class RedIdDirective {
}
@Pipe({name: 'blue', pure: true, standalone: true})
class BluePipe implements PipeTransform {
transform() {
return 'blue';
}
}
const DirAndPipe = [RedIdDirective, BluePipe] as const;
@Component({
selector: 'standalone',
standalone: true,
template: `<div red>{{'' | blue}}</div>`,
imports: [DirAndPipe],
})
class TestComponent {
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe('<div red="true">blue</div>');
});
it('should deduplicate declarations', () => {
@Component({selector: 'test-red', standalone: true, template: 'red(<ng-content></ng-content>)'})
class RedComponent {
}
@Component({selector: 'test-blue', template: 'blue(<ng-content></ng-content>)'})
class BlueComponent {
}
@NgModule({declarations: [BlueComponent], exports: [BlueComponent]})
class BlueModule {
}
@NgModule({exports: [BlueModule]})
class BlueAModule {
}
@NgModule({exports: [BlueModule]})
class BlueBModule {
}
@Component({
selector: 'standalone',
standalone: true,
template: `<test-red><test-blue>orange</test-blue></test-red>`,
imports: [RedComponent, RedComponent, BlueAModule, BlueBModule],
})
class TestComponent {
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML)
.toBe('<test-red>red(<test-blue>blue(orange)</test-blue>)</test-red>');
});
it('should error when forwardRef does not resolve to a truthy value', () => {
@Component({
selector: 'test',
standalone: true,
imports: [forwardRef(() => null)],
template: '',
})
class TestComponent {
}
expect(() => {
TestBed.createComponent(TestComponent);
})
.toThrowError(
'Expected forwardRef function, imported from "TestComponent", to return a standalone entity or NgModule but got "null".');
});
it('should error when a non-standalone component is imported', () => {
@Component({
selector: 'not-a-standalone',
template: '',
})
class NonStandaloneCmp {
}
@Component({
selector: 'standalone',
standalone: true,
template: '',
imports: [NonStandaloneCmp],
})
class StandaloneCmp {
}
expect(() => {
TestBed.createComponent(StandaloneCmp);
})
.toThrowError(
'The "NonStandaloneCmp" component, imported from "StandaloneCmp", is not standalone. Did you forget to add the standalone: true flag?');
});
it('should error when a non-standalone directive is imported', () => {
@Directive({selector: '[not-a-standalone]'})
class NonStandaloneDirective {
}
@Component({
selector: 'standalone',
standalone: true,
template: '',
imports: [NonStandaloneDirective],
})
class StandaloneCmp {
}
expect(() => {
TestBed.createComponent(StandaloneCmp);
})
.toThrowError(
'The "NonStandaloneDirective" directive, imported from "StandaloneCmp", is not standalone. Did you forget to add the standalone: true flag?');
});
it('should error when a non-standalone pipe is imported', () => {
@Pipe({name: 'not-a-standalone'})
class NonStandalonePipe {
}
@Component({
selector: 'standalone',
standalone: true,
template: '',
imports: [NonStandalonePipe],
})
class StandaloneCmp {
}
expect(() => {
TestBed.createComponent(StandaloneCmp);
})
.toThrowError(
'The "NonStandalonePipe" pipe, imported from "StandaloneCmp", is not standalone. Did you forget to add the standalone: true flag?');
});
it('should error when an unknown type is imported', () => {
class SthElse {}
@Component({
selector: 'standalone',
standalone: true,
template: '',
imports: [SthElse],
})
class StandaloneCmp {
}
expect(() => {
TestBed.createComponent(StandaloneCmp);
})
.toThrowError(
'The "SthElse" type, imported from "StandaloneCmp", must be a standalone component / directive / pipe or an NgModule. Did you forget to add the required @Component / @Directive / @Pipe or @NgModule annotation?');
});
it('should error when a module with providers is imported', () => {
@NgModule()
class OtherModule {
}
@NgModule()
class LibModule {
static forComponent() {
return {ngModule: OtherModule};
}
}
@Component({
standalone: true,
template: '',
// we need to import a module with a provider in a nested array since module with providers
// are disallowed on the type level
imports: [[LibModule.forComponent()]],
})
class StandaloneCmp {
}
expect(() => {
TestBed.createComponent(StandaloneCmp);
})
.toThrowError(
'A module with providers was imported from "StandaloneCmp". Modules with providers are not supported in standalone components imports.');
});
it('should support forwardRef imports', () => {
@Component({
selector: 'test',
standalone: true,
imports: [forwardRef(() => StandaloneComponent)],
template: `(<other-standalone></other-standalone>)`
})
class TestComponent {
}
@Component({selector: 'other-standalone', standalone: true, template: `standalone component`})
class StandaloneComponent {
}
const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('(standalone component)');
});
describe('schemas', () => {
it('should allow schemas in standalone component', () => {
@Component({
standalone: true,
template: '<maybe-custom-elm></maybe-custom-elm>',
schemas: [NO_ERRORS_SCHEMA]
})
class AppCmp {
}
const fixture = TestBed.createComponent(AppCmp);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe('<maybe-custom-elm></maybe-custom-elm>');
});
it('should error when schemas are specified for a non-standalone component', () => {
@Component({template: '', schemas: [NO_ERRORS_SCHEMA]})
class AppCmp {
}
expect(() => {
TestBed.createComponent(AppCmp);
})
.toThrowError(
`The 'schemas' was specified for the AppCmp but is only valid on a component that is standalone.`);
});
});
describe('unknown template elements', () => {
const unknownElErrorRegex = (tag: string) => {
const prefix =
`'${tag}' is not a known element \\(used in the 'AppCmp' component template\\):`;
const message1 = `1. If '${
tag}' is an Angular component, then verify that it is included in the '@Component.imports' of this component.`;
const message2 = `2. If '${
tag}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@Component.schemas' of this component to suppress this message.`;
return new RegExp(`${prefix}s*\ns*${message1}s*\ns*${message2}`);
};
it('should warn the user when an unknown element is present', () => {
const spy = spyOn(console, 'error');
@Component({
standalone: true,
template: '<unknown-tag></unknown-tag>',
})
class AppCmp {
}
TestBed.createComponent(AppCmp);
const errorRegex = unknownElErrorRegex('unknown-tag');
expect(spy).toHaveBeenCalledOnceWith(jasmine.stringMatching(errorRegex));
});
it('should warn the user when multiple unknown elements are present', () => {
const spy = spyOn(console, 'error');
@Component({
standalone: true,
template: '<unknown-tag-A></unknown-tag-A><unknown-tag-B></unknown-tag-B>',
})
class AppCmp {
}
TestBed.createComponent(AppCmp);
const errorRegexA = unknownElErrorRegex('unknown-tag-A');
const errorRegexB = unknownElErrorRegex('unknown-tag-B');
expect(spy).toHaveBeenCalledWith(jasmine.stringMatching(errorRegexA));
expect(spy).toHaveBeenCalledWith(jasmine.stringMatching(errorRegexB));
});
it('should not warn the user when an unknown element is present inside an ng-template', () => {
const spy = spyOn(console, 'error');
@Component({
standalone: true,
template: '<ng-template><unknown-tag></unknown-tag><ng-template>',
})
class AppCmp {
}
TestBed.createComponent(AppCmp);
expect(spy).not.toHaveBeenCalled();
});
it('should warn the user when an unknown element is present in an instantiated embedded view',
() => {
const spy = spyOn(console, 'error');
@Component({
standalone: true,
template: '<ng-template [ngIf]="true"><unknown-tag></unknown-tag><ng-template>',
imports: [CommonModule],
})
class AppCmp {
}
const fixture = TestBed.createComponent(AppCmp);
fixture.detectChanges();
const errorRegex = unknownElErrorRegex('unknown-tag');
expect(spy).toHaveBeenCalledOnceWith(jasmine.stringMatching(errorRegex));
});
});
/*
The following test verify that we don't impose limits when it comes to extending components of
various type (standalone vs. non-standalone).
This is based on the reasoning that the "template"
/ "templateUrl", "imports", "schemas" and "standalone" properties are all related and they
specify how to compile a template. As of today extending a component means providing a new
template and this implies providing compiler configuration for a new template. In this sense
neither a template nor its compiler configuration is carried over from a class being extended
(we can think of each component being a "fresh copy" when it comes to a template and its
compiler configuration).
*/
describe('inheritance', () => {
it('should allow extending a regular component and turn it into a standalone one', () => {
@Component({selector: 'regular', template: 'regular: {{in}}'})
class RegularCmp {
@Input() in : string|undefined;
}
@Component({selector: 'standalone', template: 'standalone: {{in}}', standalone: true})
class StandaloneCmp extends RegularCmp {
}
const fixture = TestBed.createComponent(StandaloneCmp);
fixture.componentInstance.in = 'input value';
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('standalone: input value');
});
it('should allow extending a regular component and turn it into a standalone one', () => {
@Component({selector: 'standalone', template: 'standalone: {{in}}', standalone: true})
class StandaloneCmp {
@Input() in : string|undefined;
}
@Component({selector: 'regular', template: 'regular: {{in}}'})
class RegularCmp extends StandaloneCmp {
}
const fixture = TestBed.createComponent(RegularCmp);
fixture.componentInstance.in = 'input value';
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('regular: input value');
});
it('should ?', () => {
@Component({
selector: 'inner',
template: 'inner',
standalone: true,
})
class InnerCmp {
}
@Component({
selector: 'standalone',
standalone: true,
template: 'standalone: {{in}}; (<inner></inner>)',
imports: [InnerCmp]
})
class StandaloneCmp {
@Input() in : string|undefined;
}
@Component({selector: 'regular'})
class RegularCmp extends StandaloneCmp {
}
const fixture = TestBed.createComponent(RegularCmp);
fixture.componentInstance.in = 'input value';
fixture.detectChanges();
// the assumption here is that not providing a template is equivalent to providing an empty
// one
expect(fixture.nativeElement.textContent).toBe('');
});
});
describe('isStandalone()', () => {
it('should return `true` if component is standalone', () => {
@Component({selector: 'standalone-cmp', standalone: true})
class StandaloneCmp {
}
expect(isStandalone(StandaloneCmp)).toBeTrue();
});
it('should return `false` if component is not standalone', () => {
@Component({selector: 'standalone-cmp', standalone: false})
class StandaloneCmp {
}
expect(isStandalone(StandaloneCmp)).toBeFalse();
});
it('should return `true` if directive is standalone', () => {
@Directive({selector: '[standaloneDir]', standalone: true})
class StandAloneDirective {
}
expect(isStandalone(StandAloneDirective)).toBeTrue();
});
it('should return `false` if directive is standalone', () => {
@Directive({selector: '[standaloneDir]', standalone: false})
class StandAloneDirective {
}
expect(isStandalone(StandAloneDirective)).toBeFalse();
});
it('should return `true` if pipe is standalone', () => {
@Pipe({name: 'standalonePipe', standalone: true})
class StandAlonePipe {
}
expect(isStandalone(StandAlonePipe)).toBeTrue();
});
it('should return `false` if pipe is standalone', () => {
@Pipe({name: 'standalonePipe', standalone: false})
class StandAlonePipe {
}
expect(isStandalone(StandAlonePipe)).toBeFalse();
});
it('should return `false` if the class is not annotated', () => {
class NonAnnotatedClass {}
expect(isStandalone(NonAnnotatedClass)).toBeFalse();
});
it('should return `false` if the class is an NgModule', () => {
@NgModule({})
class Module {
}
expect(isStandalone(Module)).toBeFalse();
});
it('should render a recursive cycle of standalone components', () => {
@Component({
selector: 'cmp-a',
standalone: true,
template: '<ng-template [ngIf]="false"><cmp-c></cmp-c></ng-template>A',
imports: [forwardRef(() => StandaloneCmpC)],
})
class StandaloneCmpA {
}
@Component({
selector: 'cmp-b',
standalone: true,
template: '(<cmp-a></cmp-a>)B',
imports: [StandaloneCmpA],
})
class StandaloneCmpB {
}
@Component({
selector: 'cmp-c',
standalone: true,
template: '(<cmp-b></cmp-b>)C',
imports: [StandaloneCmpB],