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. 本博客迁移到Github,之后停止更新

    本博客之后停止更新或者更新不是很及时,关注博客请移步   http://waylife.github.io 或者 http://blog.13kbook.com   谢谢支持. Update Time ...

  2. Docker管理面板Crane开源了!

    导读 数人云容器管理面板 Crane 开源啦!Crane 包含着数人云工程师对 Docker 最新技术的热爱和实践.希望借助开源社区的力量,让 Crane 完善自身,更好地成长起来,让更多的国内用户体 ...

  3. IOS UIActivityIndicatorView 等待指示器

    自己做的一个 等待指示器 #import <UIKit/UIKit.h> @interface RockIndicatorView : UIView { } @property(nonat ...

  4. Int.Parse()、Convert.toInt32()和(int)区别

    通过网上的查询从而了解了Int.Parse().Convert.toInt32()和(int)区别. 一.定义上的差别 int类型表示一种整型,.NET Framework 类型为 System.In ...

  5. linux下无法安装VMware的解决方法

    在Reahat下安装VMware-Player-6.0.1-1379776.x86_64.bundle,结果却提示 Extracting VMware Installer...done.NOT_REA ...

  6. 生成chm文档工具- Sandcastle -摘自网络

    Sandcastle是微软官方的文档生成工具,NDoc开发停止后,这个貌似也是唯一的一个这方面的工具.它从dll文件及其xml注释文件能够 生成完整的帮助文档,支持多种生成格式(Helpe1x:chm ...

  7. homework-02 最大子区域和

    题目描述 题目建立上一个作业的题目基础上,上一次作业是要求在一个一维序列里找一个最大连续子串,这次task最基础的要求是在一个二维表里找一个最大连续子矩形,但是这次作业有若干个升级版,分别要求可以加入 ...

  8. PC问题-可以PING通IP,PING名字不通,可以远程,但不能访问共享文件夹?

    问题现象:可以PING通IP,PING名字不通,可以远程,但不能访问共享文件夹? 问题原因:目标主机中NetLogon服务未启动. 问题处理:远程打开目标主机,“我的电脑-管理-服务-启动(改为自动) ...

  9. maven系列(1)-maven的介绍与安装

    maven的介绍 maven是大名鼎鼎的Apache下的java构建工具. Apache Maven is a software project management and comprehensio ...

  10. jquery easyui中的formatter多用法

    1.formatter能多数据进行格式化后输出,formatter必须返回一个字符串,第一个用法:当一个单元格的数据很多,不能显示完全时,鼠标放上去能动态显示出所有内容. formatter:func ...