angularjs - Angular 1.5 unit test controller that requires parent component's controller -
i'm trying unit test (child) controller of angularjs 1.5 (with webpack) component requires parent component , controller module.
child controller structure:
function childcontroller () { var vm = this; vm.searchtext = ''; vm.submit = function() { var data = {}; data['srch'] = vm.searchtext; vm.parentctrl.submittextsearch(data); }; } module.exports = childcontroller;
child component:
var template = require('./child.html'); var controller = require('./child.controller'); var childcomponent = { require: { parentctrl: '^parent' }, template: template, controller: controller, controlleras: 'vm' }; module.exports = childcomponent;
so mock out parentctrl that's required in childcontroller's submit()-function. i've been unable find how this. i've found similar child-parent directive solutions , tried those, e.g. injecting parent controller through fake html-element described in this child-parent directive example , same stackoverflow solutions no results. problems differ @ least in fact child , parent controller in different modules. , suppose scope-tricks not angular 1.5-style?
the skeleton of jasmine test without failed mock attempts:
describe('child component', function() { describe('child controller', function() { var controller; beforeeach(angular.mock.module('child')); beforeeach(inject(function(_$componentcontroller_) { controller = _$componentcontroller_('child'); })) it('should work', function() { controller.searchtext = "test"; controller.submit(); }) }) })
that results in typeerror: cannot read property 'submittextsearch' of undefined
. should mock parent controller out? limited experience in angular, i'm out of ideas.
in case you're add parentctrl
dependency of component in order test have mock parent component , assign controller. you'd need like:
beforeeach(inject(function(_$componentcontroller_) { controller = _$componentcontroller_('child'); parentctrl = _$componentcontroller_('parent'); controller.parentctrl = parentctrl; }))
Comments
Post a Comment