File rename, refactor $httpBackend mocks

This commit is contained in:
Per Ploug Krogslund
2013-07-02 09:17:32 +02:00
parent 32d7b88b45
commit 0e431ffd3c
11 changed files with 127 additions and 120 deletions

View File

@@ -1,8 +1,8 @@
angular.module('umbraco.mocks').
factory('contentMocks', ['$httpBackend', 'mocksUtills', function ($httpBackend, mocksUtills) {
'use strict';
function returnNodebyId(status, data, headers) {
angular.module('umbraco.mocks').
factory('contentMocks', ['$httpBackend', 'mocksUtills', function ($httpBackend, mocksUtills) {
'use strict';
function returnNodebyId(status, data, headers) {
var id = mocksUtills.getParameterByName(data, "id") || 1234;
var node = {
@@ -74,18 +74,19 @@ angular.module('umbraco.mocks').
]
};
return [200, node, null];
}
return {
register: function() {
$httpBackend
.whenGET(mocksUtills.urlRegex('/umbraco/UmbracoApi/Content/GetById'))
.respond(returnNodebyId);
}
};
}
return {
register: function() {
$httpBackend
.whenGET(mocksUtills.urlRegex('/umbraco/UmbracoApi/Content/GetById'))
.respond(returnNodebyId);
},
expectGetById: function() {
$httpBackend
.expectGET(mocksUtills.urlRegex('/umbraco/UmbracoApi/Content/GetById'));
}
};
}]);

View File

@@ -3,22 +3,24 @@ angular.module('umbraco.mocks').
'use strict';
function returnAllowedChildren(status, data, headers) {
var types = [
{ name: "News Article", description: "Standard news article", alias: "newsArticle", id: 1234, cssClass: "file" },
{ name: "News Area", description: "Area to hold all news articles, there should be only one", alias: "newsArea", id: 1234, cssClass: "suitcase" },
{ name: "Employee", description: "Employee profile information page", alias: "employee", id: 1234, cssClass: "user" }
];
var types = [
{ name: "News Article", description: "Standard news article", alias: "newsArticle", id: 1234, cssClass: "file" },
{ name: "News Area", description: "Area to hold all news articles, there should be only one", alias: "newsArea", id: 1234, cssClass: "suitcase" },
{ name: "Employee", description: "Employee profile information page", alias: "employee", id: 1234, cssClass: "user" }
];
return [200, types, null];
}
return {
register: function() {
$httpBackend
.whenGET(mocksUtills.urlRegex('/umbraco/Api/'))
$httpBackend
.whenGET(mocksUtills.urlRegex('/umbraco/Api/'))
.respond(returnAllowedChildren);
},
expectAllowedChildren: function(){
console.log("expecting get");
$httpBackend.expectGET(mocksUtills.urlRegex('/umbraco/Api/'));
}
};
}]);

View File

