17
17
18
18
import { CUSTOM_ELEMENTS_SCHEMA } from "@angular/core" ;
19
19
import { TestBed } from "@angular/core/testing" ;
20
- import { By } from "@angular/platform-browser" ;
21
- import { expect } from "chai" ;
20
+
22
21
import {
23
22
ComponentWithoutChildren ,
24
23
ComponentWithChildren ,
@@ -30,6 +29,8 @@ import {
30
29
ComponentWithDeclarativeEvent
31
30
} from "./components" ;
32
31
32
+ import tests from 'basic-tests' ;
33
+
33
34
beforeEach ( function ( ) {
34
35
TestBed . configureTestingModule ( {
35
36
declarations : [
@@ -46,121 +47,44 @@ beforeEach(function() {
46
47
} ) ;
47
48
} ) ;
48
49
49
- describe ( "basic support" , function ( ) {
50
+ function render ( Component ) {
51
+ const fixture = TestBed . createComponent ( Component ) ;
52
+ fixture . detectChanges ( ) ;
53
+ const el = fixture . debugElement . nativeElement ;
54
+ const wc = el . querySelector ( '#wc' ) ;
55
+ return { wc, fixture }
56
+ }
50
57
51
- describe ( "no children" , function ( ) {
52
- it ( "can display a Custom Element with no children" , function ( ) {
53
- this . weight = 3 ;
54
- let fixture = TestBed . createComponent ( ComponentWithoutChildren ) ;
58
+ tests ( {
59
+ renderComponentWithoutChildren ( ) {
60
+ return render ( ComponentWithoutChildren ) ;
61
+ } ,
62
+ renderComponentWithChildren ( ) {
63
+ return render ( ComponentWithChildren ) ;
64
+ } ,
65
+ async renderComponentWithChildrenRerender ( ) {
66
+ const results = render ( ComponentWithChildrenRerender ) ;
67
+ await new Promise ( ( r ) => setTimeout ( r , 1000 ) ) ;
68
+ results . fixture . detectChanges ( ) ;
69
+ return results ;
70
+ } ,
71
+ renderComponentWithDifferentViews ( ) {
72
+ const { wc, fixture } = render ( ComponentWithDifferentViews ) ;
73
+ function toggle ( ) {
74
+ fixture . componentInstance . toggle ( ) ;
55
75
fixture . detectChanges ( ) ;
56
- let el = fixture . debugElement . nativeElement ;
57
- let wc = el . querySelector ( "ce-without-children" ) ;
58
- expect ( wc ) . to . exist ;
59
- } ) ;
60
- } ) ;
61
-
62
- describe ( "with children" , function ( ) {
63
- function expectHasChildren ( wc ) {
64
- expect ( wc ) . to . exist ;
65
- let shadowRoot = wc . shadowRoot ;
66
- let heading = shadowRoot . querySelector ( "h1" ) ;
67
- expect ( heading ) . to . exist ;
68
- expect ( heading . textContent ) . to . eql ( "Test h1" ) ;
69
- let paragraph = shadowRoot . querySelector ( "p" ) ;
70
- expect ( paragraph ) . to . exist ;
71
- expect ( paragraph . textContent ) . to . eql ( "Test p" ) ;
72
76
}
73
-
74
- it ( "can display a Custom Element with children in a Shadow Root" , function ( ) {
75
- this . weight = 3 ;
76
- let fixture = TestBed . createComponent ( ComponentWithChildren ) ;
77
- fixture . detectChanges ( ) ;
78
- let root = fixture . debugElement . nativeElement ;
79
- let wc = root . querySelector ( "#wc" ) ;
80
- expectHasChildren ( wc ) ;
81
- } ) ;
82
-
83
- it ( "can display a Custom Element with children in a Shadow Root and pass in Light DOM children" , function (
84
- done
85
- ) {
86
- this . weight = 3 ;
87
- let fixture = TestBed . createComponent ( ComponentWithChildrenRerender ) ;
88
- fixture . detectChanges ( ) ;
89
- setTimeout ( function ( ) {
90
- fixture . detectChanges ( ) ;
91
- let root = fixture . debugElement . nativeElement ;
92
- let wc = root . querySelector ( "#wc" ) ;
93
- expectHasChildren ( wc ) ;
94
- expect ( wc . textContent . includes ( "2" ) ) . to . be . true ;
95
- done ( ) ;
96
- } , 1000 ) ;
97
- } ) ;
98
-
99
- it ( "can display a Custom Element with children in a Shadow Root and handle hiding and showing the element" , function ( ) {
100
- this . weight = 3 ;
101
- let fixture = TestBed . createComponent ( ComponentWithDifferentViews ) ;
102
- fixture . detectChanges ( ) ;
103
- let component = fixture . componentInstance ;
104
- let root = fixture . debugElement . nativeElement ;
105
- let wc = root . querySelector ( "#wc" ) ;
106
- expectHasChildren ( wc ) ;
107
- component . toggle ( ) ;
108
- fixture . detectChanges ( ) ;
109
- let dummy = root . querySelector ( "#dummy" ) ;
110
- expect ( dummy ) . to . exist ;
111
- expect ( dummy . textContent ) . to . eql ( "Dummy view" ) ;
112
- component . toggle ( ) ;
113
- fixture . detectChanges ( ) ;
114
- wc = root . querySelector ( "#wc" ) ;
115
- expectHasChildren ( wc ) ;
116
- } ) ;
117
- } ) ;
118
-
119
- describe ( "attributes and properties" , function ( ) {
120
- it ( "will pass boolean data as either an attribute or a property" , function ( ) {
121
- this . weight = 3 ;
122
- let fixture = TestBed . createComponent ( ComponentWithProperties ) ;
123
- fixture . detectChanges ( ) ;
124
- let root = fixture . debugElement . nativeElement ;
125
- let wc = root . querySelector ( "#wc" ) ;
126
- let data = wc . bool || wc . hasAttribute ( "bool" ) ;
127
- expect ( data ) . to . be . true ;
128
- } ) ;
129
-
130
- it ( "will pass numeric data as either an attribute or a property" , function ( ) {
131
- this . weight = 3 ;
132
- let fixture = TestBed . createComponent ( ComponentWithProperties ) ;
133
- fixture . detectChanges ( ) ;
134
- let root = fixture . debugElement . nativeElement ;
135
- let wc = root . querySelector ( "#wc" ) ;
136
- let data = wc . num || wc . getAttribute ( "num" ) ;
137
- expect ( parseInt ( data , 10 ) ) . to . eql ( 42 ) ;
138
- } ) ;
139
-
140
- it ( "will pass string data as either an attribute or a property" , function ( ) {
141
- this . weight = 3 ;
142
- let fixture = TestBed . createComponent ( ComponentWithProperties ) ;
143
- fixture . detectChanges ( ) ;
144
- let root = fixture . debugElement . nativeElement ;
145
- let wc = root . querySelector ( "#wc" ) ;
146
- let data = wc . str || wc . getAttribute ( "str" ) ;
147
- expect ( data ) . to . eql ( "Angular" ) ;
148
- } ) ;
149
- } ) ;
150
-
151
- describe ( "events" , function ( ) {
152
- it ( "can imperatively listen to a DOM event dispatched by a Custom Element" , function ( ) {
153
- this . weight = 3 ;
154
- let fixture = TestBed . createComponent ( ComponentWithImperativeEvent ) ;
155
- fixture . detectChanges ( ) ;
156
- let root = fixture . debugElement . nativeElement ;
157
- let wc = root . querySelector ( "#wc" ) ;
158
- let handled = root . querySelector ( "#handled" ) ;
159
- expect ( handled . textContent ) . to . eql ( "false" ) ;
77
+ return { wc, fixture, toggle }
78
+ } ,
79
+ renderComponentWithProperties ( ) {
80
+ return render ( ComponentWithProperties ) ;
81
+ } ,
82
+ renderComponentWithImperativeEvent ( ) {
83
+ const { wc, fixture } = render ( ComponentWithImperativeEvent ) ;
84
+ function click ( ) {
160
85
wc . click ( ) ;
161
86
fixture . detectChanges ( ) ;
162
- expect ( handled . textContent ) . to . eql ( "true" ) ;
163
- } ) ;
164
- } ) ;
165
-
87
+ }
88
+ return { wc, fixture, click } ;
89
+ }
166
90
} ) ;
0 commit comments