[英]Testing component without compile nested components in Angular 1.5
I'm currently facing a situation where I have to test a component which contains another component which has its own UT isolated. 我目前面临的情况是,我必须测试一个组件,其中包含另一个具有自己的UT隔离的组件。 Something like this: 像这样:
<parent-component>
<div>
<child-component form-data="formData"></child-component>
</div>
</parent-component>
Before test cases I compile the element passing scope params along with its template: 在测试用例之前,我会编译元素传递范围参数及其模板:
beforeEach(inject((_$q_, _$rootScope_, $compile) => {
$q = _$q_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
makeComponent = (scopeParam, template) => {
const componentTemplate = template || defaultTemplate;
const scope = _.merge($scope, scopeParam);
element = $compile(angular.element(componentTemplate))(scope);
scope.$apply();
};
makeController = (params, template) => {
makeComponent(params, template);
return element.controller(component.name);
};
}));
One of the scope params its referring to formData
which is passed one-binding to child-component
and whenever changes are (on $onInit of parent-component
in fact), $onChanges
of child-component
is fired and listening to this changes causes to call a method which retrieve some data from an endpoint which I'm mocking in its UT respectively. 一个范围的PARAMS其指formData
这是通过一个结合到child-component
,每当变化(上$ OnInit的parent-component
的事实), $onChanges
的child-component
被触发,听了这个变化的原因,以调用一个方法,该方法分别从我正在其UT中模拟的端点检索一些数据。
The thing is I want to avoid the need of rendering this child-component
on parent-component
and replicate the test logic when the sole purpose of UT is to testing component logic separately. 问题是,当UT的唯一目的是分别测试组件逻辑时,我想避免在parent-component
child-component
上呈现此child-component
parent-component
并复制测试逻辑。 How could I do to UT only test this parent component and discard whatever nested component on it? 我该如何做UT仅测试该父组件并丢弃其上的任何嵌套组件?
I answer myself with a solution. 我回答自己一个解决方案。 The idea is to use decorators
to modify a behaviour for an existing class without modifying the code itself. 这个想法是使用decorators
来修改现有类的行为,而不修改代码本身。 Learn more: https://docs.angularjs.org/guide/decorators . 了解更多: https : //docs.angularjs.org/guide/decorators 。 The only thing we need to do is instantiate our mock module and pass $provide
and calling decorator
method with our component name follow with 'Directive' passing at the same time $delegate
which contains the component/service before to register against the provider. 我们唯一需要做的就是实例化我们的模拟模块,并传递$provide
并调用带有我们组件名称的decorator
方法,后面跟着“ Directive”,同时传递$delegate
,其中包含要向提供者注册的组件/服务。 We only need to set a custom template and controller, in this case: emtpy values, and return it. 在这种情况下,我们只需要设置一个自定义模板和控制器:emtpy值,然后将其返回。 Simply as that, the child component will result as an empty template along its empty controller and we can stay focused on testing our parent component isolated. 如此简单,子组件将沿其空控制器生成一个空模板,我们可以继续专注于测试隔离的父组件。
beforeEach(angular.mock.module(module, ($provide) => {
$provide.decorator('myComponent' + 'Directive', ($delegate) => {
const comp = $delegate[0];
comp.template = '';
comp.controller = class {};
return $delegate;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.