@@ -1,15 +1,17 @@
var umbracoAppDev = angular.module('umbraco.httpbackend', ['umbraco', 'ngMockE2E', 'umbraco.mocks']);
function initBackEnd($httpBackend, contentMocks, treeMocks, userMocks, contentTypeMocks) {
//Register mocked http responses
contentMocks.register();
treeMocks.register();
userMocks.register();
function initBackEnd($httpBackend, contentMocks, treeMocks, userMocks, contentTypeMocks) {
console.log("httpBackend inited");
//Register mocked http responses
contentMocks.register();
treeMocks.register();
userMocks.register();
contentTypeMocks.register();
$httpBackend.whenGET(/^views\//).passThrough();

View File

@@ -33,12 +33,12 @@ function contentResource($q, $http, umbDataFormatter, umbRequestHelper) {
return {
getById: function (id) {
var deferred = $q.defer();
// var deferred = $q.defer();
return $http.get("/umbraco/UmbracoApi/Content/GetById?id=" + id);
//go and get the data
$http.get(getContentUrl(id)).
/*$http.get(getContentUrl(id)).
success(function (data, status, headers, config) {
console.log("getting by id success");
@@ -58,7 +58,7 @@ function contentResource($q, $http, umbDataFormatter, umbRequestHelper) {
deferred.reject('Failed to retreive data for content id ' + id);
});
return deferred.promise;
return deferred.promise;*/
},
getByIds: function (ids) {

View File

@@ -43,23 +43,14 @@ function contentTypeResource($q, $http) {
//return all types allowed under given document
getAllowedTypes: function (contentId) {
var deferred = $q.defer();
//go and get the tree data
$http.get(getChildContentTypesUrl(contentId)).
success(function (data, status, headers, config) {
console.log("success");
$http.get(getChildContentTypesUrl(contentId))
.success(function (data, status, headers, config) {
deferred.resolve(data);
}).
error(function (data, status, headers, config) {
console.log("wrong");
deferred.reject('Failed to retreive data for content id ' + contentId);
});
return deferred.promise;
}

View File

@@ -60,7 +60,7 @@ module.exports = function(karma) {
// - PhantomJS
// - IE (only Windows)
// CLI --browsers Chrome,Firefox,Safari
browsers: ['PhantomJS'],
browsers: ['Chrome'],
// If browser does not capture in given timeout [ms], kill it
// CLI --capture-timeout 5000

View File

@@ -1,44 +1,39 @@
describe('content factory tests', function () {
var $rootScope, $httpBackend, contentFactory;
var $rootScope, $httpBackend, contentFactory, mocks, $http;
beforeEach(module('umbraco.services'));
beforeEach(module('umbraco.resources'));
beforeEach(module('umbraco.httpbackend'));
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$rootScope = $injector.get('$rootScope');
$httpBackend = $injector.get('$httpBackend');
mocks = $injector.get("contentMocks");
mocks.register();
contentFactory = $injector.get('contentResource');
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('global content factory crud', function () {
it('should return a content object, given an id', function () {
var doc = "meh";
contentFactory.getById(1234).then(function (result) {
doc = result;
});
console.log("doc:", doc);
$rootScope.$root.$digest();
//$scope.$apply();
//.then(function (doc1) {
console.log("doc:", doc);
expect(doc).toNotBe(undefined);
expect(doc.id).toBe(1234);
//});
it('should return a content object, given an id', function () {
var doc;
contentFactory.getById(1234).then(function(result){
doc = result;
});
$rootScope.$digest();
expect(doc).toNotBe(undefined);
expect(doc.id).toBe(1234);
});
it('should return a content children collection given an id', function () {
var collection = contentFactory.getChildren(1234, undefined);
expect(collection.resultSet.length).toBe(10);

View File

@@ -1,34 +1,50 @@
describe('content type factory tests', function () {
var $rootScope, contentTypeResource;
var $rootScope, $httpBackend, contentTypeResource, m;
beforeEach(module('ngMockE2E'));
beforeEach(module('umbraco.resources'));
beforeEach(module('umbraco.httpbackend'));
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
beforeEach(inject(function ($injector, contentTypeMocks) {
$rootScope = $injector.get('$rootScope');
$httpBackend = $injector.get('$httpBackend');
m = contentTypeMocks;
m.register();
contentTypeResource = $injector.get('contentTypeResource');
}));
describe('global content type factory crud', function () {
it('should return a content type object, given an id', function () {
var ct1 = contentTypeResource.getContentType(1234);
$rootScope.$digest();
var ct1;
contentTypeResource.getContentType(1234).then(function(result){
ct1 = result;
});
$rootScope.$digest();
$rootScope.$apply();
console.log("ct1:", ct1);
expect(ct1).toNotBe(undefined);
expect(ct1.id).toBe(1234);
});
it('should return a allowed content type collection given a document id', function(){
// var collection = contentTypeResource.getAllowedTypes(1234);
// $rootScope.$apply();
// console.log("running test", angular.toJson(collection));
// expect(collection.length).toBe(3);
m.expectAllowedChildren();
var collection;
contentTypeResource.getAllowedTypes(1234).then(function(result){
collection = result;
});
$rootScope.$digest();
$rootScope.$apply();
expect(collection.length).toBe(3);
});
});
});

View File

@@ -1,30 +1,30 @@
<!doctype html>
<html lang="en">
<head>
<base href="/belle/" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Umbraco</title>
<link rel="stylesheet" href="assets/css/umbraco.css" />
</head>
<body ng-controller="MainController" id="umbracoMainPageBody">
<div ng-hide="!authenticated" ng-cloak ng-animate="'fade'" id="Div1" class="clearfix" ng-click="closeDialogs($event)">
<umb-left-column></umb-left-column>
<section id="contentwrapper">
<div id="contentcolumn">
<div ng-view></div>
</div>
</section>
</div>
<umb-notifications></umb-notifications>
<script src="lib/yepnope/yepnope.min.js"></script>
<script src="js/loader.js"></script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<base href="/belle/" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Umbraco</title>
<link rel="stylesheet" href="assets/css/umbraco.css" />
</head>
<body ng-controller="MainController" id="umbracoMainPageBody">
<div ng-hide="!authenticated" ng-cloak ng-animate="'fade'" id="Div1" class="clearfix" ng-click="closeDialogs($event)">
<umb-left-column></umb-left-column>
<section id="contentwrapper">
<div id="contentcolumn">
<div ng-view></div>
</div>
</section>
</div>
<umb-notifications></umb-notifications>
<script src="lib/yepnope/yepnope.min.js"></script>
<script src="js/loader.js"></script>
</body>
</html>