Skip to content

Commit fbe85fd

Browse files
author
Bryan Wyman
committed
Add some unit tests
1 parent dcffbd5 commit fbe85fd

File tree

4 files changed

+164
-6
lines changed

4 files changed

+164
-6
lines changed

Gruntfile.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ module.exports = function (grunt) {
33
'use strict';
44
grunt.initConfig({
55
connect: {
6-
options: {
7-
port: 9001,
8-
hostname: '0.0.0.0'
6+
server: {
7+
options: {
8+
port: 9001,
9+
base: '.',
10+
hostname: '0.0.0.0'
11+
}
912
}
1013
},
1114
karma: {
@@ -19,4 +22,4 @@ module.exports = function (grunt) {
1922
grunt.loadNpmTasks('grunt-contrib-connect');
2023
grunt.loadNpmTasks('grunt-karma');
2124
grunt.registerTask('test', ['karma']);
22-
};
25+
};

bower.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
"license": "MIT",
66
"dependencies": {
77
"angular": "1.2.7"
8+
},
9+
"devDependencies": {
10+
"angular-mocks": "1.2.7",
11+
"jasmine": "~1.3.1"
812
}
913
}

karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ module.exports = function(config) {
1111

1212
plugins: [
1313
'karma-phantomjs-launcher',
14+
'karma-chrome-launcher',
1415
'karma-jasmine'
1516
],
1617

1718
// list of files / patterns to load in the browser
1819
files: [
1920
'bower_components/angular/angular.js',
20-
//'bower_components/angular/angular-mocks.js',
21+
'bower_components/angular-mocks/angular-mocks.js',
2122
'src/*.js',
2223
'test/*.js'
2324
],
@@ -47,7 +48,6 @@ module.exports = function(config) {
4748
// - IE (only Windows)
4849
browsers: ['PhantomJS'],
4950

50-
5151
// Continuous Integration mode
5252
// if true, it capture browsers, run tests and exit
5353
singleRun: true

test/testDynamicForm.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)