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

Commit

Permalink
Merge pull request #728 from cdriscol/angular_tests
Browse files Browse the repository at this point in the history
Add client side tests
Fixes #663
  • Loading branch information
ilanbiala committed Jul 29, 2015
2 parents 99a8168 + d5ea5c9 commit 612a76c
Show file tree
Hide file tree
Showing 9 changed files with 1,277 additions and 383 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public/lib/
app/tests/coverage/
.bower-*/
.idea/
coverage/

# MEAN.js app and assets
# ======================
Expand Down Expand Up @@ -68,4 +69,3 @@ mongod
*.ntvs*
*.njsproj
*.sln

179 changes: 106 additions & 73 deletions modules/articles/tests/client/articles.client.controller.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
$httpBackend,
$stateParams,
$location,
Authentication;
Authentication,
Articles,
mockArticle;

// 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
Expand All @@ -36,7 +38,7 @@
// 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, _$location_, _$stateParams_, _$httpBackend_, _Authentication_) {
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Authentication_, _Articles_) {
// Set a new global scope
scope = $rootScope.$new();

Expand All @@ -45,6 +47,14 @@
$httpBackend = _$httpBackend_;
$location = _$location_;
Authentication = _Authentication_;
Articles = _Articles_;

// create mock article
mockArticle = new Articles({
_id: '525a8422f6d0f87f0e407a33',
title: 'An Article about MEAN',
content: 'MEAN rocks!'
});

// Mock logged in user
Authentication.user = {
Expand All @@ -58,14 +68,8 @@
}));

it('$scope.find() should create an array with at least one article object fetched from XHR', inject(function(Articles) {
// Create sample article using the Articles service
var sampleArticle = new Articles({
title: 'An Article about MEAN',
content: 'MEAN rocks!'
});

// Create a sample articles array that includes the new article
var sampleArticles = [sampleArticle];
var sampleArticles = [mockArticle];

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

it('$scope.findOne() should create an array with one article object fetched from XHR using a articleId URL parameter', inject(function(Articles) {
// Define a sample article object
var sampleArticle = new Articles({
title: 'An Article about MEAN',
content: 'MEAN rocks!'
});

// Set the URL parameter
$stateParams.articleId = '525a8422f6d0f87f0e407a33';
$stateParams.articleId = mockArticle._id;

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

// Run controller functionality
scope.findOne();
$httpBackend.flush();

// Test scope value
expect(scope.article).toEqualData(sampleArticle);
expect(scope.article).toEqualData(mockArticle);
}));

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) {
// Create a sample article object
var sampleArticlePostData = new Articles({
title: 'An Article about MEAN',
content: 'MEAN rocks!'
});
describe('$scope.craete()', function() {
var sampleArticlePostData;

// Create a sample article response
var sampleArticleResponse = new Articles({
_id: '525cf20451979dea2c000001',
title: 'An Article about MEAN',
content: 'MEAN rocks!'
beforeEach(function() {
// Create a sample article object
sampleArticlePostData = new Articles({
title: 'An Article about MEAN',
content: 'MEAN rocks!'
});

// Fixture mock form input values
scope.title = 'An Article about MEAN';
scope.content = 'MEAN rocks!';

spyOn($location, 'path');
});

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

// Set POST response
$httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(sampleArticleResponse);
// Run controller functionality
scope.create();
$httpBackend.flush();

// Run controller functionality
scope.create();
$httpBackend.flush();
// Test form inputs are reset
expect(scope.title).toEqual('');
expect(scope.content).toEqual('');

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

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

it('$scope.update() should update a valid article', inject(function(Articles) {
// Define a sample article put data
var sampleArticlePutData = new Articles({
_id: '525cf20451979dea2c000001',
title: 'An Article about MEAN',
content: 'MEAN Rocks!'
scope.create();
$httpBackend.flush();

expect(scope.error).toBe(errorMessage);
});
});

// Mock article in scope
scope.article = sampleArticlePutData;
describe('$scope.update()', function() {
beforeEach(function() {
// Mock article in scope
scope.article = mockArticle;
});

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

// Run controller functionality
scope.update();
$httpBackend.flush();
// Run controller functionality
scope.update();
$httpBackend.flush();

// Test URL location to new object
expect($location.path()).toBe('/articles/' + sampleArticlePutData._id);
}));
// Test URL location to new object
expect($location.path()).toBe('/articles/' + mockArticle._id);
}));

it('should set scope.error to error response message', inject(function(Articles) {
var errorMessage = 'error';
$httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(400, {
message: errorMessage
});

scope.update();
$httpBackend.flush();

expect(scope.error).toBe(errorMessage);
}));
});

it('$scope.remove() should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Articles) {
// Create new article object
var sampleArticle = new Articles({
_id: '525a8422f6d0f87f0e407a33'
describe('$scope.remove(article)', function() {
beforeEach(function() {
// Create new articles array and include the article
scope.articles = [mockArticle, {}];

// Set expected DELETE response
$httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204);

// Run controller functionality
scope.remove(mockArticle);
});

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

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

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

// Test array after successful delete
expect(scope.articles.length).toBe(0);
}));
scope.remove();
$httpBackend.flush();
});

it('should redirect to articles', function() {
expect($location.path).toHaveBeenCalledWith('articles');
});
});
});
}());
90 changes: 87 additions & 3 deletions modules/chat/tests/client/chat.client.controller.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,91 @@
* Chat client controller tests
*/
(function() {
describe('ChatController', function() {
// TODO: Add chat client controller tests
});
describe('ChatController', function() {
//Initialize global variables
var scope,
Socket,
ChatController,
$timeout,
$location,
Authentication;

// Load the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));

beforeEach(inject(function($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$location_) {
scope = $rootScope.$new();
Socket = _Socket_;
$timeout = _$timeout_;
$location = _$location_;
Authentication = _Authentication_;
}));

describe('when user logged out', function() {
beforeEach(inject(function($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$location_) {
Authentication.user = undefined;
spyOn($location, 'path');
ChatController = $controller('ChatController', {
$scope: scope,
});
}));

it('should redirect logged out user to /', function() {
expect($location.path).toHaveBeenCalledWith('/');
});
});

describe('when user logged in', function() {
beforeEach(inject(function($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$location_) {
Authentication.user = {
name: 'user',
roles: ['user']
};

ChatController = $controller('ChatController', {
$scope: scope,
});
}));

it('should make sure socket is connected', function() {
expect(Socket.socket).toBeTruthy();
});

it('should define messages array', function() {
expect(scope.messages).toBeDefined();
expect(scope.messages.length).toBe(0);
});

describe('sendMessage', function() {
var text = 'hello world!';
beforeEach(function() {
scope.messageText = text;
scope.sendMessage();
$timeout.flush();
});

it('should add message to messages', function() {
expect(scope.messages.length).toBe(1);
});

it('should add message with proper text attribute set', function() {
expect(scope.messages[0].text).toBe(text);
});

it('should clear messageText', function() {
expect(scope.messageText).toBe('');
});
});

describe('$destroy()', function() {
beforeEach(function() {
scope.$destroy();
});

it('should remove chatMessage listener', function() {
expect(Socket.socket.cbs.chatMessage).toBeUndefined();
});
});
});
});
}());
Loading

0 comments on commit 612a76c

Please sign in to comment.