Struggling to access my directive's scope for a unit test. Running into compile errors when trying to execute the unit test.
The application compiles (using gulp) and runs smoothly, and I am able to successfully unit test non-directives. However, testing directives has been challenging as I am experimenting with various solutions from other sources and making educated guesses.
Main page's HTML and JS
<div company-modal="companyOptions" show-close="false"></div>
.
(function() {
'use strict';
angular
.module('app')
.controller('companyModalCtrl', ['$scope', selectionPage]);
function selectionPage($scope) {
$scope.companyOptions = {};
}
})();
This is just the initial part of my directive (as it's quite extensive so including only the crucial first section).
(function() {
'use strict';
angular
.module('app')
.directive('companyModal',
companyModal
);
function companyModal() {
return {
restrict: 'A',
replace: false,
transclude: true,
templateUrl: '/src/login/company.html',
scope: {
options: '=companyModal',
showClose: '='
},
bindToController: true,
controller: companySelection,
controllerAs: 'csmvm'
};
}
companySelection.$inject = ['$state'];
function companySelection($state) {
var csmvm = this;
var url;
csmvm.emailList = [];
Here is my Unit Test attempt
'use strict';
describe('Company', function () {
var scope;
var controller;
var elem;
beforeEach(module('app'));
beforeEach(inject(function ($controller, $rootScope, $compile) {
scope = $rootScope.$new();
elem = angular.element('<div company-modal="companyOptions" show-close="false"></div>');
$compile(elem)($rootScope);
controller = $controller(elem.controller('companyModal as csmvm'), {
$scope: scope
});
scope.csmvm.emailList.email = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cdef3fedcfbf1fdf5f0b2fff3f1">[email protected]</a>";
}));
describe('Invite', function () {
it('should be an array for emailList', function () {
expect(scope.csmvm.emailList).to.be.an('array');
});
});
});
The issue I am encountering (apologies for the lack of detail) is that I consistently receive runtime errors during the test. For example:
Failed Tests:
Company
"before each" hook: workFn
18) the object { "message": "'undefined' is not an object (evaluating '(isArray(Type) : Type).prototype)".
Once again, my app compiles (gulp) without any issues and functions well, and I can successfully unit test non-directive components.