From c4685392cfd841a9873ce7b28810d1c5f81eaaba Mon Sep 17 00:00:00 2001 From: cdriscol Date: Sun, 26 Jul 2015 10:21:49 -0600 Subject: [PATCH] Adding karma-ng-html2js-preprocessor to fix karma tests. Adding user mock to articles tests. --- karma.conf.js | 98 ++--- .../articles.client.controller.tests.js | 339 +++++++++--------- package.json | 1 + 3 files changed, 229 insertions(+), 209 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 2e926ad5f3..ecd20fc4a6 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -4,50 +4,62 @@ * Module dependencies. */ var _ = require('lodash'), - defaultAssets = require('./config/assets/default'), - testAssets = require('./config/assets/test'); + defaultAssets = require('./config/assets/default'), + testAssets = require('./config/assets/test'); // Karma configuration module.exports = function(karmaConfig) { - karmaConfig.set({ - // Frameworks to use - frameworks: ['jasmine'], - - // List of files / patterns to load in the browser - files: _.union(defaultAssets.client.lib.js, defaultAssets.client.lib.tests, defaultAssets.client.js, testAssets.tests.client), - - // Test results reporter to use - // Possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' - reporters: ['progress'], - - // Web server port - port: 9876, - - // Enable / disable colors in the output (reporters and logs) - colors: true, - - // Level of logging - // Possible values: karmaConfig.LOG_DISABLE || karmaConfig.LOG_ERROR || karmaConfig.LOG_WARN || karmaConfig.LOG_INFO || karmaConfig.LOG_DEBUG - logLevel: karmaConfig.LOG_INFO, - - // Enable / disable watching file and executing tests whenever any file changes - autoWatch: true, - - // Start these browsers, currently available: - // - Chrome - // - ChromeCanary - // - Firefox - // - Opera - // - Safari (only Mac) - // - PhantomJS - // - IE (only Windows) - browsers: ['PhantomJS'], - - // If browser does not capture in given timeout [ms], kill it - captureTimeout: 60000, - - // Continuous Integration mode - // If true, it capture browsers, run tests and exit - singleRun: true - }); + karmaConfig.set({ + // Frameworks to use + frameworks: ['jasmine'], + + preprocessors: { + 'modules/*/client/views/**/*.html': ['ng-html2js'] + }, + + ngHtml2JsPreprocessor: { + moduleName: 'mean', + + cacheIdFromPath: function(filepath) { + return filepath.replace('/client', ''); + }, + }, + + // List of files / patterns to load in the browser + files: _.union(defaultAssets.client.lib.js, defaultAssets.client.lib.tests, defaultAssets.client.js, testAssets.tests.client, defaultAssets.client.views), + + // Test results reporter to use + // Possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' + reporters: ['progress'], + + // Web server port + port: 9876, + + // Enable / disable colors in the output (reporters and logs) + colors: true, + + // Level of logging + // Possible values: karmaConfig.LOG_DISABLE || karmaConfig.LOG_ERROR || karmaConfig.LOG_WARN || karmaConfig.LOG_INFO || karmaConfig.LOG_DEBUG + logLevel: karmaConfig.LOG_INFO, + + // Enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + // Start these browsers, currently available: + // - Chrome + // - ChromeCanary + // - Firefox + // - Opera + // - Safari (only Mac) + // - PhantomJS + // - IE (only Windows) + browsers: ['PhantomJS'], + + // If browser does not capture in given timeout [ms], kill it + captureTimeout: 60000, + + // Continuous Integration mode + // If true, it capture browsers, run tests and exit + singleRun: true + }); }; diff --git a/modules/articles/tests/client/articles.client.controller.tests.js b/modules/articles/tests/client/articles.client.controller.tests.js index 04c0918437..e05f07ecbb 100644 --- a/modules/articles/tests/client/articles.client.controller.tests.js +++ b/modules/articles/tests/client/articles.client.controller.tests.js @@ -1,170 +1,177 @@ 'use strict'; (function() { - // Articles Controller Spec - describe('Articles Controller Tests', function() { - // Initialize global variables - var ArticlesController, - scope, - $httpBackend, - $stateParams, - $location; - - // 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. - beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) { - // Set a new global scope - scope = $rootScope.$new(); - - // Point global variables to injected services - $stateParams = _$stateParams_; - $httpBackend = _$httpBackend_; - $location = _$location_; - - // Initialize the Articles controller. - ArticlesController = $controller('ArticlesController', { - $scope: scope - }); - })); - - 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]; - - // Set GET response - $httpBackend.expectGET('api/articles').respond(sampleArticles); - - // Run controller functionality - scope.find(); - $httpBackend.flush(); - - // Test scope value - expect(scope.articles).toEqualData(sampleArticles); - })); - - 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'; - - // Set GET response - $httpBackend.expectGET(/api\/articles\/([0-9a-fA-F]{24})$/).respond(sampleArticle); - - // Run controller functionality - scope.findOne(); - $httpBackend.flush(); - - // Test scope value - expect(scope.article).toEqualData(sampleArticle); - })); - - 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!' - }); - - // Create a sample article response - var sampleArticleResponse = new Articles({ - _id: '525cf20451979dea2c000001', - title: 'An Article about MEAN', - content: 'MEAN rocks!' - }); - - // Fixture mock form input values - scope.title = 'An Article about MEAN'; - scope.content = 'MEAN rocks!'; - - // Set POST response - $httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(sampleArticleResponse); - - // Run controller functionality - scope.create(); - $httpBackend.flush(); - - // Test form inputs are reset - expect(scope.title).toEqual(''); - expect(scope.content).toEqual(''); - - // Test URL redirection after the article was created - expect($location.path()).toBe('/articles/' + sampleArticleResponse._id); - })); - - 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!' - }); - - // Mock article in scope - scope.article = sampleArticlePutData; - - // Set PUT response - $httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(); - - // Run controller functionality - scope.update(); - $httpBackend.flush(); - - // Test URL location to new object - expect($location.path()).toBe('/articles/' + sampleArticlePutData._id); - })); - - 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' - }); - - // Create new articles array and include the article - scope.articles = [sampleArticle]; - - // Set expected DELETE response - $httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204); - - // Run controller functionality - scope.remove(sampleArticle); - $httpBackend.flush(); - - // Test array after successful delete - expect(scope.articles.length).toBe(0); - })); - }); + // Articles Controller Spec + describe('Articles Controller Tests', function() { + // Initialize global variables + var ArticlesController, + scope, + $httpBackend, + $stateParams, + $location, + Authentication; + + // 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. + beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Authentication_) { + // Set a new global scope + scope = $rootScope.$new(); + + // Point global variables to injected services + $stateParams = _$stateParams_; + $httpBackend = _$httpBackend_; + $location = _$location_; + Authentication = _Authentication_; + + // Mock logged in user + Authentication.user = { + roles: ['user'] + }; + + // Initialize the Articles controller. + ArticlesController = $controller('ArticlesController', { + $scope: scope + }); + })); + + 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]; + + // Set GET response + $httpBackend.expectGET('api/articles').respond(sampleArticles); + + // Run controller functionality + scope.find(); + $httpBackend.flush(); + + // Test scope value + expect(scope.articles).toEqualData(sampleArticles); + })); + + 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'; + + // Set GET response + $httpBackend.expectGET(/api\/articles\/([0-9a-fA-F]{24})$/).respond(sampleArticle); + + // Run controller functionality + scope.findOne(); + $httpBackend.flush(); + + // Test scope value + expect(scope.article).toEqualData(sampleArticle); + })); + + 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!' + }); + + // Create a sample article response + var sampleArticleResponse = new Articles({ + _id: '525cf20451979dea2c000001', + title: 'An Article about MEAN', + content: 'MEAN rocks!' + }); + + // Fixture mock form input values + scope.title = 'An Article about MEAN'; + scope.content = 'MEAN rocks!'; + + // Set POST response + $httpBackend.expectPOST('api/articles', sampleArticlePostData).respond(sampleArticleResponse); + + // Run controller functionality + scope.create(); + $httpBackend.flush(); + + // Test form inputs are reset + expect(scope.title).toEqual(''); + expect(scope.content).toEqual(''); + + // Test URL redirection after the article was created + expect($location.path()).toBe('/articles/' + sampleArticleResponse._id); + })); + + 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!' + }); + + // Mock article in scope + scope.article = sampleArticlePutData; + + // Set PUT response + $httpBackend.expectPUT(/api\/articles\/([0-9a-fA-F]{24})$/).respond(); + + // Run controller functionality + scope.update(); + $httpBackend.flush(); + + // Test URL location to new object + expect($location.path()).toBe('/articles/' + sampleArticlePutData._id); + })); + + 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' + }); + + // Create new articles array and include the article + scope.articles = [sampleArticle]; + + // Set expected DELETE response + $httpBackend.expectDELETE(/api\/articles\/([0-9a-fA-F]{24})$/).respond(204); + + // Run controller functionality + scope.remove(sampleArticle); + $httpBackend.flush(); + + // Test array after successful delete + expect(scope.articles.length).toBe(0); + })); + }); }()); diff --git a/package.json b/package.json index fb3c7b3b3f..24ecb6a4dc 100644 --- a/package.json +++ b/package.json @@ -97,6 +97,7 @@ "karma-coverage": "~0.4.2", "karma-firefox-launcher": "~0.1.6", "karma-jasmine": "~0.3.6", + "karma-ng-html2js-preprocessor": "^0.1.2", "karma-phantomjs-launcher": "~0.2.0", "load-grunt-tasks": "^3.2.0", "run-sequence": "^1.1.1",