简体   繁体   中英

Testing Kendo Grid command

I have a KendoUI Grid with one of the columns being a command.

$("#table").kendoGrid({
    columns: [{
        command: { text: $scope.open, click: $scope.openItem},
        title: $scope.link,
        field: "Id
    }]

The command is bound to an angularJS scope function that looks like this

$scope.openItem= function (e) {
    e.preventDefault();

    var row = $(e.currentTarget).closest("tr");

    var dataItem = this.dataItem(row);

    var id = dataItem.Id;
    var evalId = dataItem.EvaluationId;

    performeAction(id, evalId);
};

We are using karma to do our testing and I hit a roadblock with this 'openItem' function.

This is how the test looks at the moment

it("Button testing", inject(function ($compile){
     var object = {};
     element = angular.element('<myAngularDirective></myAngularDirective>');
     $compile(element)(scope);
     scope.$digest();

     //element.scope().openSelectedInstantCoach(object);
}));

The part that's causing problems is the commented line. The three lines above work fine for the other tests. I don't really know what I need to pass into the function because of the way kendoUI handles the click.

Any ideas?

Do you really need to simulate how Kendo UI handles "click" on commands? Maybe it is easier simulating the click and let Kendo UI generate the parameters.

Example: grid is the KendoUI Grid object initialized as:

var grid = $("#grid").kendoGrid({
    ...,
    columns   : [
        { command : { text: "open", click: myClick }, title: "Cmd" },
        ...
    ]

}).data("kendoGrid");

When you want to click the open command in row 0 , you can do it as:

var btn = $("tr:nth(0) .k-grid-open", grid.tbody);
btn.trigger("click");

I'm finding the row using tr:nth(0) and the target button in the specific row using k-grid-open CSS class (this is something that KendoUI generates from the text of your button).

Now, triggering the button is as simple as invoking trigger("click") .

You can see an example here : http://jsfiddle.net/OnaBai/5APDW/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM