last day (14 days later) » 

1:36 PM
0
Q: AngularJS unit testing with jasmine form validity in a watch breaking tests

ChrisI'm pulling my hair out trying to work out whats I'm missing and after 5 hours of googling I'm not closer to the answer so this is my last shot. I have a AngularJS app and in one of the unit tests I'm checking that a controller displays some data after a api call. My problem is I have a watch o...

 
How are you getting the Form into your controller?
 
I've not done anything to get the form into the controller. I thought Angular injected the form into $scope? My apologies here as I'm pretty new to Angular
 
As far as I know it does not. Does your example work in your app, outside of the test? You shouldn't really be using the Forms in your controller.
 
Thats the thing the code works fine when the app is run up and has been working for a while. Its just the unit test that breaks but its looking like the unit test is right and the code is wrong. Am I right in assuming any custom validation should be performed using directives?
 
You're right, you should probably be doing it in directives. See Custom Validation: docs.angularjs.org/guide/forms.
You could for example make a is-before-date="dateToBeBefore" directive and a is-after-date="dateToBeAfter" directive. And you just provide the model value to each other inputs directive. Hope you understand what I meant.
 
1:36 PM
Thanks m.brand that makes sense to me, I'll look at refactoring this into a directive or two. I'm still curious thought as to how I have been able to reference the form and its controls though $scope :\
 
i could have a look for that if you wan't to provide some more code :)
 
This is the controller in question
'use strict';

angular.module('dfrtApp')
.controller('OrdersCtrl', [
'$scope', '$rootScope', '$http', '$filter', 'ngTableParams', '$sce', '$modal', '$state', 'orderSummaryService', 'orderService', 'searchCriteria', 'documentService', 'modalAlertService',
function ($scope, $rootScope, $http, $filter, ngTableParams, $sce, $modal, $state, orderSummaryService, orderService, SearchCriteria, documentService, modalAlertService) {

//// VARIABLES ////

$scope.ordersLoading = false;
$scope.pageHeading = '';
 
Hmm, really can't find any spot where this happens. Maybe you're mapping it somewhere in the html templates ... ?
 
1:51 PM
no i don't think so
<form class="form-horizontal" name="filterOrdersForm" novalidate>
thats the form declaration and there is nothing out of the ordinary there
weird maybe just chalk this one up to experience and move on
 
Uhh, I just saw that it always gets into the scope somehow, check this: plnkr.co/edit/s9TUMsDDWdeMwMF4IHLK?p=preview
see the console.logs(); the form is not there on init, but later on after you click 'save' which calls update, it's there.
Ahh, I see. See the first sentence. docs.angularjs.org/api/ng/directive/form . I didn't know that. But I really think that you should be going with directives.
I guess the form is beeing published the first time you're actually submitting that form.
 
2:46 PM
I agree thanks for the help
 

last day (14 days later) »