Traditionally you had to create DOM elements to test a directive but by shifting our focus to components, writing unit tests got a lot easier using $componentControllerwithin ngMocks. We can now test our component's controller with an easy to use API that does not require us to spin up any DOM elements to do so. In this lesson, we will see how our ES6 tests are transpiled, learn how to test a component using$componentController and talk about how to simulate lifecycle hooks.

Controller:

class CategoriesController {
constructor(CategoriesModel) {
'ngInject'; this.CategoriesModel = CategoriesModel;
} $onInit() {
this.CategoriesModel.getCategories()
.then(result => this.categories = result);
} onCategorySelected(category) {
this.CategoriesModel.setCurrentCategory(category);
} isCurrentCategory(category) {
return this.CategoriesModel.getCurrentCategory() &&
this.CategoriesModel.getCurrentCategory().id === category.id;
}
} export default CategoriesController;

Test:

import CategoriesModule from './categories';
import CategoriesController from './categories.controller';
import CategoriesComponent from './categories.component';
import CategoriesTemplate from './categories.html'; describe('Categories', () => {
let component, $componentController, CategoriesModel; beforeEach(() => {
window.module('categories'); window.module($provide => {
$provide.value('CategoriesModel', {
getCategories: () => {
return {
then: () => {}
};
}
});
});
}); beforeEach(inject((_$componentController_, _CategoriesModel_) => {
CategoriesModel = _CategoriesModel_;
$componentController = _$componentController_;
})); describe('Module', () => {
it('is named correctly', () => {
expect(CategoriesModule.name).toEqual('categories');
});
}); describe('Controller', () => {
it('calls CategoriesModel.getCategories immediately', () => {
spyOn(CategoriesModel, 'getCategories').and.callThrough(); component = $componentController('categories', {
CategoriesModel
});
component.$onInit(); expect(CategoriesModel.getCategories).toHaveBeenCalled();
});
});

[AngularJS] Test an Angular Component with $componentController的更多相关文章

  1. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  2. Angular开发实践(三):剖析Angular Component

    Web Component 在介绍Angular Component之前,我们先简单了解下W3C Web Components 定义 W3C为统一组件化标准方式,提出Web Component的标准. ...

  3. 【js类库AngularJs】解决angular+springmvc的post提交问题

    [js类库AngularJs]解决angular+springmvc的post提交问题 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前 ...

  4. [AngularJS] Exploring the Angular 1.5 .component() method

    Angualr 1.4: .directive('counter', function counter() { return { scope: {}, restrict: 'EA', transclu ...

  5. [AngularJS] Lazy loading Angular modules with ocLazyLoad

    With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...

  6. [AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled

    By default, Angular provides a lot of debug information on the DOM that's only necessary for tools l ...

  7. [AngularJS] New in Angular 1.5 ng-animate-swap

    <!DOCTYPE html> <html ng-app="MyApplication"> <head> <link rel=" ...

  8. AngularJS进阶(五)Angular实现下拉菜单多选

    Angular实现下拉菜单多选 写这篇文章时,引用文章地址如下: http://ngmodules.org/modules/angularjs-dropdown-multiselect http:// ...

  9. AngularJS进阶(四)ANGULAR.JS实现下拉菜单单选

    ANGULAR.JS: NG-SELECT AND NG-OPTIONS PS:其实看英文文档比看中文文档更容易理解,前提是你的英语基础还可以.英文文档对于知识点讲述简明扼要,通俗易懂,而有些中文文档 ...

随机推荐

  1. [Everyday Mathematics]20150205

    设 $\phi:[k_0,\infty)\to[0,\infty)$ 是有界递减函数, 并且 $$\bex \phi(k)\leq \sex{\frac{A}{h-k}}^\al\phi(h)^\be ...

  2. Easy steps to create a System Tray Application with C# z

    Hiding the C# application to the system tray is quiet straight forward. With a few line of codes in ...

  3. ie对行高line-height的诡异解释

    切 游戏页面真地是要求太精细了,做按钮的时候我犯了一个错误,居然用span的内联元素的行高和padding来控制,虽然有很多好处,但是IE对 line-height的解释导致按钮经常下边会缺一小部分, ...

  4. bjfu1252 贪心

    题目意思是给出一些开区间,这些区间有的相交,有的不相交,问你能否选出一些区间,使这些区间之间都不相交,并且选出的区间数最大. 这是个典型的贪心问题了.按区间的结束位置排序,然后顺序地选取区间,只要当前 ...

  5. linux中的livecd、liveDVD和其他安装方式简介

    下载了几种不同格式的centos版本的iso文件,从而对比下各种iso文件的差别,下载的内容如下: 下载之后,分别在虚拟机中进行安装,从而查看有何区别: 1. 使用LiveCD进行安装 在选择安装介质 ...

  6. java易混淆概念之类变量、成员变量、局部变量

      类变量.成员变量.局部变量 类变量(也叫静态变量)是类中独立于方法之外的变量,用static 修饰.(static表示“全局的”.“静态的”,用来修饰成员变量和成员方法,或静态代码块(静态代码块独 ...

  7. json字符串转换为JSONObject和JSONArray

    一.下载json 具体到http://www.json.org/上找java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了 二.具体转化过程 //JSONObject String ...

  8. 如何用chrome修改js代码,跳过网站等待时间

    用chrome修改js代码 By Z.H. Fu 切问录 [maplewizard.github.io](http://maplewizard.github.io ) 网页中大部分的限制都是由js编写 ...

  9. UIButton 在 iOS7.0与iOS7.1 中关于enabled的一点区别

    前些日子,在一台iOS7.0的设备上进行调试,关于UIButton的一部分代码如下 1 self.btn_loadmore.enabled = NO; 2 [self.btn_loadmore set ...

  10. AHOI2013 Round2 Day1 简要题解

    第一题,好吧这是个dp.(搜素也能在BZOJ上卡过). 第二题,BFS搜索碰到的立方体面数,智硬没有想到... 第三题,其实一看就有思路,但关键是求x坐标不交的矩形对数+y坐标不交的矩形对数 - x, ...