Angular e2e test to check a list of elements

Issue My case is as follows – I have a simple page which lists available products: <ul class=”products ng-scope” id=”products”> <!– ngRepeat: tag in products –> <li ng-repeat=”product in products” class=”ng-scope”> <a ng-href=”/#/viewProduct/1″ class=”ng-binding” href=”/#/viewProduct/1″>Product A</a> </li><li ng-repeat=”product in products”

Continue reading

maxlength on field not working with Karma

Issue I have a field with a maxlength of 6, but somehow the following way of entering data results in 7 chars being allowed: <input type=”text” name=”myName” maxlength=”6″ ng-model=”myModel”> this is the test bit: input(‘myModel’).enter(‘1111117’); expect(input(‘myModel’).val()).toBe(111111); and this is the

Continue reading

karma/jasmine/angular toHaveBeenCalledWith is really called

Issue in my app for instance I’ve got $rootScope.$emit(‘loggedin’,data.user); $rootScope.$on(‘loggedin’, function(event,user) { console.log(‘called’); }); in my test spyOn($rootScope, ‘$emit’); var response = { “success”:1, “user”:{ “id”:1, “email”:”lama@test.test”, “fullname”:”Lama user”, “username”:”lamauser”, “groups”:[“Users”] }, “logged”:1 }; $httpBackend.when(‘POST’, ‘/api/v1/user’).respond(200,response); $scope.save(); $httpBackend.flush(); expect($scope.errors.length).toEqual(0); expect($rootScope.$emit).toHaveBeenCalledWith(‘loggedin’,response.user);

Continue reading

karma/angularjs how to test run block with an asynchronous service

Issue How can I test like: init.js lama.system module angular.module(‘lama.system’, []) .config([‘$httpProvider’, function($httpProvider) { // Crossdomain requests not allowed if you want do cors request see filter.php $httpProvider.defaults.headers.common[‘X-Requested-With’] = ‘XMLHttpRequest’; }]) .run([‘$rootScope’, ‘$state’, ‘$log’, ‘Global’,function ($rootScope, $state, $log, Global) {

Continue reading

Angular services not being defined in beforeEach in Karma

Issue I have an Angular Application using Jasmine and Karma for testing. This is my testing class: var data = require(‘./user.mock.js’); describe(‘Service: UserService’, function () { var ServerUrl; var httpBackend; var userService; beforeEach(angular.mock.module(‘myModule’)); beforeEach(angular.mock.inject(function (_userService_, $httpBackend, _ServerUrl_) { userService =

Continue reading

Testing $interval in Jasmine/ Karma

Issue I have a simple factory angular.module(‘myApp.dice’,[]).factory(‘Dice’, [‘$interval’, function($interval){ return { rollDice: function(){ return $interval(function(c){ count++; }, 100, 18, false, 0); } }; }]); In my test case I have describe(‘rolling dice’, function(){ var promise, promiseCalled = false, notify =

Continue reading

karma test returning validateAmount is not a function

Issue I’m trying to test scope function which will check due amount, but while running the test I’m getting validateAmount is not a function. app.js var ManagmentApp = angular.module(“ManagemntApp”, [‘ngRoute’, ‘angularModalService’, ‘ng-fusioncharts’]); ManagmentApp.config([‘$routeProvider’, function ($routeProvider){ $routeProvider.when(‘/’, { templateUrl: ‘templates/CandidateForm.html’, controller:

Continue reading

Getting a `$digest already in progress` error when testing a `catch()` error handler

Issue This is my test: it(‘add.user() should POST to /users/, failure’, function() { mockBackend.expectPOST(“/users/”, {username:’u’, password: ‘p’, email: ‘e’, location: ‘loc’}).respond(400, {msg: “bad request”}); BaseService.add.user({username:’u’, password: ‘p’, email: ‘e’, location: ‘loc’}); mockBackend.flush(); }); afterEach(function() { mockBackend.verifyNoOutstandingExpectation(); mockBackend.verifyNoOutstandingRequest(); }); And when

Continue reading

Test component with a providers

Issue I have a service SoundPanelService which is used in service isolation scenario ( like https://angular.io/guide/hierarchical-dependency-injection#scenario-service-isolation ) @Injectable() export class SoundPanelService { recorded = new Subject<Sound>(); constructor() { } } and I have SoundPanelComponent Component({ selector: ‘app-sound-panel’, templateUrl: ‘./sound-panel.component.html’, styleUrls:

Continue reading

karma plugin dependencies not being found

Issue When I run karma start I get the following issues C:\devl\JS\myProject>karma start 06 09 2015 11:30:19.133:WARN [plugin]: Cannot find plugin “karma-chrome-launcher “. Did you forget to install it ? npm install karma-chrome-launcher –save-dev 06 09 2015 11:30:19.149:WARN [plugin]: Cannot

Continue reading

Mocking $httpBackend – how to handle "Unexpected request, No more request expected"?

Issue I have a Jasmine test that is coded like this: it (“should send correct message to server to get data, and correctly set up scope when receiving it”, function(){ $httpBackend.when(‘GET’, ‘https://localhost:44300/api/projectconfiguration/12’).respond(fakedDtoBase); $routeParams.projectId=fakeId; // user asks for editing project scope.$apply(function(){

Continue reading

Angular Jasmine, unit test case fails when global variable assigned

Issue I have a basic unit-test case written for two methods. it(‘should populateChartDatasets call getColorCodes’, () => { component.chartConfig = mockChartConfig; spyOn(component, ‘getColorCodes’); component.populateChartDatasets(filteredTreatments, filteredYears); expect(component.getColorCodes).toHaveBeenCalled(); }); method populateChartDatasets firstly supposed to pick colorCode from method getColorCodes then push prepared

Continue reading