Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 612a76c

Browse files
committed
Merge pull request #728 from cdriscol/angular_tests
Add client side tests Fixes #663
2 parents 99a8168 + d5ea5c9 commit 612a76c

File tree

9 files changed

+1277
-383
lines changed

9 files changed

+1277
-383
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public/lib/
1414
app/tests/coverage/
1515
.bower-*/
1616
.idea/
17+
coverage/
1718

1819
# MEAN.js app and assets
1920
# ======================
@@ -68,4 +69,3 @@ mongod
6869
*.ntvs*
6970
*.njsproj
7071
*.sln
71-

modules/articles/tests/client/articles.client.controller.tests.js

Lines changed: 106 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
$httpBackend,
1010
$stateParams,
1111
$location,
12-
Authentication;
12+
Authentication,
13+
Articles,
14+
mockArticle;
1315

1416
// The $resource service augments the response object with methods for updating and deleting the resource.
1517
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
@@ -36,7 +38,7 @@
3638
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
3739
// This allows us to inject a service but then attach it to a variable
3840
// with the same name as the service.
39-
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Authentication_) {
41+
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Authentication_, _Articles_) {
4042
// Set a new global scope
4143
scope = $rootScope.$new();
4244

@@ -45,6 +47,14 @@
4547
$httpBackend = _$httpBackend_;
4648
$location = _$location_;
4749
Authentication = _Authentication_;
50+
Articles = _Articles_;
51+
52+
// create mock article
53+
mockArticle = new Articles({
54+
_id: '525a8422f6d0f87f0e407a33',
55+
title: 'An Article about MEAN',
56+
content: 'MEAN rocks!'
57+
});
4858

4959
// Mock logged in user
5060
Authentication.user = {
@@ -58,14 +68,8 @@
5868
}));
5969

6070
it('$scope.find() should create an array with at least one article object fetched from XHR', inject(function(Articles) {
61-
// Create sample article using the Articles service
62-
var sampleArticle = new Articles({
63-
title: 'An Article about MEAN',
64-
content: 'MEAN rocks!'
65-
});
66-
6771
// Create a sample articles array that includes the new article
68-
var sampleArticles = [sampleArticle];
72+
var sampleArticles = [mockArticle];
6973

7074
// Set GET response
7175
$httpBackend.expectGET('api/articles').respond(sampleArticles);
@@ -79,99 +83,128 @@
7983
}));
8084

8185
it('$scope.findOne() should create an array with one article object fetched from XHR using a articleId URL parameter', inject(function(Articles) {
82-
// Define a sample article object
83-
var sampleArticle = new Articles({
84-
title: 'An Article about MEAN',
85-
content: 'MEAN rocks!'
86-
});
87-
8886
// Set the URL parameter
89-
$stateParams.articleId = '525a8422f6d0f87f0e407a33';
87+
$stateParams.articleId = mockArticle._id;
9088

9189
// Set GET response
92-
$httpBackend.expectGET(/api\/articles\/([0-9a-fA-F]{24})$/).respond(sampleArticle);
90+
$httpBackend.expectGET(/api\/articles\/([0-9a-fA-F]{24})$/).respond(mockArticle);
9391

9492
// Run controller functionality
9593
scope.findOne();
9694
$httpBackend.flush();
9795

9896
// Test scope value
99-
expect(scope.article).toEqualData(sampleArticle);
97+
expect(scope.article).toEqualData(mockArticle);
10098
}));
10199

