-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.tests.js
130 lines (100 loc) · 4.55 KB
/
app.tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
describe('ToolsCtrl', function(){
// we'll use this scope in our tests
var $httpBackend, $rootScope, createController;
// mock Application to allow us to inject our own dependencies
beforeEach(angular.mock.module('pivotApp'));
beforeEach(inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
$httpBackend.when('GET', '/api/tools')
.respond({ "tooldesc" : "this is cloud manifest",
"toolname" : "Cloud Manifest",
"toolurl" : "cloudmanifest.com" });
// Get hold of a scope (i.e. the root scope)
$rootScope = $injector.get('$rootScope');
// The $controller service is used to create instances of controllers
var $controller = $injector.get('$controller');
createController = function() {
return $controller('ToolsCtrl', {'$scope' : $rootScope });
};
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
// tests start here //
// test initial setting of tools var
it('should have tools from api', function(){
var controller = createController();
$httpBackend.flush();
expect($rootScope.tools).toEqual({ "tooldesc" : "this is cloud manifest", "toolname" : "Cloud Manifest", "toolurl" : "cloudmanifest.com" });
expect($rootScope.tools.toolname).toBe("Cloud Manifest");
});
// test addTool()
it('should send new tool to api', function() {
var controller = createController();
$httpBackend.flush();
$rootScope.newName = "new tool";
$rootScope.newUrl = "newtool.com";
$rootScope.newDesc = "new tool description";
$rootScope.current_id = "123456";
$httpBackend.expectPOST('/api/tools', {"toolname":$rootScope.newName, "toolurl":$rootScope.newUrl, "tooldesc":$rootScope.newDesc}).respond(200, '');
$rootScope.addTool();
$httpBackend.flush();
expect($rootScope.tools.toolname).toBe("Cloud Manifest");
expect($rootScope.valid).toBe(true);
});
// test deleteTool()
it('should delete specific tool', function() {
var controller = createController();
$httpBackend.flush();
$httpBackend.expectDELETE('/api/tools/123456').respond(200, '')
$rootScope.deleteTool('123456');
$httpBackend.flush();
expect($rootScope.valid).toBe(true);
});
// test updateEditForm()
it('should update vars in the DOM', function() {
var controller = createController();
$httpBackend.flush();
$httpBackend.expectGET('/api/tools/123456').respond(200, {"toolname":"tool1", "toolurl":"tool1.com", "tooldesc":"tool1 description"});
$rootScope.updateEditForm('123456');
$httpBackend.flush();
expect($rootScope.newName).toBe('tool1');
expect($rootScope.newUrl).toBe('tool1.com');
expect($rootScope.newDesc).toBe('tool1 description');
expect($rootScope.current_id).toBe('123456');
});
// test updateTool()
it('should delete specific tool', function() {
var controller = createController();
$httpBackend.flush();
$rootScope.newName = "new tool";
$rootScope.newUrl = "newtool.com";
$rootScope.newDesc = "new tool description";
$rootScope.current_id = "123456";
$httpBackend.expectPUT('/api/tools/123456', {"toolname":$rootScope.newName, "toolurl":$rootScope.newUrl, "tooldesc":$rootScope.newDesc}).respond(200, '');
$rootScope.updateTool();
$httpBackend.flush();
expect($rootScope.valid).toBe(true);
});
// test clearToolForm()
it('should clear out the input values', function(){
var controller = createController();
$httpBackend.flush();
$rootScope.newName = "new tool";
$rootScope.newUrl = "newtool.com";
$rootScope.newDesc = "new tool description";
$rootScope.current_id = "12345";
expect($rootScope.newName).not.toBe("");
expect($rootScope.newUrl).not.toBe("");
expect($rootScope.newDesc).not.toBe("");
expect($rootScope.current_id).not.toBe("");
$rootScope.clearToolForm();
expect($rootScope.newName).toBe("");
expect($rootScope.newUrl).toBe("");
expect($rootScope.newDesc).toBe("");
expect($rootScope.current_id).toBe("");
});
});