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. C#中常见的winform控件命名规范

    我们知道Button 常常简称为btn,那么Winform中的其它控件呢,这篇文章在C#的winform控件命名规范 的基础上对一些控件的名称的简称进行了整理. 1. 标准控件 NO. 控件类型简写 ...

  2. memmove函数

    写一个函数,完成内存之间的拷贝 void* mymemcpy( void *dest, const void *src, size_t count ) { char* pdest = static_c ...

  3. QT-【转】基础(略)

    第0篇 开始学习Qt 与Qt Creator 第1篇 基础(一)Qt开发环境的搭建和hello world 第2篇 基础(二)编写Qt多窗口程序 第3篇 基础(三)Qt登录对话框 第4篇 基础(四)添 ...

  4. CentOS VPS创建pptpd VPN服务

    原文地址http://www.hi-vps.com/wiki/doku.php?id=xen_vps_centos6_install_pptpd CentOS VPS创建pptpd VPN服务 Xen ...

  5. Java IO流中的File类学习总结

    一.File类概述 File类位于java.io包中,是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹. File类有多种重载的构造方法.File类保存文件或目录的各种 ...

  6. 对ArrayList 进行深拷贝

    ArrayList arr = new ArrayList(); arr.Add()); arr.Add()); arr.Add()); ArrayList arr2 = new ArrayList( ...

  7. 第三百零二天 how can I 坚持

    今天给掌中宝提了几个bug,确实管用,哈哈. 还有就是弟弟买房了,海亮艺术公馆,还好,至少安定下来了,可惜啊,我看好的房子也有的卖了,咋办啊. 看准的东西总是会想法设法的买了,可是无能为力啊. 还有, ...

  8. Java邮件服务学习之四:邮箱服务客户端Spring Mail

    一.Spring Mail API Spring邮件抽象层的主要包为org.springframework.mail,Spring提供的邮件发送不仅支持简单邮件的发送.添加附件. 1.邮件发送的核心接 ...

  9. HDU 5783 Divide the Sequence (贪心)

    Divide the Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...

  10. MSGPACK(一)

    MSGPACK跨平台的数据序列规范,为多种语言所支持.用它序列还是还原数据都异常方便. 而且它支持序列的数据格式非常之多,因为它支持的数据格式多,所以MSGPACK的第二功用:缓存. DELPHI的M ...