单元测试KendoUI网格对象

时间:2019-07-06 03:44:49

标签: javascript angularjs typescript jasmine kendo-grid

我正在编写单元测试,我需要能够模拟kendo网格对象。

我已经使用茉莉花框架编写了以下单元测试:

describe("Asset Configuration Grid", () => {

        var ctrl: AssetConfigurationGridController;
        var $http: ng.IHttpService;
        var $state: ng.ui.IStateService;
        var $jsonBlobService: JsonBlobsService;

        var ctrl: AssetConfigurationGridController;

        beforeEach(() => {
            ctrl = new AssetConfigurationGridController($http, $state, $jsonBlobService);

        });

        describe("Test search for active components", () => {
            it("Should show only the active components only", () => {
                let gridOptions = ctrl.savedAssetOptions(true);
                ctrl.setActiveTab('Active');
                ctrl.searchQuery = 'someString';
            });
        });    
    });

这是我的剑道网格的示例html:

<md-tab>
                <md-tab-title ng-click="$ctrl.setActiveTab('active')">
                    Active
                </md-tab-title>
                <md-tab-body>
                    <div kendo-grid="$ctrl.activeAssetsGrid" k-options="$ctrl.savedAssetOptions(true)">
                    </div>
                </md-tab-body>
            </md-tab>

基于上面的代码,我想编写一个单元测试来模拟以下对象:

<div kendo-grid="$ctrl.activeAssetsGrid" k-options="$ctrl.savedAssetOptions(true)">

这是我的控制器代码:

    export class AssetConfigurationGridController {
        private _searchQuery: string;
        private activeAssetsGrid: kendo.ui.Grid;
        private inactiveAssetsGrid: kendo.ui.Grid;
        private incompleteAssetsGrid: kendo.ui.Grid;
        private activeTab: string = 'active';

        static $inject = ['$http', '$state', 'jsonBlobsService'];

        constructor(private http: ng.IHttpService, private state: ng.ui.IStateService, private jsonBlobsService: JsonBlobsService) {
        }

        get searchQuery(): string {
            return this._searchQuery;
        }

        set searchQuery(value: string) {
            this._searchQuery = value;

            if (this.activeTab === 'active') {
                if (this.activeAssetsGrid) {
                    this.activeAssetsGrid.dataSource.fetch();
                }
            } else if (this.activeTab === 'inactive') {
                if (this.inactiveAssetsGrid) {
                    this.inactiveAssetsGrid.dataSource.fetch();
                }
            } else if (this.activeTab === 'incomplete') {
                if (this.incompleteAssetsGrid) {
                    this.incompleteAssetsGrid.dataSource.fetch();
                }
            }
        }

        setActiveTab(value: string) {
            this.activeTab = value;
            this.searchQuery = "";
        }     

        private baseGridOptions: kendo.ui.GridOptions = {
            pageable: true,
            sortable: true,
            groupable: false,
            selectable: false,
            dataSource: {

            }
        };

        savedAssetOptions(active: boolean): kendo.ui.GridOptions {
            //Some code here
        }  

        get incompleteResourceOptions(): kendo.ui.GridOptions {
            //Some code here
        }

        edit(id: number) {
        }

        draft(id: number) {
        }
    }

预期结果将是模拟剑道网格对象。

0 个答案:

没有答案