From a984bf0d688b8d55e514b92931731d40614b2586 Mon Sep 17 00:00:00 2001 From: Per Ploug Krogslund Date: Thu, 4 Jul 2013 09:28:27 +0200 Subject: [PATCH] Fixes failing tests Adds a seperate unit test app loader, removes e2e backend from tests adds scope.digest and httpbackend.flush to tests --- .../src/{app_dev.js => app.dev.js} | 0 .../common/mocks/resources/content.mocks.js | 3 +- src/Umbraco.Web.UI.Client/src/loader.js | 2 +- .../test/config/app.unit.js | 8 ++ .../test/config/karma.conf.js | 7 +- .../app/content/editContentController.spec.js | 85 ++++++++++--------- src/Umbraco.Web.UI/umbraco/index.html | 60 ++++++------- src/Umbraco.Web.UI/umbraco/js/app.dev.js | 8 ++ src/Umbraco.Web.UI/umbraco/js/loader.js | 2 +- 9 files changed, 100 insertions(+), 75 deletions(-) rename src/Umbraco.Web.UI.Client/src/{app_dev.js => app.dev.js} (100%) create mode 100644 src/Umbraco.Web.UI.Client/test/config/app.unit.js create mode 100644 src/Umbraco.Web.UI/umbraco/js/app.dev.js diff --git a/src/Umbraco.Web.UI.Client/src/app_dev.js b/src/Umbraco.Web.UI.Client/src/app.dev.js similarity index 100% rename from src/Umbraco.Web.UI.Client/src/app_dev.js rename to src/Umbraco.Web.UI.Client/src/app.dev.js diff --git a/src/Umbraco.Web.UI.Client/src/common/mocks/resources/content.mocks.js b/src/Umbraco.Web.UI.Client/src/common/mocks/resources/content.mocks.js index 4e35d03085..1abc57898b 100644 --- a/src/Umbraco.Web.UI.Client/src/common/mocks/resources/content.mocks.js +++ b/src/Umbraco.Web.UI.Client/src/common/mocks/resources/content.mocks.js @@ -103,11 +103,10 @@ angular.module('umbraco.mocks'). .whenGET(mocksUtills.urlRegex('/umbraco/UmbracoApi/Content/GetById')) .respond(returnNodebyId); + $httpBackend .whenGET(mocksUtills.urlRegex('/umbraco/UmbracoApi/Content/GetEmpty')) .respond(returnEmptyNode); - - }, diff --git a/src/Umbraco.Web.UI.Client/src/loader.js b/src/Umbraco.Web.UI.Client/src/loader.js index 78102a6811..1c8490e1f1 100644 --- a/src/Umbraco.Web.UI.Client/src/loader.js +++ b/src/Umbraco.Web.UI.Client/src/loader.js @@ -10,7 +10,7 @@ yepnope({ 'lib/umbraco/Extensions.js', 'js/umbraco.servervariables.js', - 'js/app_dev.js', + 'js/app.dev.js', 'js/umbraco.httpbackend.js', 'js/umbraco.testing.js', diff --git a/src/Umbraco.Web.UI.Client/test/config/app.unit.js b/src/Umbraco.Web.UI.Client/test/config/app.unit.js new file mode 100644 index 0000000000..e21617e5f5 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/test/config/app.unit.js @@ -0,0 +1,8 @@ +var app = angular.module('umbraco', [ + 'umbraco.filters', + 'umbraco.directives', + 'umbraco.resources', + 'umbraco.services', + 'umbraco.mocks', + 'umbraco.security' +]); \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/test/config/karma.conf.js b/src/Umbraco.Web.UI.Client/test/config/karma.conf.js index 6ffe03a7df..8b6f171072 100644 --- a/src/Umbraco.Web.UI.Client/test/config/karma.conf.js +++ b/src/Umbraco.Web.UI.Client/test/config/karma.conf.js @@ -12,13 +12,16 @@ module.exports = function(karma) { 'lib/underscore/underscore.js', 'test/lib/angular/angular-mocks.js', 'lib/umbraco/Extensions.js', - 'src/app_dev.js', + + 'test/config/app.unit.js', + 'src/common/mocks/umbraco.servervariables.js', + 'src/common/directives/*.js', 'src/common/filters/*.js', 'src/common/services/*.js', 'src/common/security/*.js', 'src/common/resources/*.js', - 'src/common/mocks/**/*.js', + 'src/common/mocks/resources/*.js', 'src/views/**/*.controller.js', 'test/unit/**/*.spec.js' ], diff --git a/src/Umbraco.Web.UI.Client/test/unit/app/content/editContentController.spec.js b/src/Umbraco.Web.UI.Client/test/unit/app/content/editContentController.spec.js index 8941a37d17..fd1d963e16 100644 --- a/src/Umbraco.Web.UI.Client/test/unit/app/content/editContentController.spec.js +++ b/src/Umbraco.Web.UI.Client/test/unit/app/content/editContentController.spec.js @@ -4,10 +4,15 @@ describe('edit content controller tests', function () { beforeEach(module('umbraco')); - beforeEach(inject(function ($rootScope, $controller, angularHelper, $httpBackend) { + //inject the contentMocks service + beforeEach(inject(function ($rootScope, $controller, angularHelper, $httpBackend, contentMocks) { httpBackend = $httpBackend; scope = $rootScope.$new(); + //have the contentMocks register its expect urls on the httpbackend + //see /mocks/content.mocks.js for how its setup + contentMocks.register(); + //this controller requires an angular form controller applied to it scope.contentForm = angularHelper.getNullForm("contentForm"); @@ -15,6 +20,15 @@ describe('edit content controller tests', function () { $scope: scope, $routeParams: routeParams }); + + //For controller tests its easiest to have the digest and flush happen here + //since its intially always the same $http calls made + + //scope.$digest resolves the promise against the httpbackend + scope.$digest(); + //httpbackend.flush() resolves all request against the httpbackend + //to fake a async response, (which is what happens on a real setup) + httpBackend.flush(); })); describe('content edit controller save and publish', function () { @@ -35,52 +49,45 @@ describe('edit content controller tests', function () { expect(scope.files[0].file).toBe("testFile2"); }); - //it('it should have an content object', function() { + it('it should have an content object', function() { - /* - NOTE: I cannot figure out how to make this work... I've followed along with a few sources like: - http://stackoverflow.com/questions/15833462/angularjs-need-help-to-unit-test-a-factory-with-promise - http://www.benlesh.com/2013/05/angularjs-unit-testing-controllers.html + //controller should have a content object + expect(scope.content).toNotBe(undefined); - But it tells me that there is no pending request to flush, so dunno what is going on there? - */ + //if should be the same as the routeParams defined one + expect(scope.content.id).toBe(1234); + }); - // httpBackend.flush(); + it('it should have a tabs collection', function () { + expect(scope.content.tabs.length).toBe(5); + }); - // rootScope.$apply(); + it('it should have a properties collection on each tab', function () { + $(scope.content.tabs).each(function(i, tab){ + expect(tab.properties.length).toBeGreaterThan(0); + }); + }); - // expect(scope.content).toNotBe(undefined); - // //expect(scope.content.id).toBe(1234); - //}); + it('it should change updateDate on save', function () { + var currentUpdateDate = scope.content.updateDate; - //it('it should have a tabs collection', function () { - // expect(scope.content.tabs.length).toBe(5); - //}); + setTimeout(function(){ + scope.save(scope.content); + expect(scope.content.updateDate).toBeGreaterThan(currentUpdateDate); + }, 1000); + }); - //it('it should have a properties collection on each tab', function () { - // $(scope.content.tabs).each(function(i, tab){ - // expect(tab.properties.length).toBeGreaterThan(0); - // }); - //}); + it('it should change publishDate on publish', function () { + var currentPublishDate = scope.content.publishDate; - //it('it should change updateDate on save', function () { - // var currentUpdateDate = scope.content.updateDate; - - // setTimeout(function(){ - // scope.save(scope.content); - // expect(scope.content.updateDate).toBeGreaterThan(currentUpdateDate); - // }, 1000); - // }); - - //it('it should change publishDate on publish', function () { - // var currentPublishDate = scope.content.publishDate; - - // //wait a sec before you publish - // setTimeout(function(){ - // scope.saveAndPublish(scope.content); - // expect(scope.content.publishDate).toBeGreaterThan(currentPublishDate); - // }, 1000); - //}); + //wait a sec before you publish + setTimeout(function(){ + scope.saveAndPublish(scope.content); + + expect(scope.content.publishDate).toBeGreaterThan(currentPublishDate); + + }, 1000); + }); }); diff --git a/src/Umbraco.Web.UI/umbraco/index.html b/src/Umbraco.Web.UI/umbraco/index.html index cd60633009..d715deb792 100644 --- a/src/Umbraco.Web.UI/umbraco/index.html +++ b/src/Umbraco.Web.UI/umbraco/index.html @@ -1,30 +1,30 @@ - - - - - - - - Umbraco - - - - -
- - - -
-
-
-
-
-
- - - - - - - - + + + + + + + + Umbraco + + + + +
+ + + +
+
+
+
+
+
+ + + + + + + + diff --git a/src/Umbraco.Web.UI/umbraco/js/app.dev.js b/src/Umbraco.Web.UI/umbraco/js/app.dev.js new file mode 100644 index 0000000000..fd806e6d25 --- /dev/null +++ b/src/Umbraco.Web.UI/umbraco/js/app.dev.js @@ -0,0 +1,8 @@ +var app = angular.module('umbraco', [ + 'umbraco.filters', + 'umbraco.directives', + 'umbraco.resources', + 'umbraco.services', + 'umbraco.httpbackend', + 'umbraco.security' +]); \ No newline at end of file diff --git a/src/Umbraco.Web.UI/umbraco/js/loader.js b/src/Umbraco.Web.UI/umbraco/js/loader.js index 78102a6811..1c8490e1f1 100644 --- a/src/Umbraco.Web.UI/umbraco/js/loader.js +++ b/src/Umbraco.Web.UI/umbraco/js/loader.js @@ -10,7 +10,7 @@ yepnope({ 'lib/umbraco/Extensions.js', 'js/umbraco.servervariables.js', - 'js/app_dev.js', + 'js/app.dev.js', 'js/umbraco.httpbackend.js', 'js/umbraco.testing.js',