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. [译]LINT TO SQL 介绍(数据库查询) - Part.3

    出处:Linq To Sql (Part.3 – Querying our database) 术语表 Built-in:内置的 Clause:子句 Debugger:调试器 Object Relat ...

  2. 解决:Unable to connect to repository https://dl-ssl.google.com/android/eclipse/site.xml

    ailed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connection ...

  3. Redis入门教程:特性及数据类型的操作

    虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面是一个比较系统的Redis介绍,对Redis的特性及各种数据类型及操作进行了介绍.是一个很不错的Redis入门 ...

  4. leveldb源码笔记

    关于KV数据库leveldb的介绍,网上已经太多了,这里只是自己再学习源码过程中,整理的笔记,磁盘存储和内存存储的结构用了伪代码表示出来了,首先是内存中存储结构,然后是log文件存储结构和磁盘数据ss ...

  5. 《Python CookBook2》 第四章 Python技巧 对象拷贝 && 通过列表推导构建列表

    (先学第四章) 对象拷贝 任务: Python通常只是使用指向原对象的引用,并不是真正的拷贝. 解决方案: >>> a = [1,2,3] >>> import c ...

  6. 几种常见的FTP软件的二进制设置说明

    几种常见的FTP软件的二进制设置说明: 1.FlashFXP: 打开 FlashFXP:在工具栏中,选项 => 参数(也可以直接按F6键),在弹出来的窗口中,选择“传输(T)”卡,在传输模式中选 ...

  7. 如何从ST网站找到对应的固件库

    ST官方网站改版后,基本上很难搜索到固件库的地址,找了半天才找到固件库的下载地址,通过此方法可以找到其他需要的资源,故记下来方便大家. 下载的网站地址为: Home>Tools and Soft ...

  8. 解决YUM无法正常工作

    1. 错误发生背景 在进行安装依赖包的时候,能够在YUM源中找到相关的RPM包,但是无法进行下载,在单独进行安装RPM包的时候能够进行安装,报错截图如下: 具体的报错信息如下: Error Downl ...

  9. redo文件四

    v$session_wait 用来查询redo buffer的空间信息 select sid,event,seconds_in_wait,state from v$session_wait where ...

  10. LeetCode题解——Roman to Integer

    题目: 将罗马数字转换为整数. 解法: 可以参考上一篇数字转换为罗马数字的规则. 代码: class Solution { public: int sym2int(char sym) //罗马数字字符 ...