zoukankan      html  css  js  c++  java
  • [AngularJS + Unit Testing] Testing a component with requiring ngModel

    The component test:

    describe('The component test', () => {
        let component, $componentController, $controller, $injector, $scope;
    
        beforeEach(module("componennts.module"));
        beforeEach(inject((_$componentController_, _$controller_, _$injector_, _$rootScope_) => {
            $componentController = _$componentController_;
            $controller = _$controller_;
            $injector = _$injector_;
            $scope = _$rootScope_.$new();
        }));
    
        describe('Controller', () => {
            it('should have ng-model with the correct binding', () => {    
                let locals = {
                    $scope: $scope,
                    $element: angular.element('<my-component ng-model="value"></my-component>'),
                    $attrs: { ngModel: 'value' }
                };
                locals.$scope.value = [1];
                let ngModelController = $injector.get('ngModelDirective')[0].controller;
                let ngModelInstance = $controller(ngModelController, locals);
                $scope.$digest();
                component = $componentController('myComponent', null, { ngModel: ngModelInstance });
                component.$onInit();
                expect(component).toBeDefined();
                expect(component._selectedValues).toEqual([1]); // _selectedValues = ngModel.$viewValue
            });
        });
    });
  • 相关阅读:
    SQL分类
    广度/深度优先生成树
    图的基本概念
    哈夫曼树构造/哈夫曼编码
    二叉排序树/平衡二叉树
    树、森林与二叉树的转换
    树/二叉树的基本性质
    /*传说中的土办法找中序前驱*/
    KiCAD原理图更换库
    博客园添加版权信息
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9958978.html
Copyright © 2011-2022 走看看