102-
it('$scope.create() with valid form data should send a POST request with the form input values and then locate to new object URL', inject(function(Articles) {
103-
// Create a sample article object
104-
var sampleArticlePostData = new Articles({
105-
title: 'An Article about MEAN',
106-
content: 'MEAN rocks!'
107-
});
100+
describe('$scope.craete()', function() {
101+
var sampleArticlePostData;
108102

109-
// Create a sample article response
110-
var sampleArticleResponse = new Articles({
111-
_id: '525cf20451979dea2c000001',
112-
title: 'An Article about MEAN',
113-
content: 'MEAN rocks!'
103+
beforeEach(function() {
104+
// Create a sample article object
105+
sampleArticlePostData = new Articles({
106+
title: 'An Article about MEAN',
107+
content: 'MEAN rocks!'
108+
});
109+
110+
// Fixture mock form input values
111+
scope.title = 'An Article about MEAN';
112+
scope.content = 'MEAN rocks!';
113+
114+
spyOn($location, 'path');
114115
});
115116

116-
// Fixture mock form input values
117-
scope.title = 'An Article about MEAN';
118-
scope.content = 'MEAN rocks!';
117+
it('should send a POST request with the form input values and then locate to new object URL', inject(function(Articles) {
118+
// Set POST response
119+
$httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(mockArticle);
119120

120-
// Set POST response
121-
$httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(sampleArticleResponse);
121+
// Run controller functionality
122+
scope.create();
123+
$httpBackend.flush();
122124

123-
// Run controller functionality
124-
scope.create();
125-
$httpBackend.flush();
125+
// Test form inputs are reset
126+
expect(scope.title).toEqual('');
127+
expect(scope.content).toEqual('');
126128

127-
// Test form inputs are reset
128-
expect(scope.title).toEqual('');
129-
expect(scope.content).toEqual('');
129+
// Test URL redirection after the article was created
130+
expect($location.path.calls.mostRecent().args[0]).toBe('articles/' + mockArticle._id);
131+
}));
130132

131-
// Test URL redirection after the article was created
132-
expect($location.path()).toBe('/articles/' + sampleArticleResponse._id);
133-
}));
133+
it('should set scope.error if save error', function() {
134+
var errorMessage = 'this is an error message';
135+
$httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(400, {
136+
message: errorMessage
137+
});
134138

135-
it('$scope.update() should update a valid article', inject(function(Articles) {
136-
// Define a sample article put data
137-
var sampleArticlePutData = new Articles({
138-
_id: '525cf20451979dea2c000001',
139-
title: 'An Article about MEAN',
140-
content: 'MEAN Rocks!'
139+
scope.create();
140+
$httpBackend.flush();
141+
142+
expect(scope.error).toBe(errorMessage);
141143
});
144+
});
142145

143-
// Mock article in scope
144-
scope.article = sampleArticlePutData;
146+
describe('$scope.update()', function() {
147+
beforeEach(function() {
148+
// Mock article in scope
149+
scope.article = mockArticle;
150+
});
145151

146-
// Set PUT response
147-
$httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond();
152+
it('should update a valid article', inject(function(Articles) {
153+
// Set PUT response
154+
$httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond();
148155

149-
// Run controller functionality
150-
scope.update();
151-
$httpBackend.flush();
156+
// Run controller functionality
157+
scope.update();
158+
$httpBackend.flush();
152159

153-
// Test URL location to new object
154-
expect($location.path()).toBe('/articles/' + sampleArticlePutData._id);
155-
}));
160+
// Test URL location to new object
161+
expect($location.path()).toBe('/articles/' + mockArticle._id);
162+
}));
163+
164+
it('should set scope.error to error response message', inject(function(Articles) {
165+
var errorMessage = 'error';
166+
$httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(400, {
167+
message: errorMessage
168+
});
169+
170+
scope.update();
171+
$httpBackend.flush();
172+
173+
expect(scope.error).toBe(errorMessage);
174+
}));
175+
});
156176

157-
it('$scope.remove() should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Articles) {
158-
// Create new article object
159-
var sampleArticle = new Articles({
160-
_id: '525a8422f6d0f87f0e407a33'
177+
describe('$scope.remove(article)', function() {
178+
beforeEach(function() {
179+
// Create new articles array and include the article
180+
scope.articles = [mockArticle, {}];
181+
182+
// Set expected DELETE response
183+
$httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204);
184+
185+
// Run controller functionality
186+
scope.remove(mockArticle);
161187
});
162188

163-
// Create new articles array and include the article
164-
scope.articles = [sampleArticle];
189+
it('should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Articles) {
190+
expect(scope.articles.length).toBe(1);
191+
}));
192+
});
165193

166-
// Set expected DELETE response
167-
$httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204);
194+
describe('scope.remove()', function() {
195+
beforeEach(function() {
196+
spyOn($location, 'path');
197+
scope.article = mockArticle;
168198

169-
// Run controller functionality
170-
scope.remove(sampleArticle);
171-
$httpBackend.flush();
199+
$httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204);
172200

173-
// Test array after successful delete
174-
expect(scope.articles.length).toBe(0);
175-
}));
201+
scope.remove();
202+
$httpBackend.flush();
203+
});
204+
205+
it('should redirect to articles', function() {
206+
expect($location.path).toHaveBeenCalledWith('articles');
207+
});
208+
});
176209
});
177210
}());

modules/chat/tests/client/chat.client.controller.tests.js

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,91 @@
44
* Chat client controller tests
55
*/
66
(function() {
7-
describe('ChatController', function() {
8-
// TODO: Add chat client controller tests
9-
});
7+
describe('ChatController', function() {
8+
//Initialize global variables
9+
var scope,
10+
Socket,
11+
ChatController,
12+
$timeout,
13+
$location,
14+
Authentication;
15+
16+
// Load the main application module
17+
beforeEach(module(ApplicationConfiguration.applicationModuleName));
18+
19+
beforeEach(inject(function($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$location_) {
20+
scope = $rootScope.$new();
21+
Socket = _Socket_;
22+
$timeout = _$timeout_;
23+
$location = _$location_;
24+
Authentication = _Authentication_;
25+
}));
26+
27+
describe('when user logged out', function() {
28+
beforeEach(inject(function($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$location_) {
29+
Authentication.user = undefined;
30+
spyOn($location, 'path');
31+
ChatController = $controller('ChatController', {
32+
$scope: scope,
33+
});
34+
}));
35+
36+
it('should redirect logged out user to /', function() {
37+
expect($location.path).toHaveBeenCalledWith('/');
38+
});
39+
});
40+
41+
describe('when user logged in', function() {
42+
beforeEach(inject(function($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$location_) {
43+
Authentication.user = {
44+
name: 'user',
45+
roles: ['user']
46+
};
47+
48+
ChatController = $controller('ChatController', {
49+
$scope: scope,
50+
});
51+
}));
52+
53+
it('should make sure socket is connected', function() {
54+
expect(Socket.socket).toBeTruthy();
55+
});
56+
57+
it('should define messages array', function() {
58+
expect(scope.messages).toBeDefined();
59+
expect(scope.messages.length).toBe(0);
60+
});
61+
62+
describe('sendMessage', function() {
63+
var text = 'hello world!';
64+
beforeEach(function() {
65+
scope.messageText = text;
66+
scope.sendMessage();
67+
$timeout.flush();
68+
});
69+
70+
it('should add message to messages', function() {
71+
expect(scope.messages.length).toBe(1);
72+
});
73+
74+
it('should add message with proper text attribute set', function() {
75+
expect(scope.messages[0].text).toBe(text);
76+
});
77+
78+
it('should clear messageText', function() {
79+
expect(scope.messageText).toBe('');
80+
});
81+
});
82+
83+
describe('$destroy()', function() {
84+
beforeEach(function() {
85+
scope.$destroy();
86+
});
87+
88+
it('should remove chatMessage listener', function() {
89+
expect(Socket.socket.cbs.chatMessage).toBeUndefined();
90+
});
91+
});
92+
});
93+
});
1094
}());

0 commit comments

Comments
 (0)