2016-07-25 17:34:06 -07:00
|
|
|
|
(function () {
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
2016-08-26 03:27:43 -07:00
|
|
|
|
describe('Articles Admin Controller Tests', function () {
|
2016-07-25 17:34:06 -07:00
|
|
|
|
// Initialize global variables
|
2016-08-26 03:27:43 -07:00
|
|
|
|
var ArticlesAdminController,
|
2016-07-25 17:34:06 -07:00
|
|
|
|
$scope,
|
|
|
|
|
|
$httpBackend,
|
|
|
|
|
|
$state,
|
|
|
|
|
|
Authentication,
|
|
|
|
|
|
ArticlesService,
|
2016-10-10 17:51:44 -04:00
|
|
|
|
mockArticle,
|
|
|
|
|
|
Notification;
|
2016-07-25 17:34:06 -07:00
|
|
|
|
|
|
|
|
|
|
// 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.
|
2016-10-10 17:51:44 -04:00
|
|
|
|
beforeEach(inject(function ($controller, $rootScope, _$state_, _$httpBackend_, _Authentication_, _ArticlesService_, _Notification_) {
|
2016-07-25 17:34:06 -07:00
|
|
|
|
// Set a new global scope
|
|
|
|
|
|
$scope = $rootScope.$new();
|
|
|
|
|
|
|
|
|
|
|
|
// Point global variables to injected services
|
|
|
|
|
|
$httpBackend = _$httpBackend_;
|
|
|
|
|
|
$state = _$state_;
|
|
|
|
|
|
Authentication = _Authentication_;
|
|
|
|
|
|
ArticlesService = _ArticlesService_;
|
2016-10-10 17:51:44 -04:00
|
|
|
|
Notification = _Notification_;
|
2016-07-25 17:34:06 -07:00
|
|
|
|
|
2016-10-05 17:51:53 -07:00
|
|
|
|
// Ignore parent template get on state transitions
|
|
|
|
|
|
$httpBackend.whenGET('/modules/core/client/views/home.client.view.html').respond(200, '');
|
|
|
|
|
|
|
2016-07-25 17:34:06 -07:00
|
|
|
|
// 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.
|
2016-08-26 03:27:43 -07:00
|
|
|
|
ArticlesAdminController = $controller('ArticlesAdminController as vm', {
|
2016-07-25 17:34:06 -07:00
|
|
|
|
$scope: $scope,
|
|
|
|
|
|
articleResolve: {}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Spy on state go
|
|
|
|
|
|
spyOn($state, 'go');
|
2016-10-10 17:51:44 -04:00
|
|
|
|
spyOn(Notification, 'error');
|
|
|
|
|
|
spyOn(Notification, 'success');
|
2016-07-25 17:34:06 -07:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2016-10-05 16:04:22 -07:00
|
|
|
|
$httpBackend.expectPOST('/api/articles', sampleArticlePostData).respond(mockArticle);
|
2016-07-25 17:34:06 -07:00
|
|
|
|
|
|
|
|
|
|
// Run controller functionality
|
|
|
|
|
|
$scope.vm.save(true);
|
|
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
|
// Test Notification success was called
|
|
|
|
|
|
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Article saved successfully!' });
|
2016-07-25 17:34:06 -07:00
|
|
|
|
// Test URL redirection after the article was created
|
|
|
|
|
|
expect($state.go).toHaveBeenCalledWith('admin.articles.list');
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
|
it('should call Notification.error if error', function () {
|
2016-07-25 17:34:06 -07:00
|
|
|
|
var errorMessage = 'this is an error message';
|
2016-10-05 16:04:22 -07:00
|
|
|
|
$httpBackend.expectPOST('/api/articles', sampleArticlePostData).respond(400, {
|
2016-07-25 17:34:06 -07:00
|
|
|
|
message: errorMessage
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$scope.vm.save(true);
|
|
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
|
expect(Notification.error).toHaveBeenCalledWith({ message: errorMessage, title: '<i class="glyphicon glyphicon-remove"></i> Article save error!' });
|
2016-07-25 17:34:06 -07:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
|
// Test Notification success was called
|
|
|
|
|
|
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Article saved successfully!' });
|
2016-07-25 17:34:06 -07:00
|
|
|
|
// Test URL location to new object
|
|
|
|
|
|
expect($state.go).toHaveBeenCalledWith('admin.articles.list');
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
|
it('should call Notification.error if error', inject(function (ArticlesService) {
|
2016-07-25 17:34:06 -07:00
|
|
|
|
var errorMessage = 'error';
|
|
|
|
|
|
$httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(400, {
|
|
|
|
|
|
message: errorMessage
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$scope.vm.save(true);
|
|
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
|
expect(Notification.error).toHaveBeenCalledWith({ message: errorMessage, title: '<i class="glyphicon glyphicon-remove"></i> Article save error!' });
|
2016-07-25 17:34:06 -07:00
|
|
|
|
}));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
|
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Article deleted successfully!' });
|
2016-07-25 17:34:06 -07:00
|
|
|
|
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();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}());
|