forked from meanjs/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.articles.client.controller.tests.js
More file actions
180 lines (146 loc) · 6.29 KB
/
Copy pathadmin.articles.client.controller.tests.js
File metadata and controls
180 lines (146 loc) · 6.29 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(function () {
'use strict';
describe('Articles Admin Controller Tests', function () {
// Initialize global variables
var ArticlesAdminController,
$scope,
$httpBackend,
$state,
Authentication,
ArticlesService,
mockArticle,
Notification;
// The $resource service augments the response object with methods for updating and deleting the resource.
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
// the responses exactly. To solve the problem, we define a new toEqualData Jasmine matcher.
// When the toEqualData matcher compares two objects, it takes only object properties into
// account and ignores methods.
beforeEach(function () {
jasmine.addMatchers({
toEqualData: function (util, customEqualityTesters) {
return {
compare: function (actual, expected) {
return {
pass: angular.equals(actual, expected)
};
}
};
}
});
});
// Then we can start by loading the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
// This allows us to inject a service but then attach it to a variable
// with the same name as the service.
beforeEach(inject(function ($controller, $rootScope, _$state_, _$httpBackend_, _Authentication_, _ArticlesService_, _Notification_) {
// Set a new global scope
$scope = $rootScope.$new();
// Point global variables to injected services
$httpBackend = _$httpBackend_;
$state = _$state_;
Authentication = _Authentication_;
ArticlesService = _ArticlesService_;
Notification = _Notification_;
// Ignore parent template get on state transitions
$httpBackend.whenGET('/modules/core/client/views/home.client.view.html').respond(200, '');
// create mock article
mockArticle = new ArticlesService({
_id: '525a8422f6d0f87f0e407a33',
title: 'An Article about MEAN',
content: 'MEAN rocks!'
});
// Mock logged in user
Authentication.user = {
roles: ['user']
};
// Initialize the Articles controller.
ArticlesAdminController = $controller('ArticlesAdminController as vm', {
$scope: $scope,
articleResolve: {}
});
// Spy on state go
spyOn($state, 'go');
spyOn(Notification, 'error');
spyOn(Notification, 'success');
}));
describe('vm.save() as create', function () {
var sampleArticlePostData;
beforeEach(function () {
// Create a sample article object
sampleArticlePostData = new ArticlesService({
title: 'An Article about MEAN',
content: 'MEAN rocks!'
});
$scope.vm.article = sampleArticlePostData;
});
it('should send a POST request with the form input values and then locate to new object URL', inject(function (ArticlesService) {
// Set POST response
$httpBackend.expectPOST('/api/articles', sampleArticlePostData).respond(mockArticle);
// Run controller functionality
$scope.vm.save(true);
$httpBackend.flush();
// Test Notification success was called
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Article saved successfully!' });
// Test URL redirection after the article was created
expect($state.go).toHaveBeenCalledWith('admin.articles.list');
}));
it('should call Notification.error if error', function () {
var errorMessage = 'this is an error message';
$httpBackend.expectPOST('/api/articles', sampleArticlePostData).respond(400, {
message: errorMessage
});
$scope.vm.save(true);
$httpBackend.flush();
expect(Notification.error).toHaveBeenCalledWith({ message: errorMessage, title: '<i class="glyphicon glyphicon-remove"></i> Article save error!' });
});
});
describe('vm.save() as update', function () {
beforeEach(function () {
// Mock article in $scope
$scope.vm.article = mockArticle;
});
it('should update a valid article', inject(function (ArticlesService) {
// Set PUT response
$httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond();
// Run controller functionality
$scope.vm.save(true);
$httpBackend.flush();
// Test Notification success was called
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Article saved successfully!' });
// Test URL location to new object
expect($state.go).toHaveBeenCalledWith('admin.articles.list');
}));
it('should call Notification.error if error', inject(function (ArticlesService) {
var errorMessage = 'error';
$httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(400, {
message: errorMessage
});
$scope.vm.save(true);
$httpBackend.flush();
expect(Notification.error).toHaveBeenCalledWith({ message: errorMessage, title: '<i class="glyphicon glyphicon-remove"></i> Article save error!' });
}));
});
describe('vm.remove()', function () {
beforeEach(function () {
// Setup articles
$scope.vm.article = mockArticle;
});
it('should delete the article and redirect to articles', function () {
// Return true on confirm message
spyOn(window, 'confirm').and.returnValue(true);
$httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204);
$scope.vm.remove();
$httpBackend.flush();
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Article deleted successfully!' });
expect($state.go).toHaveBeenCalledWith('admin.articles.list');
});
it('should should not delete the article and not redirect', function () {
// Return false on confirm message
spyOn(window, 'confirm').and.returnValue(false);
$scope.vm.remove();
expect($state.go).not.toHaveBeenCalled();
});
});
});
}());