1+ describe ( 'angularjs-dynamic-form-test' , function ( ) {
2+
3+ var element ;
4+ var $scope ;
5+ var compile ;
6+ var scopeFields = [
7+ {
8+ caption : 'Name' ,
9+ model : 'name' ,
10+ type : 'string' ,
11+ maxLength : 25
12+ } ,
13+ {
14+ caption : 'Street Address' ,
15+ model : 'address.street' ,
16+ type : 'string' ,
17+ maxLength : 25
18+ } ,
19+ {
20+ caption : 'State' ,
21+ model : 'address.state' ,
22+ type : 'select' ,
23+ options : [
24+ {
25+ caption : 'California' ,
26+ value : 'CA'
27+ } ,
28+ {
29+ caption : 'New York' ,
30+ value : 'NY'
31+ } ,
32+ {
33+ caption : 'Washington' ,
34+ value : 'WA'
35+ }
36+ ]
37+ } ,
38+ ] ;
39+
40+ var scopeData = {
41+ name : 'John Smith' ,
42+ nicknames : [ 'bill' , 'dan' , 'grumpy' ] ,
43+ address : { }
44+ } ;
45+ var html = '<dynamic-table-form fields="fields" data="data">' +
46+ '<input dynamic-field-type="string" ng-model="value" type="text">' +
47+ '<select dynamic-field-type="select" ng-model="value" ng-options=' +
48+ '"option.value as option.caption for option in config.options"></select>' +
49+ '</dynamic-table-form>' ;
50+
51+
52+
53+ beforeEach ( module ( 'dynamic-form' ) ) ;
54+
55+ beforeEach ( inject ( function ( $compile , $rootScope ) {
56+ $scope = $rootScope . $new ( ) ;
57+ compile = $compile ;
58+ $scope . fields = [ ] ;
59+ $scope . data = { } ;
60+
61+ element = angular . element ( html ) ;
62+ } ) ) ;
63+
64+ it ( 'Should have added control groups for each element' , function ( ) {
65+ $scope . fields = [
66+ {
67+ caption : 'Name' ,
68+ model : 'name' ,
69+ type : 'string' ,
70+ } ,
71+ {
72+ caption : 'Street Address' ,
73+ model : 'address.street' ,
74+ type : 'string' ,
75+ } ,
76+ {
77+ caption : 'State' ,
78+ model : 'address.state' ,
79+ type : 'select' ,
80+ options : [
81+ {
82+ caption : 'California' ,
83+ value : 'CA'
84+ } ,
85+ {
86+ caption : 'New York' ,
87+ value : 'NY'
88+ } ,
89+ {
90+ caption : 'Washington' ,
91+ value : 'WA'
92+ }
93+ ]
94+ } ,
95+ ] ;
96+
97+ compile ( element ) ( $scope ) ;
98+ $scope . $digest ( ) ;
99+
100+ expect ( element . children ( ) . length ) . toBe ( scopeFields . length ) ;
101+ } ) ;
102+
103+ it ( 'Should display a string field as an input field' , function ( ) {
104+ $scope . fields = [
105+ { caption : 'Name' , model : 'name' , type : 'string' }
106+ ] ;
107+
108+ $scope . data = {
109+ name : 'John Smith'
110+ } ;
111+
112+ compile ( element ) ( $scope ) ;
113+ $scope . $digest ( ) ;
114+
115+ expect ( element . children ( ) . length ) . toBe ( 1 ) ;
116+ expect ( element . find ( 'label' ) . html ( ) ) . toBe ( 'Name' ) ;
117+ expect ( element . find ( 'input' ) . length ) . toBe ( 1 ) ;
118+ expect ( element . find ( 'input' ) . val ( ) ) . toBe ( 'John Smith' ) ;
119+ } ) ;
120+
121+ it ( 'Should display option fields as select with options' , function ( ) {
122+ $scope . fields = [
123+ {
124+ caption : 'State' ,
125+ model : 'address.state' ,
126+ type : 'select' ,
127+ options : [
128+ {
129+ caption : 'California' ,
130+ value : 'CA'
131+ } ,
132+ {
133+ caption : 'New York' ,
134+ value : 'NY'
135+ } ,
136+ {
137+ caption : 'Washington' ,
138+ value : 'WA'
139+ }
140+ ]
141+ }
142+ ] ;
143+ compile ( element ) ( $scope ) ;
144+ $scope . $digest ( ) ;
145+
146+ expect ( element . children ( ) . length ) . toBe ( 1 ) ;
147+ expect ( element . children ( ) . find ( 'select' ) . length ) . toBe ( 1 ) ;
148+ // 1 extra default option
149+ expect ( element . children ( ) . find ( 'option' ) . length ) . toBe ( 4 ) ;
150+ } ) ;
151+ } ) ;
0 commit comments