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语言利用pcre正则表达式库

    C语言使用正则表达式,可以利用pcre库,这个比较不错的哦. 在使用过程中,利用python进行测试正则表达式是否OK,后发现出现了问题.如下所示: regex.c:11:18: warning: u ...

  2. SSH 连接慢的解决方案详解

    SSH 连接慢的解决方案详解 http://www.codeceo.com/article/ssh-slow.html

  3. Robotium简要学习

    Robotium是一款国外的Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手势操作(点击.长按.滑动等).查找和断言机制的API,能够对各种控件进行 ...

  4. 怎么用PHP在HTML中生成PDF文件

    原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...

  5. PHP的MySQL扩展:MySQL数据库概述

    来源:http://www.ido321.com/1023.html 一.SQL:结构化查询语言 SQL(Structured Query Language)是高级的非过程化变成语言,专门用于查询和修 ...

  6. [LeetCod] Single Number

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  7. Hello, Github Blog

    hello, I am using github to write a post, I am so exciting- 原文地址: http://vblog.vell001.ml/2014/03/08 ...

  8. 迁移web.py项目至git@osc的项目演示平台

    1. 开启演示平台 选择WSGI,输入应用名称,即是演示网页的网址. 2. web.py代码迁移 将Python的site-packages目录下的web文件夹复制到代码目录下,与网页程序在同一个文件 ...

  9. 第二百四十六天 how can I 坚持

    领悟啊.好伤心啊. 到底应该是怎样的一个过程,才能得到想要的结果啊. 我不懂我自己.. 睡觉吧. 中午吃的米线. 好渴,晚上没喝水呢,活该.谁让你一直玩游戏. 睡觉了.弟弟回家了,过两天去烟台待一个月 ...

  10. homework-03

    1.分工准备 这次的工作是结对编程,在第二次作业中我是使用python完成的作业,而小明是使用C完成的作业.因为打算使用动态链接库的方式将第二次的代码嵌入到本次的作业中,而python生成动态链接库不 ...