1
1
//make sure you import mocha-config before angular2/core
2
2
import { assert } from "./test-config" ;
3
- import { bootstrap } from "../nativescript-angular/application" ;
4
3
import {
5
- Type ,
6
4
Component ,
7
- ComponentRef ,
8
- DynamicComponentLoader ,
9
- ViewChild ,
10
5
ElementRef ,
11
- provide ,
12
- ApplicationRef
13
6
} from "angular2/core" ;
14
- import { View } from "ui/core/view" ;
15
- import * as background from "ui/styling/background" ;
16
- import { StackLayout } from "ui/layouts/stack-layout" ;
17
- import { GridLayout } from "ui/layouts/grid-layout" ;
18
- import { LayoutBase } from "ui/layouts/layout-base" ;
19
7
import { ProxyViewContainer } from "ui/proxy-view-container" ;
20
- import { topmost } from 'ui/frame' ;
21
- import { APP_ROOT_VIEW } from "../nativescript-angular/platform-providers" ;
22
8
import { Red } from "color/known-colors" ;
23
-
24
- @Component ( {
25
- selector : 'my-app' ,
26
- template : `<StackLayout #loadSite></StackLayout>`
27
- } )
28
- export class App {
29
- @ViewChild ( "loadSite" ) public loadSiteRef : ElementRef ;
30
-
31
- constructor ( public loader : DynamicComponentLoader ,
32
- public elementRef : ElementRef ,
33
- public appRef : ApplicationRef ) {
34
- }
35
- }
9
+ import { dumpView } from "./test-utils" ;
10
+ import { TestApp } from "./test-app" ;
36
11
37
12
@Component ( {
38
13
template : `<StackLayout><Label text="Layout"></Label></StackLayout>`
@@ -89,72 +64,66 @@ export class StyledLabelCmp {
89
64
}
90
65
91
66
@Component ( {
92
- selector : "conditional -label" ,
67
+ selector : "ng-if -label" ,
93
68
template : `<Label *ngIf="show" text="iffed"></Label>`
94
69
} )
95
- export class ConditionalLabel {
70
+ export class NgIfLabel {
96
71
public show : boolean = false ;
97
72
constructor ( public elementRef : ElementRef ) {
98
73
}
99
74
}
100
75
76
+ @Component ( {
77
+ selector : "ng-for-label" ,
78
+ template : `<Label *ngFor="#item of items" [text]="item"></Label>`
79
+ } )
80
+ export class NgForLabel {
81
+ public items : Array < string > = [ "one" , "two" , "three" ] ;
82
+ constructor ( public elementRef : ElementRef ) {
83
+ }
84
+ }
85
+
101
86
102
87
describe ( 'Renderer E2E' , ( ) => {
103
- let appComponent : App = null ;
104
- let _pendingDispose : ComponentRef [ ] = [ ] ;
105
- let appRef : ApplicationRef = null ;
106
-
107
- function loadComponent ( type : Type ) : Promise < ComponentRef > {
108
- return appComponent . loader . loadIntoLocation ( type , appComponent . elementRef , "loadSite" ) . then ( ( componentRef ) => {
109
- _pendingDispose . push ( componentRef ) ;
110
- return componentRef ;
111
- } ) ;
112
- }
88
+ let testApp : TestApp = null ;
113
89
114
- afterEach ( ( ) => {
115
- while ( _pendingDispose . length > 0 ) {
116
- const componentRef = _pendingDispose . pop ( )
117
- componentRef . dispose ( ) ;
118
- }
90
+ before ( ( ) => {
91
+ return TestApp . create ( ) . then ( ( app ) => {
92
+ testApp = app ;
93
+ } )
119
94
} ) ;
120
95
121
- before ( ( ) => {
122
- //bootstrap the app in a custom location
123
- const page = topmost ( ) . currentPage ;
124
- const rootLayout = < LayoutBase > page . content ;
125
- const viewRoot = new StackLayout ( ) ;
126
- rootLayout . addChild ( viewRoot ) ;
127
- GridLayout . setRow ( rootLayout , 50 ) ;
128
- const rootViewProvider = provide ( APP_ROOT_VIEW , { useFactory : ( ) => viewRoot } ) ;
129
- return bootstrap ( App , [ rootViewProvider ] ) . then ( ( componentRef ) => {
130
- appComponent = componentRef . instance ;
131
- appRef = appComponent . appRef ;
132
- } ) ;
96
+ after ( ( ) => {
97
+ testApp . dispose ( ) ;
98
+ } ) ;
99
+
100
+ afterEach ( ( ) => {
101
+ testApp . disposeComponenets ( ) ;
133
102
} ) ;
134
103
135
104
it ( "component with a layout" , ( ) => {
136
- return loadComponent ( LayoutWithLabel ) . then ( ( componentRef ) => {
105
+ return testApp . loadComponent ( LayoutWithLabel ) . then ( ( componentRef ) => {
137
106
const componentRoot = componentRef . instance . elementRef . nativeElement ;
138
107
assert . equal ( "(ProxyViewContainer (StackLayout (Label)))" , dumpView ( componentRoot ) ) ;
139
108
} ) ;
140
109
} ) ;
141
110
142
111
it ( "component without a layout" , ( ) => {
143
- return loadComponent ( LabelContainer ) . then ( ( componentRef ) => {
112
+ return testApp . loadComponent ( LabelContainer ) . then ( ( componentRef ) => {
144
113
const componentRoot = componentRef . instance . elementRef . nativeElement ;
145
114
assert . equal ( "(ProxyViewContainer (GridLayout (ProxyViewContainer (Label))))" , dumpView ( componentRoot ) ) ;
146
115
} ) ;
147
116
} ) ;
148
117
149
118
it ( "projects content into components" , ( ) => {
150
- return loadComponent ( ProjectionContainer ) . then ( ( componentRef ) => {
119
+ return testApp . loadComponent ( ProjectionContainer ) . then ( ( componentRef ) => {
151
120
const componentRoot = componentRef . instance . elementRef . nativeElement ;
152
121
assert . equal ( "(ProxyViewContainer (GridLayout (ProxyViewContainer (StackLayout (Button)))))" , dumpView ( componentRoot ) ) ;
153
122
} ) ;
154
123
} ) ;
155
124
156
125
it ( "applies component styles" , ( ) => {
157
- return loadComponent ( StyledLabelCmp ) . then ( ( componentRef ) => {
126
+ return testApp . loadComponent ( StyledLabelCmp ) . then ( ( componentRef ) => {
158
127
const componentRoot = componentRef . instance . elementRef . nativeElement ;
159
128
const label = ( < ProxyViewContainer > componentRoot ) . getChildAt ( 0 ) ;
160
129
assert . equal ( Red , label . style . color . hex ) ;
@@ -163,44 +132,52 @@ describe('Renderer E2E', () => {
163
132
164
133
describe ( "Structural directives" , ( ) => {
165
134
it ( "ngIf hides component when false" , ( ) => {
166
- return loadComponent ( ConditionalLabel ) . then ( ( componentRef ) => {
135
+ return testApp . loadComponent ( NgIfLabel ) . then ( ( componentRef ) => {
167
136
const componentRoot = componentRef . instance . elementRef . nativeElement ;
168
137
assert . equal ( "(ProxyViewContainer (template))" , dumpView ( componentRoot ) ) ;
169
138
} ) ;
170
139
} ) ;
171
140
172
141
it ( "ngIf show component when true" , ( ) => {
173
- return loadComponent ( ConditionalLabel ) . then ( ( componentRef ) => {
174
- const component = < ConditionalLabel > componentRef . instance ;
142
+ return testApp . loadComponent ( NgIfLabel ) . then ( ( componentRef ) => {
143
+ const component = < NgIfLabel > componentRef . instance ;
175
144
const componentRoot = component . elementRef . nativeElement ;
176
145
177
146
component . show = true ;
178
- appRef . tick ( ) ;
147
+ testApp . appRef . tick ( ) ;
179
148
assert . equal ( "(ProxyViewContainer (template), (Label))" , dumpView ( componentRoot ) ) ;
180
149
} ) ;
181
150
} )
182
- } )
183
151
184
- } ) ;
152
+ it ( "ngFor creates element for each item" , ( ) => {
153
+ return testApp . loadComponent ( NgForLabel ) . then ( ( componentRef ) => {
154
+ const componentRoot = componentRef . instance . elementRef . nativeElement ;
155
+ assert . equal ( "(ProxyViewContainer (template), (Label[text=one]), (Label[text=two]), (Label[text=three]))" , dumpView ( componentRoot , true ) ) ;
156
+ } ) ;
157
+ } ) ;
185
158
186
- function dumpView ( view : View ) : string {
187
- let nodeName = ( < any > view ) . nodeName
188
- if ( ! nodeName ) {
189
- nodeName = ( < any > view . constructor ) . name + '!' ;
190
- }
191
- let output = [ "(" , nodeName , " " ] ;
192
- ( < any > view ) . _eachChildView ( ( child ) => {
193
- const childDump = dumpView ( child ) ;
194
- output . push ( childDump ) ;
195
- output . push ( ", " ) ;
196
- return true ;
197
- } ) ;
198
- if ( output [ output . length - 1 ] == ", " ) {
199
- output . pop ( ) ;
200
- }
201
- if ( output [ output . length - 1 ] == " " ) {
202
- output . pop ( ) ;
203
- }
204
- output . push ( ")" ) ;
205
- return output . join ( "" ) ;
206
- }
159
+ it ( "ngFor updates when item is removed" , ( ) => {
160
+ return testApp . loadComponent ( NgForLabel ) . then ( ( componentRef ) => {
161
+ const component = < NgForLabel > componentRef . instance ;
162
+ const componentRoot = component . elementRef . nativeElement ;
163
+
164
+ component . items . splice ( 1 , 1 ) ;
165
+ testApp . appRef . tick ( ) ;
166
+
167
+ assert . equal ( "(ProxyViewContainer (template), (Label[text=one]), (Label[text=three]))" , dumpView ( componentRoot , true ) ) ;
168
+ } ) ;
169
+ } ) ;
170
+
171
+ it ( "ngFor updates when item is inserted" , ( ) => {
172
+ return testApp . loadComponent ( NgForLabel ) . then ( ( componentRef ) => {
173
+ const component = < NgForLabel > componentRef . instance ;
174
+ const componentRoot = component . elementRef . nativeElement ;
175
+
176
+ component . items . splice ( 1 , 0 , "new" ) ;
177
+ testApp . appRef . tick ( ) ;
178
+
179
+ assert . equal ( "(ProxyViewContainer (template), (Label[text=one]), (Label[text=new]), (Label[text=two]), (Label[text=three]))" , dumpView ( componentRoot , true ) ) ;
180
+ } ) ;
181
+ } ) ;
182
+ } )
183
+ } )
0 commit comments