1+ // modeled after https://github.com/angular/angular/blob/cee2318110eeea115e5f6fc5bfc814cbaa7d90d8/modules/angular2/test/common/directives/ng_for_spec.ts
2+ import { it , iit , describe , expect , inject , injectAsync , beforeEachProviders , fakeAsync , tick , TestComponentBuilder , AsyncTestCompleter } from 'angular2/testing_internal' ;
3+ import { Component , ViewChild , TemplateRef } from 'angular2/core' ;
4+ import * as Infragistics from '../../../src/igniteui.angular2' ;
5+
6+ export function main ( ) {
7+ describe ( 'Infragistics Angular2 Grid' , ( ) => {
8+ it ( 'should initialize correctly' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
9+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts"></ig-grid></div>' ;
10+ tcb . overrideTemplate ( TestComponent , template )
11+ . createAsync ( TestComponent )
12+ . then ( ( fixture ) => {
13+ fixture . detectChanges ( ) ;
14+ expect ( fixture . debugElement . componentInstance . viewChild ) . toBeAnInstanceOf ( Infragistics . IgGridComponent ) ;
15+ async . done ( ) ;
16+ } ) ;
17+ } ) ) ;
18+
19+ it ( 'should detect and apply changes from model' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
20+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts" [changeDetectionInterval]="cdi"></ig-grid></div>' ;
21+ tcb . overrideTemplate ( TestComponent , template )
22+ . createAsync ( TestComponent )
23+ . then ( ( fixture ) => {
24+ fixture . detectChanges ( ) ;
25+ fixture . componentInstance . data [ 0 ] . Name = "Mr. Smith" ;
26+ setTimeout ( ( ) => {
27+ fixture . detectChanges ( ) ;
28+ expect ( $ ( fixture . debugElement . nativeElement ) . find ( "#grid1 tr:first td[aria-describedby='grid1_Name']" ) . text ( ) )
29+ . toBe ( "Mr. Smith" ) ;
30+ async . done ( ) ;
31+ } , 10 ) ;
32+ } ) ;
33+ } ) ) ;
34+
35+ it ( 'should detect and apply deleting records from model' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
36+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts" [changeDetectionInterval]="cdi"></ig-grid></div>' ;
37+ tcb . overrideTemplate ( TestComponent , template )
38+ . createAsync ( TestComponent )
39+ . then ( ( fixture ) => {
40+ fixture . detectChanges ( ) ;
41+ fixture . componentInstance . data . splice ( 2 , 1 ) ;
42+ setTimeout ( ( ) => {
43+ fixture . detectChanges ( ) ;
44+ expect ( $ ( fixture . debugElement . nativeElement ) . find ( "#grid1 tbody tr" ) . length )
45+ . toBe ( 2 ) ;
46+ async . done ( ) ;
47+ } , 10 ) ;
48+ } ) ;
49+ } ) ) ;
50+
51+ it ( 'should detect and apply adding records from model' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
52+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts" [changeDetectionInterval]="cdi"></ig-grid></div>' ;
53+ tcb . overrideTemplate ( TestComponent , template )
54+ . createAsync ( TestComponent )
55+ . then ( ( fixture ) => {
56+ fixture . detectChanges ( ) ;
57+ fixture . componentInstance . data . push ( { "Id" : 4 , "Name" : "Bob Ferguson" , "Age" : 33 } ) ;
58+ setTimeout ( ( ) => {
59+ fixture . detectChanges ( ) ;
60+ expect ( $ ( fixture . debugElement . nativeElement ) . find ( "#grid1 tbody tr" ) . length )
61+ . toBe ( 4 ) ;
62+ async . done ( ) ;
63+ } , 10 ) ;
64+ } ) ;
65+ } ) ) ;
66+
67+ it ( 'should detect and apply changes to model' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
68+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts"></ig-grid></div>' ;
69+ tcb . overrideTemplate ( TestComponent , template )
70+ . createAsync ( TestComponent )
71+ . then ( ( fixture ) => {
72+ fixture . detectChanges ( ) ;
73+ $ ( fixture . debugElement . nativeElement ) . find ( "#grid1 tr[data-id='2'] td[aria-describedby='grid1_Name']" ) . click ( ) ;
74+ $ ( fixture . debugElement . nativeElement ) . find ( "#grid1" ) . igGridUpdating ( "setCellValue" , 2 , "Name" , "Mary Jackson" ) ;
75+ $ ( fixture . debugElement . nativeElement ) . find ( "#grid1_container #grid1_updating_done" ) . click ( ) ;
76+ expect ( fixture . debugElement . componentInstance . data [ 1 ] . Name )
77+ . toBe ( "Mary Jackson" ) ;
78+ async . done ( ) ;
79+ } ) ;
80+ } ) ) ;
81+
82+ it ( 'should detect and apply deleting records to model' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
83+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts"></ig-grid></div>' ;
84+ tcb . overrideTemplate ( TestComponent , template )
85+ . createAsync ( TestComponent )
86+ . then ( ( fixture ) => {
87+ fixture . detectChanges ( ) ;
88+ $ ( fixture . debugElement . nativeElement ) . find ( "#grid1" ) . igGridUpdating ( "deleteRow" , 2 ) ;
89+ expect ( fixture . debugElement . componentInstance . data . length )
90+ . toBe ( 2 ) ;
91+ async . done ( ) ;
92+ } ) ;
93+ } ) ) ;
94+
95+ it ( 'should detect and apply adding records to model' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
96+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts"></ig-grid></div>' ;
97+ tcb . overrideTemplate ( TestComponent , template )
98+ . createAsync ( TestComponent )
99+ . then ( ( fixture ) => {
100+ fixture . detectChanges ( ) ;
101+ $ ( fixture . debugElement . nativeElement ) . find ( "#grid1" ) . igGridUpdating ( "addRow" , { "Id" : 4 , "Name" : "Bob Ferguson" , "Age" : 33 } ) ;
102+ expect ( fixture . debugElement . componentInstance . data . length )
103+ . toBe ( 4 ) ;
104+ async . done ( ) ;
105+ } ) ;
106+ } ) ) ;
107+
108+ it ( 'should allow defining events' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
109+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts" (cellClick)="cellClickHandler($event)"></ig-grid></div>' ;
110+ tcb . overrideTemplate ( TestComponent , template )
111+ . createAsync ( TestComponent )
112+ . then ( ( fixture ) => {
113+ fixture . detectChanges ( ) ;
114+ $ ( fixture . debugElement . nativeElement ) . find ( "#grid1 tr[data-id='1'] td[aria-describedby='grid1_Name']" ) . click ( ) ;
115+ setTimeout ( ( ) => {
116+ expect ( fixture . debugElement . componentInstance . firedEvent . event . type )
117+ . toBe ( "iggridcellclick" ) ;
118+ expect ( fixture . debugElement . componentInstance . firedEvent . ui . colIndex )
119+ . toBe ( 1 ) ;
120+ expect ( fixture . debugElement . componentInstance . firedEvent . ui . colKey )
121+ . toBe ( "Name" ) ;
122+ async . done ( ) ;
123+ } , 50 ) ;
124+ } ) ;
125+ } ) ) ;
126+
127+ it ( 'should allow changing options' , injectAsync ( [ TestComponentBuilder , AsyncTestCompleter ] , ( tcb : TestComponentBuilder , async ) => {
128+ var template = '<div><ig-grid [(widgetId)]="gridID" [(options)]="opts1" [changeDetectionInterval]="cdi"></ig-grid></div>' ;
129+ tcb . overrideTemplate ( TestComponent , template )
130+ . createAsync ( TestComponent )
131+ . then ( ( fixture ) => {
132+ fixture . detectChanges ( ) ;
133+ fixture . componentInstance . opts1 . height = "400px" ;
134+ setTimeout ( ( ) => {
135+ fixture . detectChanges ( ) ;
136+ expect ( $ ( fixture . debugElement . nativeElement ) . find ( "#grid1_container" ) . outerHeight ( ) )
137+ . toBe ( 400 ) ;
138+ async . done ( ) ;
139+ } , 10 ) ;
140+ } ) ;
141+ } ) ) ;
142+ } ) ;
143+ }
144+
145+ @Component ( {
146+ selector : 'test-cmp' ,
147+ template : '<div></div>' , //"Component 'TestComponent' must have either 'template' or 'templateUrl' set."
148+ directives : [ Infragistics . IgGridComponent ]
149+ } )
150+ class TestComponent {
151+ private opts : any ;
152+ private opts1 : any ;
153+ private gridID : string ;
154+ private data : Array < any > ;
155+ private cdi : number ;
156+ private firedEvent : any ;
157+ @ViewChild ( Infragistics . IgGridComponent ) public viewChild : Infragistics . IgGridComponent ;
158+
159+ constructor ( ) {
160+ this . gridID = "grid1" ;
161+ this . cdi = 0 ;
162+ this . data = [
163+ { "Id" : 1 , "Name" : "John Smith" , "Age" : 45 } ,
164+ { "Id" : 2 , "Name" : "Mary Johnson" , "Age" : 32 } ,
165+ { "Id" : 3 , "Name" : "Bob Ferguson" , "Age" : 27 }
166+ ]
167+ this . opts = {
168+ primaryKey : "Id" ,
169+ dataSource : this . data ,
170+ autoCommit : true ,
171+ features : [
172+ { name : "Updating" }
173+ ]
174+ } ;
175+
176+ this . opts1 = {
177+ dataSource : this . data ,
178+ height : "300px"
179+ } ;
180+ }
181+
182+ public cellClickHandler ( evt ) {
183+ this . firedEvent = evt ;
184+ }
185+ }
0 commit comments