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
6
} from "angular2/core" ;
13
- import { View } from "ui/core/view" ;
14
- import * as background from "ui/styling/background" ;
15
- import { StackLayout } from "ui/layouts/stack-layout" ;
16
- import { GridLayout } from "ui/layouts/grid-layout" ;
17
- import { LayoutBase } from "ui/layouts/layout-base" ;
18
7
import { ProxyViewContainer } from "ui/proxy-view-container" ;
19
- import { topmost } from 'ui/frame' ;
20
- import { APP_ROOT_VIEW } from "../nativescript-angular/platform-providers" ;
21
8
import { Red } from "color/known-colors" ;
22
-
23
- @Component ( {
24
- selector : 'my-app' ,
25
- template : `<StackLayout #loadSite></StackLayout>`
26
- } )
27
- export class App {
28
- @ViewChild ( "loadSite" ) public loadSiteRef : ElementRef ;
29
-
30
- constructor ( public loader : DynamicComponentLoader ,
31
- public elementRef : ElementRef ) {
32
- }
33
- }
9
+ import { dumpView } from "./test-utils" ;
10
+ import { TestApp } from "./test-app" ;
34
11
35
12
@Component ( {
36
13
template : `<StackLayout><Label text="Layout"></Label></StackLayout>`
37
14
} )
38
15
export class LayoutWithLabel {
39
- constructor ( public elementRef : ElementRef ) { }
16
+ constructor ( public elementRef : ElementRef ) { }
40
17
}
41
18
42
19
@Component ( {
43
20
selector : "label-cmp" ,
44
21
template : `<Label text="Layout"></Label>`
45
22
} )
46
23
export class LabelCmp {
47
- constructor ( public elementRef : ElementRef ) {
24
+ constructor ( public elementRef : ElementRef ) {
48
25
}
49
26
}
50
27
@@ -53,15 +30,15 @@ export class LabelCmp {
53
30
template : `<GridLayout><label-cmp></label-cmp></GridLayout>`
54
31
} )
55
32
export class LabelContainer {
56
- constructor ( public elementRef : ElementRef ) { }
33
+ constructor ( public elementRef : ElementRef ) { }
57
34
}
58
35
59
36
@Component ( {
60
37
selector : "projectable-cmp" ,
61
38
template : `<StackLayout><ng-content></ng-content></StackLayout>`
62
39
} )
63
40
export class ProjectableCmp {
64
- constructor ( public elementRef : ElementRef ) {
41
+ constructor ( public elementRef : ElementRef ) {
65
42
}
66
43
}
67
44
@Component ( {
@@ -71,7 +48,7 @@ export class ProjectableCmp {
71
48
</GridLayout>`
72
49
} )
73
50
export class ProjectionContainer {
74
- constructor ( public elementRef : ElementRef ) { }
51
+ constructor ( public elementRef : ElementRef ) { }
75
52
}
76
53
77
54
@Component ( {
@@ -82,90 +59,125 @@ export class ProjectionContainer {
82
59
template : `<Label text="Styled!"></Label>`
83
60
} )
84
61
export class StyledLabelCmp {
85
- constructor ( public elementRef : ElementRef ) {
62
+ constructor ( public elementRef : ElementRef ) {
86
63
}
87
64
}
88
65
89
- describe ( 'Renderer E2E' , ( ) => {
90
- let appComponent : App = null ;
91
- let _pendingDispose : ComponentRef [ ] = [ ] ;
66
+ @Component ( {
67
+ selector : "ng-if-label" ,
68
+ template : `<Label *ngIf="show" text="iffed"></Label>`
69
+ } )
70
+ export class NgIfLabel {
71
+ public show : boolean = false ;
72
+ constructor ( public elementRef : ElementRef ) {
73
+ }
74
+ }
92
75
93
- function loadComponent ( type : Type ) : Promise < ComponentRef > {
94
- return appComponent . loader . loadIntoLocation ( type , appComponent . elementRef , "loadSite" ) . then ( ( componentRef ) => {
95
- _pendingDispose . push ( componentRef ) ;
96
- return componentRef ;
97
- } ) ;
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 ) {
98
83
}
84
+ }
99
85
100
- afterEach ( ( ) => {
101
- while ( _pendingDispose . length > 0 ) {
102
- const componentRef = _pendingDispose . pop ( )
103
- componentRef . dispose ( ) ;
104
- }
105
- } ) ;
86
+
87
+ describe ( 'Renderer E2E' , ( ) => {
88
+ let testApp : TestApp = null ;
106
89
107
90
before ( ( ) => {
108
- //bootstrap the app in a custom location
109
- const page = topmost ( ) . currentPage ;
110
- const rootLayout = < LayoutBase > page . content ;
111
- const viewRoot = new StackLayout ( ) ;
112
- rootLayout . addChild ( viewRoot ) ;
113
- GridLayout . setRow ( rootLayout , 50 ) ;
114
- const rootViewProvider = provide ( APP_ROOT_VIEW , { useFactory : ( ) => viewRoot } ) ;
115
- return bootstrap ( App , [ rootViewProvider ] ) . then ( ( componentRef ) => {
116
- appComponent = componentRef . instance ;
117
- } ) ;
91
+ return TestApp . create ( ) . then ( ( app ) => {
92
+ testApp = app ;
93
+ } )
94
+ } ) ;
95
+
96
+ after ( ( ) => {
97
+ testApp . dispose ( ) ;
98
+ } ) ;
99
+
100
+ afterEach ( ( ) => {
101
+ testApp . disposeComponenets ( ) ;
118
102
} ) ;
119
103
120
104
it ( "component with a layout" , ( ) => {
121
- return loadComponent ( LayoutWithLabel ) . then ( ( componentRef ) => {
105
+ return testApp . loadComponent ( LayoutWithLabel ) . then ( ( componentRef ) => {
122
106
const componentRoot = componentRef . instance . elementRef . nativeElement ;
123
107
assert . equal ( "(ProxyViewContainer (StackLayout (Label)))" , dumpView ( componentRoot ) ) ;
124
108
} ) ;
125
109
} ) ;
126
110
127
111
it ( "component without a layout" , ( ) => {
128
- return loadComponent ( LabelContainer ) . then ( ( componentRef ) => {
112
+ return testApp . loadComponent ( LabelContainer ) . then ( ( componentRef ) => {
129
113
const componentRoot = componentRef . instance . elementRef . nativeElement ;
130
114
assert . equal ( "(ProxyViewContainer (GridLayout (ProxyViewContainer (Label))))" , dumpView ( componentRoot ) ) ;
131
115
} ) ;
132
116
} ) ;
133
117
134
118
it ( "projects content into components" , ( ) => {
135
- return loadComponent ( ProjectionContainer ) . then ( ( componentRef ) => {
119
+ return testApp . loadComponent ( ProjectionContainer ) . then ( ( componentRef ) => {
136
120
const componentRoot = componentRef . instance . elementRef . nativeElement ;
137
121
assert . equal ( "(ProxyViewContainer (GridLayout (ProxyViewContainer (StackLayout (Button)))))" , dumpView ( componentRoot ) ) ;
138
122
} ) ;
139
123
} ) ;
140
124
141
125
it ( "applies component styles" , ( ) => {
142
- return loadComponent ( StyledLabelCmp ) . then ( ( componentRef ) => {
126
+ return testApp . loadComponent ( StyledLabelCmp ) . then ( ( componentRef ) => {
143
127
const componentRoot = componentRef . instance . elementRef . nativeElement ;
144
128
const label = ( < ProxyViewContainer > componentRoot ) . getChildAt ( 0 ) ;
145
129
assert . equal ( Red , label . style . color . hex ) ;
146
130
} ) ;
147
131
} ) ;
148
132
149
- } ) ;
133
+ describe ( "Structural directives" , ( ) => {
134
+ it ( "ngIf hides component when false" , ( ) => {
135
+ return testApp . loadComponent ( NgIfLabel ) . then ( ( componentRef ) => {
136
+ const componentRoot = componentRef . instance . elementRef . nativeElement ;
137
+ assert . equal ( "(ProxyViewContainer (template))" , dumpView ( componentRoot ) ) ;
138
+ } ) ;
139
+ } ) ;
150
140
151
- function dumpView ( view : View ) : string {
152
- let nodeName = ( < any > view ) . nodeName
153
- if ( ! nodeName ) {
154
- nodeName = ( < any > view . constructor ) . name + '!' ;
155
- }
156
- let output = [ "(" , nodeName , " " ] ;
157
- ( < any > view ) . _eachChildView ( ( child ) => {
158
- const childDump = dumpView ( child ) ;
159
- output . push ( childDump ) ;
160
- output . push ( ", " ) ;
161
- return true ;
162
- } ) ;
163
- if ( output [ output . length - 1 ] == ", " ) {
164
- output . pop ( ) ;
165
- }
166
- if ( output [ output . length - 1 ] == " " ) {
167
- output . pop ( ) ;
168
- }
169
- output . push ( ")" ) ;
170
- return output . join ( "" ) ;
171
- }
141
+ it ( "ngIf show component when true" , ( ) => {
142
+ return testApp . loadComponent ( NgIfLabel ) . then ( ( componentRef ) => {
143
+ const component = < NgIfLabel > componentRef . instance ;
144
+ const componentRoot = component . elementRef . nativeElement ;
145
+
146
+ component . show = true ;
147
+ testApp . appRef . tick ( ) ;
148
+ assert . equal ( "(ProxyViewContainer (template), (Label))" , dumpView ( componentRoot ) ) ;
149
+ } ) ;
150
+ } )
151
+
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
+ } ) ;
158
+
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