-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontroller-without-test-element.js
109 lines (90 loc) · 2.68 KB
/
controller-without-test-element.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
function someController(SomeService, Alerts) {
this.create = function(argument) {
SomeService.create(argument).then(function(response) {
Alerts.success('Created succesfully' + response.name)
}, function(error) {
Alerts.error('Error: ' + error.message)
});
}
}
someController.$inject = ['SomeService', 'Alerts'];
angular
.module('ctrlWithoutTE', [])
.controller('someController', someController);
describe('someController', function() {
var
someController, $controller, $rootScope, $scope,
mockedSomeService = {
create: angular.noop
},
mockedAlerts = {
success: angular.noop,
error: angular.noop
},
successCallback,
failCallback;
beforeEach(module('ctrlWithoutTE'));
beforeEach(function() {
//mocking promise
spyOn(mockedSomeService, 'create').and.returnValue({
then: function(success, fail) {
successCallback = success;
failCallback = fail;
}
});
spyOn(mockedAlerts, 'success');
spyOn(mockedAlerts, 'error');
});
beforeEach(function() {
inject(function(_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
$controller = _$controller_;
});
$scope = $rootScope.$new();
someController = $controller('someController', {
$scope: $scope,
SomeService: mockedSomeService,
Alerts: mockedAlerts
});
});
it('should not be null', function() {
expect(someController).toBeTruthy();
});
describe('create method', function() {
var someObject = {
some: 'property'
};
beforeEach(function() {
someController.create(someObject);
});
describe('create method', function() {
it('should call create method on SomeService with someObject', function() {
expect(mockedSomeService.create).toHaveBeenCalledWith(someObject);
});
describe('success', function() {
var response = {
name: 'response'
};
beforeEach(function() {
//calling first function in promise
successCallback(response);
});
it('should call success method on Alerts with proper message', function() {
expect(mockedAlerts.success).toHaveBeenCalledWith('Created succesfully' + response.name);
});
});
describe('failure', function() {
var error = {
message: 'error message'
};
beforeEach(function() {
//calling second function in promise
failCallback(error);
});
it('should call error method on Alerts with proper message', function() {
expect(mockedAlerts.error).toHaveBeenCalledWith('Error: ' + error.message);
});
});
});
});
});