zoukankan      html  css  js  c++  java
  • [AngularJS] Test an Angular Component with $componentController

    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();
        });
      });
  • 相关阅读:
    ORACLE EBS中查看某个Request的Output File
    如何查看非自己提交的请求的结果
    ORACLE EBS中OAF屏蔽的错误
    对OAF开发中的MDS的初步研究(转)
    MapGuide应用最佳实践资源库Repository的维护
    MapGuide OpenSource 2.1在Windows 7上运行
    MapGuide应用最佳实践采用托管(Managed)资源还是非托管(Unmanaged)资源
    MapGuide Open Source 2.1 正式发布
    Autodesk 2009开发者日现在开始报名
    支持Windows 7的CAD—AutoCAD Civil 3D 2010
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5854379.html
Copyright © 2011-2022 走看看