How to use grid method in wpt

Best JavaScript code snippet using wpt

jsgrid.tests.js

Source:jsgrid.tests.js Github

copy

Full Screen

1$(function() {2 var Grid = jsGrid.Grid,3 JSGRID = "JSGrid",4 JSGRID_DATA_KEY = JSGRID;5 Grid.prototype.updateOnResize = false;6 module("basic");7 test("default creation", function() {8 var gridOptions = {9 simpleOption: "test",10 complexOption: {11 a: "subtest",12 b: 1,13 c: {}14 }15 },16 grid = new Grid("#jsGrid", gridOptions);17 equal(grid._container[0], $("#jsGrid")[0], "container saved");18 equal(grid.simpleOption, "test", "primitive option extended");19 equal(grid.complexOption, gridOptions.complexOption, "non-primitive option extended");20 });21 test("jquery adapter creation", function() {22 var gridOptions = {23 option: "test"24 },25 $element = $("#jsGrid"),26 result = $element.jsGrid(gridOptions),27 grid = $element.data(JSGRID_DATA_KEY);28 equal(result, $element, "jquery fn returned source jQueryElement");29 ok(grid instanceof Grid, "jsGrid saved to jquery data");30 equal(grid.option, "test", "options provided");31 });32 test("destroy", function() {33 var $element = $("#jsGrid"),34 grid;35 $element.jsGrid({});36 grid = $element.data(JSGRID_DATA_KEY);37 grid.destroy();38 strictEqual($element.html(), "", "content is removed");39 strictEqual($element.data(JSGRID_DATA_KEY), undefined, "jquery data is removed");40 });41 test("jquery adapter second call changes option value", function() {42 var $element = $("#jsGrid"),43 gridOptions = {44 option: "test"45 },46 grid;47 $element.jsGrid(gridOptions);48 grid = $element.data(JSGRID_DATA_KEY);49 gridOptions.option = "new test";50 $element.jsGrid(gridOptions);51 equal(grid, $element.data(JSGRID_DATA_KEY), "instance was not changed");52 equal(grid.option, "new test", "option changed");53 });54 test("jquery adapter invokes jsGrid method", function() {55 var methodResult = "",56 $element = $("#jsGrid"),57 gridOptions = {58 method: function(str) {59 methodResult = "test_" + str;60 }61 };62 $element.jsGrid(gridOptions);63 $element.jsGrid("method", "invoke");64 equal(methodResult, "test_invoke", "method invoked");65 });66 test("onInit callback", function() {67 var $element = $("#jsGrid"),68 onInitArguments,69 gridOptions = {70 onInit: function(args) {71 onInitArguments = args;72 }73 };74 var grid = new Grid($element, gridOptions);75 equal(onInitArguments.grid, grid, "grid instance is provided in onInit callback arguments");76 });77 test("controller methods are $.noop when not specified", function() {78 var $element = $("#jsGrid"),79 gridOptions = {80 controller: {}81 },82 testOption;83 $element.jsGrid(gridOptions);84 deepEqual($element.data(JSGRID_DATA_KEY)._controller, {85 loadData: $.noop,86 insertItem: $.noop,87 updateItem: $.noop,88 deleteItem: $.noop89 }, "controller has stub methods");90 });91 test("option method", function() {92 var $element = $("#jsGrid"),93 gridOptions = {94 test: "value"95 },96 testOption;97 $element.jsGrid(gridOptions);98 testOption = $element.jsGrid("option", "test");99 equal(testOption, "value", "read option value");100 $element.jsGrid("option", "test", "new_value");101 testOption = $element.jsGrid("option", "test");102 equal(testOption, "new_value", "set option value");103 });104 test("fieldOption method", function() {105 var dataLoadedCount = 0;106 var $element = $("#jsGrid"),107 gridOptions = {108 loadMessage: "",109 autoload: true,110 controller: {111 loadData: function() {112 dataLoadedCount++;113 return [{ prop1: "value1", prop2: "value2", prop3: "value3" }];114 }115 },116 fields: [117 { name: "prop1", title: "_" }118 ]119 };120 $element.jsGrid(gridOptions);121 var fieldOptionValue = $element.jsGrid("fieldOption", "prop1", "name");122 equal(fieldOptionValue, "prop1", "read field option");123 $element.jsGrid("fieldOption", "prop1", "name", "prop2");124 equal($element.text(), "_value2", "set field option by field name");125 equal(dataLoadedCount, 1, "data not reloaded on field option change");126 $element.jsGrid("fieldOption", 0, "name", "prop3");127 equal($element.text(), "_value3", "set field option by field index");128 });129 test("option changing event handlers", function() {130 var $element = $("#jsGrid"),131 optionChangingEventArgs,132 optionChangedEventArgs,133 gridOptions = {134 test: "testValue",135 another: "anotherValue",136 onOptionChanging: function(e) {137 optionChangingEventArgs = $.extend({}, e);138 e.option = "another";139 e.newValue = e.newValue + "_" + this.another;140 },141 onOptionChanged: function(e) {142 optionChangedEventArgs = $.extend({}, e);143 }144 },145 anotherOption;146 $element.jsGrid(gridOptions);147 $element.jsGrid("option", "test", "newTestValue");148 equal(optionChangingEventArgs.option, "test", "option name is provided in args of optionChanging");149 equal(optionChangingEventArgs.oldValue, "testValue", "old option value is provided in args of optionChanging");150 equal(optionChangingEventArgs.newValue, "newTestValue", "new option value is provided in args of optionChanging");151 anotherOption = $element.jsGrid("option", "another");152 equal(anotherOption, "newTestValue_anotherValue", "option changing handler changed option and value");153 equal(optionChangedEventArgs.option, "another", "option name is provided in args of optionChanged");154 equal(optionChangedEventArgs.value, "newTestValue_anotherValue", "option value is provided in args of optionChanged");155 });156 test("common layout rendering", function() {157 var $element = $("#jsGrid"),158 grid = new Grid($element, {}),159 $headerGrid,160 $headerGridTable,161 $bodyGrid,162 $bodyGridTable;163 ok($element.hasClass(grid.containerClass), "container class attached");164 ok($element.children().eq(0).hasClass(grid.gridHeaderClass), "grid header");165 ok($element.children().eq(1).hasClass(grid.gridBodyClass), "grid body");166 ok($element.children().eq(2).hasClass(grid.pagerContainerClass), "pager container");167 $headerGrid = $element.children().eq(0);168 $headerGridTable = $headerGrid.children().first();169 ok($headerGridTable.hasClass(grid.tableClass), "header table");170 equal($headerGrid.find("." + grid.headerRowClass).length, 1, "header row");171 equal($headerGrid.find("." + grid.filterRowClass).length, 1, "filter row");172 equal($headerGrid.find("." + grid.insertRowClass).length, 1, "insert row");173 ok(grid._headerRow.hasClass(grid.headerRowClass), "header row class");174 ok(grid._filterRow.hasClass(grid.filterRowClass), "filter row class");175 ok(grid._insertRow.hasClass(grid.insertRowClass), "insert row class");176 $bodyGrid = $element.children().eq(1);177 $bodyGridTable = $bodyGrid.children().first();178 ok($bodyGridTable.hasClass(grid.tableClass), "body table");179 equal(grid._content.parent()[0], $bodyGridTable[0], "content is tbody in body table");180 equal($bodyGridTable.find("." + grid.noDataRowClass).length, 1, "no data row");181 equal($bodyGridTable.text(), grid.noDataContent, "no data text");182 });183 test("set default options with setDefaults", function() {184 jsGrid.setDefaults({185 defaultOption: "test"186 });187 var $element = $("#jsGrid").jsGrid({});188 equal($element.jsGrid("option", "defaultOption"), "test", "default option set");189 });190 module("loading");191 test("loading with controller", function() {192 var $element = $("#jsGrid"),193 data = [194 { test: "test1" },195 { test: "test2" }196 ],197 gridOptions = {198 controller: {199 loadData: function() {200 return data;201 }202 }203 },204 grid = new Grid($element, gridOptions);205 grid.loadData();206 equal(grid.option("data"), data, "loadData loads data");207 });208 test("loadData throws exception when controller method not found", function() {209 var $element = $("#jsGrid");210 var grid = new Grid($element);211 grid._controller = {};212 throws(function() {213 grid.loadData();214 }, /loadData/, "loadData threw an exception");215 });216 test("onError event should be fired on controller fail", function() {217 var errorArgs,218 errorFired = 0,219 $element = $("#jsGrid"),220 gridOptions = {221 controller: {222 loadData: function() {223 return $.Deferred().reject({ value: 1 }, "test").promise();224 }225 },226 onError: function(args) {227 errorFired++;228 errorArgs = args;229 }230 },231 grid = new Grid($element, gridOptions);232 grid.loadData();233 equal(errorFired, 1, "onError handler fired");234 deepEqual(errorArgs, { grid: grid, args: [{ value: 1 }, "test"] }, "error has correct params");235 });236 asyncTest("autoload should call loadData after render", 1, function() {237 new Grid($("#jsGrid"), {238 autoload: true,239 controller: {240 loadData: function() {241 ok(true, "autoload calls loadData on creation");242 start();243 return [];244 }245 }246 });247 });248 test("loading filtered data", function() {249 var filteredData,250 loadingArgs,251 loadedArgs,252 $element = $("#jsGrid"),253 data = [254 { field: "test" },255 { field: "test_another" },256 { field: "test_another" },257 { field: "test" }258 ],259 gridOptions = {260 filtering: true,261 fields: [262 {263 name: "field",264 filterValue: function(value) {265 return "test";266 }267 }268 ],269 onDataLoading: function(e) {270 loadingArgs = $.extend(true, {}, e);271 },272 onDataLoaded: function(e) {273 loadedArgs = $.extend(true, {}, e);274 },275 controller: {276 loadData: function(filter) {277 filteredData = $.grep(data, function(item) {278 return item.field === filter.field;279 });280 return filteredData;281 }282 }283 },284 grid = new Grid($element, gridOptions);285 grid.loadData();286 equal(loadingArgs.filter.field, "test");287 equal(grid.option("data").length, 2, "filtered data loaded");288 deepEqual(loadedArgs.data, filteredData);289 });290 asyncTest("loading indication", function() {291 var timeout = 10,292 stage = "initial",293 $element = $("#jsGrid"),294 gridOptions = {295 loadIndication: true,296 loadIndicationDelay: timeout,297 loadMessage: "loading...",298 loadIndicator: function(config) {299 equal(config.message, gridOptions.loadMessage, "message provided");300 ok(config.container.jquery, "grid container is provided");301 return {302 show: function() {303 stage = "started";304 },305 hide: function() {306 stage = "finished";307 }308 };309 },310 fields: [311 { name: "field" }312 ],313 controller: {314 loadData: function() {315 var deferred = $.Deferred();316 equal(stage, "initial", "initial stage");317 setTimeout(function() {318 equal(stage, "started", "loading started");319 deferred.resolve([]);320 equal(stage, "finished", "loading finished");321 start();322 }, timeout);323 return deferred.promise();324 }325 }326 },327 grid = new Grid($element, gridOptions);328 grid.loadData();329 });330 asyncTest("loadingIndication=false should not show loading", 0, function() {331 var $element = $("#jsGrid"),332 timeout = 10,333 gridOptions = {334 loadIndication: false,335 loadIndicationDelay: timeout,336 loadIndicator: function() {337 return {338 show: function() {339 ok(false, "should not call show");340 },341 hide: function() {342 ok(false, "should not call hide");343 }344 };345 },346 fields: [347 { name: "field" }348 ],349 controller: {350 loadData: function() {351 var deferred = $.Deferred();352 setTimeout(function() {353 deferred.resolve([]);354 start();355 }, timeout);356 return deferred.promise();357 }358 }359 },360 grid = new Grid($element, gridOptions);361 grid.loadData();362 });363 test("search", function() {364 var $element = $("#jsGrid"),365 data = [366 { field: "test" },367 { field: "test_another" },368 { field: "test_another" },369 { field: "test" }370 ],371 gridOptions = {372 pageIndex: 2,373 _sortField: "field",374 _sortOrder: "desc",375 filtering: true,376 fields: [377 {378 name: "field",379 filterValue: function(value) {380 return "test";381 }382 }383 ],384 controller: {385 loadData: function(filter) {386 var filteredData = $.grep(data, function(item) {387 return item.field === filter.field;388 });389 return filteredData;390 }391 }392 },393 grid = new Grid($element, gridOptions);394 grid.search();395 equal(grid.option("data").length, 2, "data filtered");396 strictEqual(grid.option("pageIndex"), 1, "pageIndex reset");397 strictEqual(grid._sortField, null, "sortField reset");398 strictEqual(grid._sortOrder, "asc", "sortOrder reset");399 });400 test("change loadStrategy on the fly", function() {401 var $element = $("#jsGrid");402 var gridOptions = {403 controller: {404 loadData: function() {405 return [];406 }407 }408 };409 var grid = new Grid($element, gridOptions);410 grid.option("loadStrategy", {411 firstDisplayIndex: function() {412 return 0;413 },414 lastDisplayIndex: function() {415 return 1;416 },417 loadParams: function() {418 return [];419 },420 finishLoad: function() {421 grid.option("data", [{}]);422 }423 });424 grid.loadData();425 equal(grid.option("data").length, 1, "new load strategy is applied");426 });427 module("filtering");428 test("filter rendering", function() {429 var $element = $("#jsGrid"),430 gridOptions = {431 filtering: true,432 fields: [433 {434 name: "test",435 align: "right",436 filtercss: "filter-class",437 filterTemplate: function() {438 var result = this.filterControl = $("<input>").attr("type", "text").addClass("filter-input");439 return result;440 }441 }442 ]443 },444 grid = new Grid($element, gridOptions);445 equal(grid._filterRow.find(".filter-class").length, 1, "filtercss class is attached");446 equal(grid._filterRow.find(".filter-input").length, 1, "filter control rendered");447 equal(grid._filterRow.find("." + grid.cellClass).length, 1, "cell class is attached");448 ok(grid._filterRow.find(".filter-class").hasClass("jsgrid-align-right"), "align class is attached");449 ok(grid.fields[0].filterControl.is("input[type=text]"), "filter control saved in field");450 });451 test("filter get/clear", function() {452 var $element = $("#jsGrid"),453 gridOptions = {454 filtering: true,455 controller: {456 loadData: function() {457 return [];458 }459 },460 fields: [461 {462 name: "field",463 filterTemplate: function() {464 return this.filterControl = $("<input>").attr("type", "text");465 },466 filterValue: function() {467 return this.filterControl.val();468 }469 }470 ]471 },472 grid = new Grid($element, gridOptions);473 grid.fields[0].filterControl.val("test");474 deepEqual(grid.getFilter(), { field: "test" }, "get filter");475 grid.clearFilter();476 deepEqual(grid.getFilter(), { field: "" }, "filter cleared");477 equal(grid.fields[0].filterControl.val(), "", "grid field filterControl cleared");478 });479 test("field without filtering", function() {480 var $element = $("#jsGrid"),481 jsGridFieldConfig = {482 filterTemplate: function() {483 var result = this.filterControl = $("<input>").attr("type", "text");484 return result;485 },486 filterValue: function(value) {487 if(!arguments.length) {488 return this.filterControl.val();489 }490 this.filterControl.val(value);491 }492 },493 gridOptions = {494 filtering: true,495 fields: [496 $.extend({}, jsGridFieldConfig, {497 name: "field1",498 filtering: false499 }),500 $.extend({}, jsGridFieldConfig, {501 name: "field2"502 })503 ]504 },505 grid = new Grid($element, gridOptions);506 grid.fields[0].filterControl.val("test1");507 grid.fields[1].filterControl.val("test2");508 deepEqual(grid.getFilter(), { field2: "test2" }, "field with filtering=false is not included in filter");509 });510 test("search with filter", function() {511 var $element = $("#jsGrid"),512 data = [513 { field: "test" },514 { field: "test_another" },515 { field: "test_another" },516 { field: "test" }517 ],518 gridOptions = {519 fields: [520 {521 name: "field"522 }523 ],524 controller: {525 loadData: function(filter) {526 var filteredData = $.grep(data, function(item) {527 return item.field === filter.field;528 });529 return filteredData;530 }531 }532 },533 grid = new Grid($element, gridOptions);534 grid.search({ field: "test" });535 equal(grid.option("data").length, 2, "data filtered");536 });537 test("filtering with static data should not do actual filtering", function() {538 var $element = $("#jsGrid"),539 gridOptions = {540 filtering: true,541 fields: [542 { type: "text", name: "field" }543 ],544 data: [545 { name: "value1" },546 { name: "value2" }547 ]548 },549 grid = new Grid($element, gridOptions);550 grid._filterRow.find("input").val("1");551 grid.search();552 equal(grid.option("data").length, 2, "data is not filtered");553 });554 module("nodatarow");555 test("nodatarow after bind on empty array", function() {556 var $element = $("#jsGrid"),557 gridOptions = {},558 grid = new Grid($element, gridOptions);559 grid.option("data", []);560 equal(grid._content.find("." + grid.noDataRowClass).length, 1, "no data row rendered");561 equal(grid._content.find("." + grid.cellClass).length, 1, "grid cell class attached");562 equal(grid._content.text(), grid.noDataContent, "no data text rendered");563 });564 test("nodatarow customize content", function() {565 var noDataMessage = "NoData Custom Content",566 $element = $("#jsGrid"),567 gridOptions = {568 noDataContent: function() {569 return noDataMessage;570 }571 },572 grid = new Grid($element, gridOptions);573 grid.option("data", []);574 equal(grid._content.find("." + grid.cellClass).length, 1, "grid cell class attached");575 equal(grid._content.text(), noDataMessage, "custom noDataContent");576 });577 module("row rendering", {578 setup: function() {579 this.testData = [580 { id: 1, text: "test1" },581 { id: 2, text: "test2" },582 { id: 3, text: "test3" }583 ];584 }585 });586 test("rows rendered correctly", function() {587 var $element = $("#jsGrid"),588 gridOptions = {589 data: this.testData590 },591 grid = new Grid($element, gridOptions);592 equal(grid._content.children().length, 3, "rows rendered");593 equal(grid._content.find("." + grid.oddRowClass).length, 2, "two odd rows for 3 items");594 equal(grid._content.find("." + grid.evenRowClass).length, 1, "one even row for 3 items");595 });596 test("custom rowClass", function() {597 var $element = $("#jsGrid"),598 gridOptions = {599 data: this.testData,600 rowClass: "custom-row-cls"601 },602 grid = new Grid($element, gridOptions);603 equal(grid._content.find("." + grid.oddRowClass).length, 2);604 equal(grid._content.find("." + grid.evenRowClass).length, 1);605 equal(grid._content.find(".custom-row-cls").length, 3, "custom row class");606 });607 test("custom rowClass callback", function() {608 var $element = $("#jsGrid"),609 gridOptions = {610 data: this.testData,611 rowClass: function(item, index) {612 return item.text;613 }614 },615 grid = new Grid($element, gridOptions);616 equal(grid._content.find("." + grid.oddRowClass).length, 2);617 equal(grid._content.find("." + grid.evenRowClass).length, 1);618 equal(grid._content.find(".test1").length, 1, "custom row class");619 equal(grid._content.find(".test2").length, 1, "custom row class");620 equal(grid._content.find(".test3").length, 1, "custom row class");621 });622 test("rowClick standard handler", function() {623 var $element = $("#jsGrid"),624 $secondRow,625 grid = new Grid($element, { editing: true });626 grid.option("data", this.testData);627 $secondRow = grid._content.find("." + grid.evenRowClass);628 $secondRow.trigger("click", $.Event($secondRow));629 equal(grid._editingRow.get(0), $secondRow.get(0), "clicked row is editingRow");630 });631 test("rowClick handler", function() {632 var rowClickArgs,633 $element = $("#jsGrid"),634 $secondRow,635 gridOptions = {636 rowClick: function(args) {637 rowClickArgs = args;638 }639 },640 grid = new Grid($element, gridOptions);641 grid.option("data", this.testData);642 $secondRow = grid._content.find("." + grid.evenRowClass);643 $secondRow.trigger("click", $.Event($secondRow));644 ok(rowClickArgs.event instanceof jQuery.Event, "jquery event arg");645 equal(rowClickArgs.item, this.testData[1], "item arg");646 equal(rowClickArgs.itemIndex, 1, "itemIndex arg");647 });648 test("row selecting with selectedRowClass", function() {649 var $element = $("#jsGrid"),650 $secondRow,651 gridOptions = {652 selecting: true653 },654 grid = new Grid($element, gridOptions);655 grid.option("data", this.testData);656 $secondRow = grid._content.find("." + grid.evenRowClass);657 $secondRow.trigger("mouseenter", $.Event($secondRow));658 ok($secondRow.hasClass(grid.selectedRowClass), "mouseenter adds selectedRowClass");659 $secondRow.trigger("mouseleave", $.Event($secondRow));660 ok(!$secondRow.hasClass(grid.selectedRowClass), "mouseleave removes selectedRowClass");661 });662 test("no row selecting while selection is disabled", function() {663 var $element = $("#jsGrid"),664 $secondRow,665 gridOptions = {666 selecting: false667 },668 grid = new Grid($element, gridOptions);669 grid.option("data", this.testData);670 $secondRow = grid._content.find("." + grid.evenRowClass);671 $secondRow.trigger("mouseenter", $.Event($secondRow));672 ok(!$secondRow.hasClass(grid.selectedRowClass), "mouseenter doesn't add selectedRowClass");673 });674 test("refreshing and refreshed callbacks", function() {675 var refreshingEventArgs,676 refreshedEventArgs,677 $element = $("#jsGrid"),678 grid = new Grid($element, {});679 grid.onRefreshing = function(e) {680 refreshingEventArgs = e;681 equal(grid._content.find("." + grid.oddRowClass).length, 0, "no items before refresh");682 };683 grid.onRefreshed = function(e) {684 refreshedEventArgs = e;685 equal(grid._content.find("." + grid.oddRowClass).length, 2, "items rendered after refresh");686 };687 grid.option("data", this.testData);688 equal(refreshingEventArgs.grid, grid, "grid provided in args for refreshing event");689 equal(refreshedEventArgs.grid, grid, "grid provided in args for refreshed event");690 equal(grid._content.find("." + grid.oddRowClass).length, 2, "items rendered");691 });692 test("grid fields normalization", function() {693 var CustomField = function(config) {694 $.extend(true, this, config);695 };696 jsGrid.fields.custom = CustomField;697 try {698 var $element = $("#jsGrid"),699 gridOptions = {700 fields: [701 new jsGrid.Field({702 name: "text1",703 title: "title1"704 }),705 {706 name: "text2",707 title: "title2"708 },709 {710 name: "text3",711 type: "custom"712 }713 ]714 },715 grid = new Grid($element, gridOptions);716 var field1 = grid.fields[0];717 ok(field1 instanceof jsGrid.Field);718 equal(field1.name, "text1", "name is set for field");719 equal(field1.title, "title1", "title field");720 var field2 = grid.fields[1];721 ok(field2 instanceof jsGrid.Field);722 equal(field2.name, "text2", "name is set for field");723 equal(field2.title, "title2", "title field");724 var field3 = grid.fields[2];725 ok(field3 instanceof CustomField);726 equal(field3.name, "text3", "name is set for field");727 } finally {728 delete jsGrid.fields.custom;729 }730 });731 test("'0' itemTemplate should be rendered", function() {732 var $element = $("#jsGrid"),733 grid = new Grid($element, {734 data: [{}],735 fields: [736 new jsGrid.Field({ name: "id", itemTemplate: function() { return 0; } })737 ]738 });739 equal(grid._bodyGrid.text(), "0", "item template is rendered");740 });741 test("grid field name used for header if title is not specified", function() {742 var $element = $("#jsGrid"),743 grid = new Grid($element, {744 fields: [745 new jsGrid.Field({ name: "id" })746 ]747 });748 equal(grid._headerRow.text(), "id", "name is rendered in header");749 });750 test("grid fields header and item rendering", function() {751 var $element = $("#jsGrid"),752 $secondRow,753 gridOptions = {754 fields: [755 new jsGrid.Field({756 name: "text",757 title: "title",758 css: "cell-class",759 headercss: "header-class",760 align: "right"761 })762 ]763 },764 grid = new Grid($element, gridOptions);765 grid.option("data", this.testData);766 equal(grid._headerRow.text(), "title", "header rendered");767 equal(grid._headerRow.find("." + grid.headerCellClass).length, 1, "header cell class is attached");768 equal(grid._headerRow.find(".header-class").length, 1, "headercss class is attached");769 ok(grid._headerRow.find(".header-class").hasClass("jsgrid-align-right"), "align class is attached");770 $secondRow = grid._content.find("." + grid.evenRowClass);771 equal($secondRow.text(), "test2", "item rendered");772 equal($secondRow.find(".cell-class").length, 1, "css class added to cell");773 equal($secondRow.find("." + grid.cellClass).length, 1, "cell class is attached");774 ok($secondRow.find(".cell-class").hasClass("jsgrid-align-right"), "align class added to cell");775 });776 test("grid field cellRenderer", function() {777 var testItem = { text: "test" },778 args;779 var $grid = $("#jsGrid");780 var gridOptions = {781 data: [testItem],782 fields: [783 {784 name: "text",785 cellRenderer: function(value, item) {786 args = {787 value: value,788 item: item789 };790 return $("<td>").addClass("custom-class").text(value);791 }792 }793 ]794 };795 var grid = new Grid($grid, gridOptions);796 var $customCell = $grid.find(".custom-class");797 equal($customCell.length, 1, "custom cell rendered");798 equal($customCell.text(), "test");799 deepEqual(args, { value: "test", item: testItem }, "cellRenderer args provided");800 });801 test("grid field 'visible' option", function() {802 var $grid = $("#jsGrid");803 var gridOptions = {804 editing: true,805 fields: [806 { name: "id", visible: false },807 { name: "test" }808 ]809 };810 var grid = new Grid($grid, gridOptions);811 equal($grid.find("." + grid.noDataRowClass).children().eq(0).prop("colspan"), 1, "no data row colspan only for visible cells");812 grid.option("data", this.testData);813 grid.editItem(this.testData[2]);814 equal($grid.find("." + grid.headerRowClass).children().length, 1, "header single cell");815 equal($grid.find("." + grid.filterRowClass).children().length, 1, "filter single cell");816 equal($grid.find("." + grid.insertRowClass).children().length, 1, "insert single cell");817 equal($grid.find("." + grid.editRowClass).children().length, 1, "edit single cell");818 equal($grid.find("." + grid.oddRowClass).eq(0).children().length, 1, "odd data row single cell");819 equal($grid.find("." + grid.evenRowClass).eq(0).children().length, 1, "even data row single cell");820 });821 module("inserting");822 test("inserting rendering", function() {823 var $element = $("#jsGrid"),824 gridOptions = {825 inserting: true,826 fields: [827 {828 name: "test",829 align: "right",830 insertcss: "insert-class",831 insertTemplate: function() {832 var result = this.insertControl = $("<input>").attr("type", "text").addClass("insert-input");833 return result;834 }835 }836 ]837 },838 grid = new Grid($element, gridOptions);839 equal(grid._insertRow.find(".insert-class").length, 1, "insertcss class is attached");840 equal(grid._insertRow.find(".insert-input").length, 1, "insert control rendered");841 equal(grid._insertRow.find("." + grid.cellClass).length, 1, "cell class is attached");842 ok(grid._insertRow.find(".insert-class").hasClass("jsgrid-align-right"), "align class is attached");843 ok(grid.fields[0].insertControl.is("input[type=text]"), "insert control saved in field");844 });845 test("field without inserting", function() {846 var $element = $("#jsGrid"),847 jsGridFieldConfig = {848 insertTemplate: function() {849 var result = this.insertControl = $("<input>").attr("type", "text");850 return result;851 },852 insertValue: function() {853 return this.insertControl.val();854 }855 },856 gridOptions = {857 inserting: true,858 fields: [859 $.extend({}, jsGridFieldConfig, {860 name: "field1",861 inserting: false862 }),863 $.extend({}, jsGridFieldConfig, {864 name: "field2"865 })866 ]867 },868 grid = new Grid($element, gridOptions);869 grid.fields[0].insertControl.val("test1");870 grid.fields[1].insertControl.val("test2");871 deepEqual(grid._getInsertItem(), { field2: "test2" }, "field with inserting=false is not included in inserting item");872 });873 test("insert data", function() {874 var $element = $("#jsGrid"),875 inserted = false,876 insertingArgs,877 insertedArgs,878 gridOptions = {879 inserting: true,880 data: [],881 fields: [882 {883 name: "field",884 insertTemplate: function() {885 var result = this.insertControl = $("<input>").attr("type", "text");886 return result;887 },888 insertValue: function() {889 return this.insertControl.val();890 }891 }892 ],893 onItemInserting: function(e) {894 insertingArgs = $.extend(true, {}, e);895 },896 onItemInserted: function(e) {897 insertedArgs = $.extend(true, {}, e);898 },899 controller: {900 insertItem: function() {901 inserted = true;902 }903 }904 },905 grid = new Grid($element, gridOptions);906 grid.fields[0].insertControl.val("test");907 grid.insertItem();908 equal(insertingArgs.item.field, "test", "field is provided in inserting args");909 equal(grid.option("data").length, 1, "data is inserted");910 ok(inserted, "controller insertItem was called");911 deepEqual(grid.option("data")[0], { field: "test" }, "correct data is inserted");912 equal(insertedArgs.item.field, "test", "field is provided in inserted args");913 });914 test("insertItem accepts item to insert", function() {915 var $element = $("#jsGrid"),916 itemToInsert = { field: "test" },917 insertedItem,918 gridOptions = {919 data: [],920 fields: [921 {922 name: "field"923 }924 ],925 controller: {926 insertItem: function(item) {927 insertedItem = item;928 }929 }930 },931 grid = new Grid($element, gridOptions);932 grid.insertItem(itemToInsert);933 deepEqual(grid.option("data")[0], itemToInsert, "data is inserted");934 deepEqual(insertedItem, itemToInsert, "controller insertItem was called with correct item");935 });936 module("editing");937 test("editing rendering", function() {938 var $element = $("#jsGrid"),939 $editRow,940 data = [{941 test: "value"942 }],943 gridOptions = {944 editing: true,945 fields: [946 {947 name: "test",948 align: "right",949 editcss: "edit-class",950 editTemplate: function(value) {951 var result = this.editControl = $("<input>").attr("type", "text").val(value).addClass("edit-input");952 return result;953 }954 }955 ]956 },957 grid = new Grid($element, gridOptions);958 grid.option("data", data);959 equal(grid._content.find("." + grid.editRowClass).length, 0, "no edit row after initial rendering");960 grid.editItem(data[0]);961 $editRow = grid._content.find("." + grid.editRowClass);962 equal($editRow.length, 1, "edit row rendered");963 equal($editRow.find(".edit-class").length, 1, "editcss class is attached");964 equal($editRow.find(".edit-input").length, 1, "edit control rendered");965 equal($editRow.find("." + grid.cellClass).length, 1, "cell class is attached");966 ok($editRow.find(".edit-class").hasClass("jsgrid-align-right"), "align class is attached");967 ok(grid.fields[0].editControl.is("input[type=text]"), "edit control saved in field");968 equal(grid.fields[0].editControl.val(), "value", "edit control value");969 });970 test("editItem accepts row to edit", function() {971 var $element = $("#jsGrid"),972 $editRow,973 data = [974 { test: "value" }975 ],976 gridOptions = {977 editing: true,978 fields: [979 { name: "test" }980 ]981 },982 grid = new Grid($element, gridOptions);983 grid.option("data", data);984 var $row = $element.find("." + grid.oddRowClass).eq(0);985 grid.editItem($row);986 $editRow = grid._content.find("." + grid.editRowClass);987 equal($editRow.length, 1, "edit row rendered");988 grid.cancelEdit();989 grid.editItem($row.get(0));990 $editRow = grid._content.find("." + grid.editRowClass);991 equal($editRow.length, 1, "edit row rendered");992 });993 test("edit item", function() {994 var $element = $("#jsGrid"),995 editingArgs,996 editingRow,997 updated = false,998 updatingArgs,999 updatingRow,1000 updatedRow,1001 updatedArgs,1002 data = [{1003 field: "value"1004 }],1005 gridOptions = {1006 editing: true,1007 fields: [1008 {1009 name: "field",1010 editTemplate: function(value) {1011 var result = this.editControl = $("<input>").attr("type", "text").val(value);1012 return result;1013 },1014 editValue: function() {1015 return this.editControl.val();1016 }1017 }1018 ],1019 controller: {1020 updateItem: function(updatingItem) {1021 updated = true;1022 }1023 },1024 onItemEditing: function(e) {1025 editingArgs = $.extend(true, {}, e);1026 editingRow = grid.rowByItem(data[0])[0];1027 },1028 onItemUpdating: function(e) {1029 updatingArgs = $.extend(true, {}, e);1030 updatingRow = grid.rowByItem(data[0])[0];1031 },1032 onItemUpdated: function(e) {1033 updatedArgs = $.extend(true, {}, e);1034 updatedRow = grid.rowByItem(data[0])[0];1035 }1036 },1037 grid = new Grid($element, gridOptions);1038 grid.option("data", data);1039 grid.editItem(data[0]);1040 deepEqual(editingArgs.item, { field: "value" }, "item before editing is provided in editing event args");1041 equal(editingArgs.itemIndex, 0, "itemIndex is provided in editing event args");1042 equal(editingArgs.row[0], editingRow, "row element is provided in editing event args");1043 grid.fields[0].editControl.val("new value");1044 grid.updateItem();1045 deepEqual(updatingArgs.previousItem, { field: "value" }, "item before editing is provided in updating event args");1046 deepEqual(updatingArgs.item, { field: "new value" }, "updating item is provided in updating event args");1047 equal(updatingArgs.itemIndex, 0, "itemIndex is provided in updating event args");1048 equal(updatingArgs.row[0], updatingRow, "row element is provided in updating event args");1049 ok(updated, "controller updateItem called");1050 deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");1051 equal(grid._content.find("." + grid.editRowClass).length, 0, "edit row removed");1052 equal(grid._content.find("." + grid.oddRowClass).length, 1, "data row rendered");1053 deepEqual(updatedArgs.previousItem, { field: "value" }, "item before editing is provided in updated event args");1054 deepEqual(updatedArgs.item, { field: "new value" }, "updated item is provided in updated event args");1055 equal(updatedArgs.itemIndex, 0, "itemIndex is provided in updated event args");1056 equal(updatedArgs.row[0], updatedRow, "row element is provided in updated event args");1057 });1058 test("failed update should not change original item", function() {1059 var $element = $("#jsGrid"),1060 data = [{1061 field: "value"1062 }],1063 gridOptions = {1064 editing: true,1065 fields: [1066 {1067 name: "field",1068 editTemplate: function(value) {1069 var result = this.editControl = $("<input>").attr("type", "text").val(value);1070 return result;1071 },1072 editValue: function() {1073 return this.editControl.val();1074 }1075 }1076 ],1077 controller: {1078 updateItem: function(updatingItem) {1079 return $.Deferred().reject();1080 }1081 }1082 },1083 grid = new Grid($element, gridOptions);1084 grid.option("data", data);1085 grid.editItem(data[0]);1086 grid.fields[0].editControl.val("new value");1087 grid.updateItem();1088 deepEqual(grid.option("data")[0], { field: "value" }, "value is not updated");1089 });1090 test("cancel edit", function() {1091 var $element = $("#jsGrid"),1092 updated = false,1093 cancellingArgs,1094 cancellingRow,1095 data = [{1096 field: "value"1097 }],1098 gridOptions = {1099 editing: true,1100 fields: [1101 {1102 name: "field",1103 editTemplate: function(value) {1104 var result = this.editControl = $("<input>").attr("type", "text").val(value);1105 return result;1106 },1107 editValue: function() {1108 return this.editControl.val();1109 }1110 }1111 ],1112 controller: {1113 updateData: function(updatingItem) {1114 updated = true;1115 }1116 },1117 onItemEditCancelling: function(e) {1118 cancellingArgs = $.extend(true, {}, e);1119 cancellingRow = grid.rowByItem(data[0])[0];1120 }1121 },1122 grid = new Grid($element, gridOptions);1123 grid.option("data", data);1124 grid.editItem(data[0]);1125 grid.fields[0].editControl.val("new value");1126 grid.cancelEdit();1127 deepEqual(cancellingArgs.item, { field: "value" }, "item before cancel is provided in cancelling event args");1128 equal(cancellingArgs.itemIndex, 0, "itemIndex is provided in cancelling event args");1129 equal(cancellingArgs.row[0], cancellingRow, "row element is provided in cancelling event args");1130 ok(!updated, "controller updateItem was not called");1131 deepEqual(grid.option("data")[0], { field: "value" }, "data were not updated");1132 equal(grid._content.find("." + grid.editRowClass).length, 0, "edit row removed");1133 equal(grid._content.find("." + grid.oddRowClass).length, 1, "data row restored");1134 });1135 test("updateItem accepts item to update and new item", function() {1136 var $element = $("#jsGrid"),1137 updatingItem,1138 data = [{1139 field: "value"1140 }],1141 gridOptions = {1142 fields: [1143 { name: "field" }1144 ],1145 controller: {1146 updateItem: function(item) {1147 return updatingItem = item;1148 }1149 }1150 },1151 grid = new Grid($element, gridOptions);1152 grid.option("data", data);1153 grid.updateItem(data[0], { field: "new value" });1154 deepEqual(updatingItem, { field: "new value" }, "controller updateItem called correctly");1155 deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");1156 });1157 test("updateItem accepts single argument - item to update", function() {1158 var $element = $("#jsGrid"),1159 updatingItem,1160 data = [{1161 field: "value"1162 }],1163 gridOptions = {1164 fields: [1165 { name: "field" }1166 ],1167 controller: {1168 updateItem: function(item) {1169 return updatingItem = item;1170 }1171 }1172 },1173 grid = new Grid($element, gridOptions);1174 grid.option("data", data);1175 data[0].field = "new value";1176 grid.updateItem(data[0]);1177 deepEqual(updatingItem, { field: "new value" }, "controller updateItem called correctly");1178 deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");1179 });1180 test("editRowRenderer", function() {1181 var $element = $("#jsGrid"),1182 data = [1183 { value: "test" }1184 ],1185 gridOptions = {1186 data: data,1187 editing: true,1188 editRowRenderer: function(item, itemIndex) {1189 return $("<tr>").addClass("custom-edit-row").append($("<td>").text(itemIndex + ":" + item.value));1190 },1191 fields: [1192 { name: "value" }1193 ]1194 },1195 grid = new Grid($element, gridOptions);1196 grid.editItem(data[0]);1197 var $editRow = grid._content.find(".custom-edit-row");1198 equal($editRow.length, 1, "edit row rendered");1199 equal($editRow.text(), "0:test", "custom edit row renderer rendered");1200 });1201 module("deleting");1202 test("delete item", function() {1203 var $element = $("#jsGrid"),1204 deleted = false,1205 deletingArgs,1206 deletedArgs,1207 data = [{1208 field: "value"1209 }],1210 gridOptions = {1211 confirmDeleting: false,1212 fields: [1213 { name: "field" }1214 ],1215 controller: {1216 deleteItem: function(deletingItem) {1217 deleted = true;1218 }1219 },1220 onItemDeleting: function(e) {1221 deletingArgs = $.extend(true, {}, e);1222 },1223 onItemDeleted: function(e) {1224 deletedArgs = $.extend(true, {}, e);1225 }1226 },1227 grid = new Grid($element, gridOptions);1228 grid.option("data", data);1229 grid.deleteItem(data[0]);1230 deepEqual(deletingArgs.item, { field: "value" }, "field and value is provided in deleting event args");1231 equal(deletingArgs.itemIndex, 0, "itemIndex is provided in updating event args");1232 equal(deletingArgs.row.length, 1, "row element is provided in updating event args");1233 ok(deleted, "controller deleteItem called");1234 equal(grid.option("data").length, 0, "data row deleted");1235 deepEqual(deletedArgs.item, { field: "value" }, "item is provided in updating event args");1236 equal(deletedArgs.itemIndex, 0, "itemIndex is provided in updating event args");1237 equal(deletedArgs.row.length, 1, "row element is provided in updating event args");1238 });1239 test("deleteItem accepts row", function() {1240 var $element = $("#jsGrid"),1241 deletedItem,1242 itemToDelete = {1243 field: "value"1244 },1245 data = [itemToDelete],1246 gridOptions = {1247 confirmDeleting: false,1248 fields: [1249 { name: "field" }1250 ],1251 controller: {1252 deleteItem: function(deletingItem) {1253 deletedItem = deletingItem;1254 }1255 }1256 },1257 grid = new Grid($element, gridOptions);1258 grid.option("data", data);1259 var $row = $element.find("." + grid.oddRowClass).eq(0);1260 grid.deleteItem($row);1261 deepEqual(deletedItem, itemToDelete, "controller deleteItem called correctly");1262 equal(grid.option("data").length, 0, "data row deleted");1263 });1264 module("paging");1265 test("pager is rendered if necessary", function() {1266 var $element = $("#jsGrid"),1267 grid = new Grid($element, {1268 data: [{}, {}, {}],1269 paging: false,1270 pageSize: 21271 });1272 ok(grid._pagerContainer.is(":hidden"), "pager is hidden when paging=false");1273 equal(grid._pagerContainer.html(), "", "pager is not rendered when paging=false");1274 grid.option("paging", true);1275 ok(grid._pagerContainer.is(":visible"), "pager is visible when paging=true");1276 ok(grid._pagerContainer.html(), "pager is rendered when paging=true");1277 grid.option("data", [{}, {}]);1278 ok(grid._pagerContainer.is(":hidden"), "pager is hidden for single page");1279 ok(grid._pagerContainer.html(), "pager is rendered for single page when paging=true");1280 });1281 test("external pagerContainer", function() {1282 var $pagerContainer = $("<div>").appendTo("#qunit-fixture").hide(),1283 $element = $("#jsGrid");1284 new Grid($element, {1285 data: [{}, {}, {}],1286 pagerContainer: $pagerContainer,1287 paging: true,1288 pageSize: 21289 });1290 ok($pagerContainer.is(":visible"), "external pager shown");1291 ok($pagerContainer.html(), "external pager rendered");1292 });1293 test("pager functionality", function() {1294 var $element = $("#jsGrid"),1295 pager,1296 pageChangedArgs,1297 grid = new Grid($element, {1298 data: [{}, {}, {}, {}, {}, {}, {}, {}, {}],1299 onPageChanged: function(args) {1300 pageChangedArgs = args;1301 },1302 paging: true,1303 pageSize: 2,1304 pageButtonCount: 31305 });1306 equal(grid._pagesCount(), 5, "correct page count");1307 equal(grid.option("pageIndex"), 1, "pageIndex is initialized");1308 equal(grid._firstDisplayingPage, 1, "_firstDisplayingPage is initialized");1309 pager = grid._pagerContainer;1310 equal(pager.find("." + grid.currentPageClass).length, 1, "there is one current page");1311 ok(pager.find("." + grid.pageClass).eq(0).hasClass(grid.currentPageClass), "first page is current");1312 equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");1313 equal(pager.find("." + grid.pagerNavButtonClass).length, 5, "five nav buttons displayed: Fisrt Prev Next Last ...");1314 equal(pager.find("." + grid.pagerNavButtonInactiveClass).length, 2, "two nav buttons inactive: Fisrt Prev");1315 grid.openPage(2);1316 equal(pager.find("." + grid.currentPageClass).length, 1, "there is one current page");1317 ok(pager.find("." + grid.pageClass).eq(1).hasClass(grid.currentPageClass), "second page is current");1318 equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");1319 equal(pager.find("." + grid.pagerNavButtonClass).length, 5, "five nav buttons displayed: First Prev Next Last and ...");1320 equal(pageChangedArgs.pageIndex, 2, "onPageChanged callback provides pageIndex in arguments");1321 grid.showNextPages();1322 equal(grid._firstDisplayingPage, 3, "navigate by pages forward");1323 grid.showPrevPages();1324 equal(grid._firstDisplayingPage, 1, "navigate by pages backward");1325 grid.openPage(5);1326 equal(grid._firstDisplayingPage, 3, "opening next non-visible page moves first displaying page forward");1327 grid.openPage(2);1328 equal(grid._firstDisplayingPage, 2, "opening prev non-visible page moves first displaying page backward");1329 });1330 test("pager format", function() {1331 var $element = $("#jsGrid"),1332 grid = new Grid($element, {1333 data: [{}, {}, {}, {}, {}, {}],1334 paging: true,1335 pageSize: 2,1336 pageIndex: 2,1337 pageButtonCount: 1,1338 pagerFormat: "a {pageIndex} {first} {prev} {pages} {next} {last} {pageCount} {itemCount} z",1339 pagePrevText: "<",1340 pageNextText: ">",1341 pageFirstText: "<<",1342 pageLastText: ">>",1343 pageNavigatorNextText: "np",1344 pageNavigatorPrevText: "pp"1345 });1346 grid._firstDisplayingPage = 2;1347 grid._refreshPager();1348 equal($.trim(grid._pagerContainer.text()), "a 2 << < pp2np > >> 3 6 z", "pager text follows the format specified");1349 });1350 test("pagerRenderer", function() {1351 var $element = $("#jsGrid");1352 var pagerRendererConfig;1353 var pageSize = 2;1354 var items = [{}, {}, {}, {}, {}, {}, {}];1355 var pageCount = Math.ceil(items.length / pageSize);1356 var grid = new Grid($element, {1357 data: items,1358 paging: true,1359 pageSize: pageSize,1360 pagerRenderer: function(pagerConfig) {1361 pagerRendererConfig = pagerConfig;1362 }1363 });1364 deepEqual(pagerRendererConfig, { pageIndex: 1, pageCount: pageCount });1365 grid.openPage(2);1366 deepEqual(pagerRendererConfig, { pageIndex: 2, pageCount: pageCount });1367 });1368 test("loading by page", function() {1369 var $element = $("#jsGrid"),1370 data = [],1371 itemCount = 20;1372 for(var i = 1; i <= itemCount; i += 1) {1373 data.push({1374 value: i1375 });1376 }1377 var gridOptions = {1378 pageLoading: true,1379 paging: true,1380 pageSize: 7,1381 fields: [1382 { name: "value" }1383 ],1384 controller: {1385 loadData: function(filter) {1386 var startIndex = (filter.pageIndex - 1) * filter.pageSize,1387 result = data.slice(startIndex, startIndex + filter.pageSize);1388 return {1389 data: result,1390 itemsCount: data.length1391 };1392 }1393 }1394 };1395 var grid = new Grid($element, gridOptions);1396 grid.loadData();1397 var pager = grid._pagerContainer;1398 var gridData = grid.option("data");1399 equal(gridData.length, 7, "loaded one page of data");1400 equal(gridData[0].value, 1, "loaded right data start value");1401 equal(gridData[gridData.length - 1].value, 7, "loaded correct data end value");1402 ok(pager.find("." + grid.pageClass).eq(0).hasClass(grid.currentPageClass), "first page is current");1403 equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");1404 grid.openPage(3);1405 gridData = grid.option("data");1406 equal(gridData.length, 6, "loaded last page of data");1407 equal(gridData[0].value, 15, "loaded right data start value");1408 equal(gridData[gridData.length - 1].value, 20, "loaded right data end value");1409 ok(pager.find("." + grid.pageClass).eq(2).hasClass(grid.currentPageClass), "third page is current");1410 equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");1411 });1412 test("'openPage' method ignores indexes out of range", function() {1413 var $element = $("#jsGrid"),1414 grid = new Grid($element, {1415 data: [{}, {}],1416 paging: true,1417 pageSize: 11418 });1419 grid.openPage(0);1420 equal(grid.option("pageIndex"), 1, "too small index is ignored");1421 grid.openPage(3);1422 equal(grid.option("pageIndex"), 1, "too big index is ignored");1423 });1424 module("sorting");1425 test("sorting", function() {1426 var $element = $("#jsGrid"),1427 gridOptions = {1428 sorting: true,1429 data: [1430 { value: 3 },1431 { value: 2 },1432 { value: 1 }1433 ],1434 fields: [1435 { name: "value", sorter: "number" }1436 ]1437 },1438 grid = new Grid($element, gridOptions);1439 var gridData = grid.option("data");1440 var $th = grid._headerRow.find("th").eq(0);1441 $th.trigger("click");1442 equal(grid._sortOrder, "asc", "asc sorting order for first click");1443 equal(grid._sortField, grid.fields[0], "sort field is set");1444 equal(gridData[0].value, 1);1445 equal(gridData[1].value, 2);1446 equal(gridData[2].value, 3);1447 ok($th.hasClass(grid.sortableClass));1448 ok($th.hasClass(grid.sortAscClass));1449 $th.trigger("click");1450 equal(grid._sortOrder, "desc", "desc sorting order for second click");1451 equal(grid._sortField, grid.fields[0], "sort field is set");1452 equal(gridData[0].value, 3);1453 equal(gridData[1].value, 2);1454 equal(gridData[2].value, 1);1455 ok(!$th.hasClass(grid.sortAscClass));1456 ok($th.hasClass(grid.sortDescClass));1457 });1458 test("sorting with pageLoading", function() {1459 var $element = $("#jsGrid"),1460 loadFilter,1461 gridOptions = {1462 sorting: true,1463 pageLoading: true,1464 data: [1465 { value: 3 },1466 { value: 2 },1467 { value: 1 }1468 ],1469 controller: {1470 loadData: function(filter) {1471 loadFilter = filter;1472 return {1473 itemsCount: 0,1474 data: []1475 };1476 }1477 },1478 fields: [1479 { name: "value", sorter: "number" }1480 ]1481 },1482 grid = new Grid($element, gridOptions);1483 var $th = grid._headerRow.find("th").eq(0);1484 $th.trigger("click");1485 equal(grid._sortOrder, "asc", "asc sorting order for first click");1486 equal(grid._sortField, grid.fields[0], "sort field is set");1487 equal(loadFilter.sortOrder, "asc", "sort direction is provided in loadFilter");1488 equal(loadFilter.sortField, "value", "sort field is provided in loadFilter");1489 $th.trigger("click");1490 equal(grid._sortOrder, "desc", "desc sorting order for second click");1491 equal(grid._sortField, grid.fields[0], "sort field is set");1492 equal(loadFilter.sortOrder, "desc", "sort direction is provided in loadFilter");1493 equal(loadFilter.sortField, "value", "sort field is provided in loadFilter");1494 });1495 test("no sorting for column with sorting = false", function() {1496 var $element = $("#jsGrid");1497 var gridOptions = {1498 sorting: true,1499 data: [1500 { value: 3 },1501 { value: 2 },1502 { value: 1 }1503 ],1504 fields: [1505 { name: "value", sorting: false }1506 ]1507 };1508 var grid = new Grid($element, gridOptions);1509 var gridData = grid.option("data");1510 var $th = grid._headerRow.find("th").eq(0);1511 $th.trigger("click");1512 equal(grid._sortField, null, "sort field is not set for the field with sorting=false");1513 equal(gridData[0].value, 3);1514 equal(gridData[1].value, 2);1515 equal(gridData[2].value, 1);1516 equal($th.hasClass(grid.sortableClass), false, "no sorting css for field with sorting=false");1517 equal($th.hasClass(grid.sortAscClass), false, "no sorting css for field with sorting=false");1518 });1519 test("sort accepts sorting config", function() {1520 var $element = $("#jsGrid"),1521 gridOptions = {1522 sorting: true,1523 data: [1524 { value: 3 },1525 { value: 2 },1526 { value: 1 }1527 ],1528 fields: [1529 { name: "value", sorter: "number" }1530 ]1531 },1532 grid = new Grid($element, gridOptions);1533 var gridData = grid.option("data");1534 grid.sort({ field: "value", order: "asc" });1535 equal(grid._sortOrder, "asc", "asc sorting order is set");1536 equal(grid._sortField, grid.fields[0], "sort field is set");1537 equal(gridData[0].value, 1);1538 equal(gridData[1].value, 2);1539 equal(gridData[2].value, 3);1540 grid.sort({ field: 0 });1541 equal(grid._sortOrder, "desc", "desc sorting order for already set asc sorting");1542 equal(grid._sortField, grid.fields[0], "sort field is set");1543 equal(gridData[0].value, 3);1544 equal(gridData[1].value, 2);1545 equal(gridData[2].value, 1);1546 grid.sort("value", "asc");1547 equal(grid._sortOrder, "asc", "asc sorting order is set");1548 equal(grid._sortField, grid.fields[0], "sort field is set");1549 grid.sort(0);1550 equal(grid._sortOrder, "desc", "desc sorting order for already set asc sorting");1551 equal(grid._sortField, grid.fields[0], "sort field is set");1552 });1553 test("getSorting returns current sorting", function() {1554 var $element = $("#jsGrid"),1555 gridOptions = {1556 sorting: true,1557 data: [1558 { value: 3 },1559 { value: 2 },1560 { value: 1 }1561 ],1562 fields: [1563 { name: "value", sorter: "number" }1564 ]1565 },1566 grid = new Grid($element, gridOptions);1567 deepEqual(grid.getSorting(), { field: undefined, order: undefined }, "undefined field and order before sorting");1568 grid.sort("value");1569 deepEqual(grid.getSorting(), { field: "value", order: "asc" }, "current sorting returned");1570 });1571 test("sorting css attached correctly when a field is hidden", function() {1572 var $element = $("#jsGrid");1573 var gridOptions = {1574 sorting: true,1575 data: [],1576 fields: [1577 { name: "field1", visible: false },1578 { name: "field2" }1579 ]1580 };1581 var grid = new Grid($element, gridOptions);1582 var gridData = grid.option("data");1583 var $th = grid._headerRow.find("th").eq(0);1584 $th.trigger("click");1585 equal($th.hasClass(grid.sortAscClass), true, "sorting css is attached to first field");1586 });1587 module("canceling events");1588 test("cancel item edit", function() {1589 var $element = $("#jsGrid");1590 var data = [{}];1591 var gridOptions = {1592 editing: true,1593 onItemEditing: function(e) {1594 e.cancel = true;1595 },1596 controller: {1597 loadData: function() {1598 return data;1599 }1600 },1601 fields: [1602 { name: "test" }1603 ]1604 };1605 var grid = new Grid($element, gridOptions);1606 grid.loadData();1607 grid.editItem(data[0]);1608 strictEqual(grid._editingRow, null, "no editing row");1609 });1610 test("cancel controller.loadData", function() {1611 var $element = $("#jsGrid");1612 var gridOptions = {1613 onDataLoading: function(e) {1614 e.cancel = true;1615 },1616 controller: {1617 loadData: function() {1618 return [{}];1619 }1620 },1621 fields: [1622 { name: "test" }1623 ]1624 };1625 var grid = new Grid($element, gridOptions);1626 grid.loadData();1627 equal(grid.option("data").length, 0, "no data loaded");1628 });1629 test("cancel controller.insertItem", function() {1630 var $element = $("#jsGrid");1631 var insertedItem = null;1632 var gridOptions = {1633 onItemInserting: function(e) {1634 e.cancel = true;1635 },1636 controller: {1637 insertItem: function(item) {1638 insertedItem = item;1639 }1640 },1641 fields: [1642 { name: "test" }1643 ]1644 };1645 var grid = new Grid($element, gridOptions);1646 grid.insertItem({ test: "value" });1647 strictEqual(insertedItem, null, "item was not inserted");1648 });1649 test("cancel controller.updateItem", function() {1650 var $element = $("#jsGrid");1651 var updatedItem = null;1652 var existingItem = { test: "value" };1653 var gridOptions = {1654 data: [1655 existingItem1656 ],1657 onItemUpdating: function(e) {1658 e.cancel = true;1659 },1660 controller: {1661 updateItem: function(item) {1662 updatedItem = item;1663 }1664 },1665 fields: [1666 { name: "test" }1667 ]1668 };1669 var grid = new Grid($element, gridOptions);1670 grid.updateItem(existingItem, { test: "new_value" });1671 strictEqual(updatedItem, null, "item was not updated");1672 });1673 test("cancel controller.deleteItem", function() {1674 var $element = $("#jsGrid");1675 var deletingItem = { test: "value" };1676 var deletedItem = null;1677 var gridOptions = {1678 data: [1679 deletingItem1680 ],1681 confirmDeleting: false,1682 onItemDeleting: function(e) {1683 e.cancel = true;1684 },1685 controller: {1686 deleteItem: function(item) {1687 deletedItem = item;1688 }1689 },1690 fields: [1691 { name: "test" }1692 ]1693 };1694 var grid = new Grid($element, gridOptions);1695 grid.deleteItem(deletingItem);1696 strictEqual(deletedItem, null, "item was not deleted");1697 });1698 module("complex properties binding");1699 test("rendering", function() {1700 var $element = $("#jsGrid");1701 var gridOptions = {1702 loadMessage: "",1703 data: [1704 { complexProp: { prop: "test" } }1705 ],1706 fields: [1707 { name: "complexProp.prop", title: "" }1708 ]1709 };1710 new Grid($element, gridOptions);1711 equal($element.text(), "test", "complex property value rendered");1712 });1713 test("editing", function() {1714 var $element = $("#jsGrid");1715 var gridOptions = {1716 editing: true,1717 data: [1718 { complexProp: { prop: "test" } }1719 ],1720 fields: [1721 { type: "text", name: "complexProp.prop" }1722 ]1723 };1724 var grid = new Grid($element, gridOptions);1725 grid.editItem(gridOptions.data[0]);1726 equal(grid.fields[0].editControl.val(), "test", "complex property value set in editor");1727 });1728 test("should not fail if property is absent", function() {1729 var $element = $("#jsGrid");1730 var gridOptions = {1731 loadMessage: "",1732 data: [1733 { complexProp: { } }1734 ],1735 fields: [1736 { name: "complexProp.subprop.prop", title: "" }1737 ]1738 };1739 new Grid($element, gridOptions);1740 equal($element.text(), "", "rendered empty value");1741 });1742 test("inserting", function() {1743 var $element = $("#jsGrid");1744 var insertingItem;1745 var gridOptions = {1746 inserting: true,1747 fields: [1748 { type: "text", name: "complexProp.prop" }1749 ],1750 onItemInserting: function(args) {1751 insertingItem = args.item;1752 }1753 };1754 var grid = new Grid($element, gridOptions);1755 grid.fields[0].insertControl.val("test");1756 grid.insertItem();1757 deepEqual(insertingItem, { complexProp: { prop: "test" } }, "inserting item has complex properties");1758 });1759 test("filtering", function() {1760 var $element = $("#jsGrid");1761 var loadFilter;1762 var gridOptions = {1763 filtering: true,1764 fields: [1765 { type: "text", name: "complexProp.prop" }1766 ],1767 controller: {1768 loadData: function(filter) {1769 loadFilter = filter;1770 }1771 }1772 };1773 var grid = new Grid($element, gridOptions);1774 grid.fields[0].filterControl.val("test");1775 grid.search();1776 deepEqual(loadFilter, { complexProp: { prop: "test" } }, "filter has complex properties");1777 });1778 test("updating", function() {1779 var $element = $("#jsGrid");1780 var updatingItem;1781 var gridOptions = {1782 editing: true,1783 data: [1784 { complexProp: { } }1785 ],1786 fields: [1787 { type: "text", name: "complexProp.prop" }1788 ],1789 onItemUpdating: function(args) {1790 updatingItem = args.item;1791 }1792 };1793 var grid = new Grid($element, gridOptions);1794 grid.editItem(gridOptions.data[0]);1795 grid.fields[0].editControl.val("test");1796 grid.updateItem();1797 deepEqual(updatingItem, { complexProp: { prop: "test" } }, "updating item has complex properties");1798 });1799 test("update nested prop", function() {1800 var $element = $("#jsGrid");1801 var updatingItem;1802 var previousItem;1803 var gridOptions = {1804 editing: true,1805 data: [1806 { prop: { subprop1: "test1", subprop2: "test2" } }1807 ],1808 fields: [1809 { type: "text", name: "prop.subprop1" },1810 { type: "text", name: "prop.subprop2" }1811 ],1812 onItemUpdating: function(args) {1813 updatingItem = args.item;1814 }1815 };1816 var grid = new Grid($element, gridOptions);1817 grid.editItem(gridOptions.data[0]);1818 grid.fields[0].editControl.val("new_test1");1819 grid.updateItem();1820 var expectedUpdatingItem = {1821 prop: {1822 subprop1: "new_test1",1823 subprop2: "test2"1824 }1825 };1826 deepEqual(updatingItem, expectedUpdatingItem, "updating item has nested properties");1827 });1828 test("updating deeply nested prop", function() {1829 var $element = $("#jsGrid");1830 var updatingItem;1831 var previousItem;1832 var gridOptions = {1833 editing: true,1834 data: [1835 { complexProp: { subprop1: { another_prop: "test" } } }1836 ],1837 fields: [1838 { type: "text", name: "complexProp.subprop1.prop1" },1839 { type: "text", name: "complexProp.subprop1.subprop2.prop12" }1840 ],1841 onItemUpdating: function(args) {1842 updatingItem = $.extend(true, {}, args.item);1843 previousItem = $.extend(true, {}, args.previousItem);1844 }1845 };1846 var grid = new Grid($element, gridOptions);1847 grid.editItem(gridOptions.data[0]);1848 grid.fields[0].editControl.val("test1");1849 grid.fields[1].editControl.val("test2");1850 grid.updateItem();1851 var expectedUpdatingItem = {1852 complexProp: {1853 subprop1: {1854 another_prop: "test",1855 prop1: "test1",1856 subprop2: { prop12: "test2" }1857 }1858 }1859 };1860 var expectedPreviousItem = {1861 complexProp: {1862 subprop1: {1863 another_prop: "test"1864 }1865 }1866 };1867 deepEqual(updatingItem, expectedUpdatingItem, "updating item has deeply nested properties");1868 deepEqual(previousItem, expectedPreviousItem, "previous item preserved correctly");1869 });1870 module("validation");1871 test("insertItem should call validation.validate", function() {1872 var $element = $("#jsGrid");1873 var fieldValidationRules = { test: "value" };1874 var validatingArgs;1875 var gridOptions = {1876 data: [],1877 inserting: true,1878 invalidNotify: $.noop,1879 validation: {1880 validate: function(args) {1881 validatingArgs = args;1882 return [];1883 }1884 },1885 fields: [1886 { type: "text", name: "Name", validate: fieldValidationRules }1887 ]1888 };1889 var grid = new Grid($element, gridOptions);1890 grid.fields[0].insertControl.val("test");1891 grid.insertItem();1892 deepEqual(validatingArgs, { value: "test", item: { Name: "test" }, itemIndex: -1,1893 row: grid._insertRow, rules: fieldValidationRules }, "validating args is provided");1894 });1895 test("insertItem rejected when data is not valid", function() {1896 var $element = $("#jsGrid");1897 var gridOptions = {1898 data: [],1899 inserting: true,1900 invalidNotify: $.noop,1901 validation: {1902 validate: function() {1903 return ["Error"];1904 }1905 },1906 fields: [1907 { type: "text", name: "Name", validate: true }1908 ]1909 };1910 var grid = new Grid($element, gridOptions);1911 grid.fields[0].insertControl.val("test");1912 grid.insertItem().done(function() {1913 ok(false, "insertItem should not be completed");1914 }).fail(function() {1915 ok(true, "insertItem should fail");1916 });1917 });1918 test("invalidClass is attached on invalid cell on inserting", function() {1919 var $element = $("#jsGrid");1920 var gridOptions = {1921 data: [],1922 inserting: true,1923 invalidNotify: $.noop,1924 validation: {1925 validate: function() {1926 return ["Error"];1927 }1928 },1929 fields: [1930 { type: "text", name: "Id", visible: false },1931 { type: "text", name: "Name", validate: true }1932 ]1933 };1934 var grid = new Grid($element, gridOptions);1935 var $insertCell = grid._insertRow.children().eq(0);1936 grid.insertItem();1937 ok($insertCell.hasClass(grid.invalidClass), "invalid class is attached");1938 equal($insertCell.attr("title"), "Error", "cell tooltip contains error message");1939 });1940 test("onItemInvalid callback", function() {1941 var $element = $("#jsGrid");1942 var errors = ["Error"];1943 var onItemInvalidCalled = 0;1944 var onItemInvalidArgs;1945 var gridOptions = {1946 data: [],1947 inserting: true,1948 invalidNotify: $.noop,1949 onItemInvalid: function(args) {1950 onItemInvalidCalled++;1951 onItemInvalidArgs = args;1952 },1953 validation: {1954 validate: function(args) {1955 return !args.value ? errors : [];1956 }1957 },1958 fields: [1959 { type: "text", name: "Name", validate: true }1960 ]1961 };1962 var grid = new Grid($element, gridOptions);1963 grid.insertItem();1964 equal(onItemInvalidCalled, 1, "onItemInvalid is called, when item data is invalid");1965 deepEqual(onItemInvalidArgs, { grid: grid, errors: [{ field: grid.fields[0], message: "Error" }],1966 item: { Name: "" }, itemIndex: -1, row: grid._insertRow }, "arguments provided");1967 grid.fields[0].insertControl.val("test");1968 grid.insertItem();1969 equal(onItemInvalidCalled, 1, "onItemInvalid was not called, when data is valid");1970 });1971 test("invalidNotify", function() {1972 var $element = $("#jsGrid");1973 var errors = ["Error"];1974 var invalidNotifyCalled = 0;1975 var invalidNotifyArgs;1976 var gridOptions = {1977 data: [],1978 inserting: true,1979 invalidNotify: function(args) {1980 invalidNotifyCalled++;1981 invalidNotifyArgs = args;1982 },1983 validation: {1984 validate: function(args) {1985 return !args.value ? errors : [];1986 }1987 },1988 fields: [1989 { type: "text", name: "Name", validate: true }1990 ]1991 };1992 var grid = new Grid($element, gridOptions);1993 grid.insertItem();1994 equal(invalidNotifyCalled, 1, "invalidNotify is called, when item data is invalid");1995 deepEqual(invalidNotifyArgs, { grid: grid, errors: [{ field: grid.fields[0], message: "Error" }],1996 row: grid._insertRow, item: { Name: "" }, itemIndex: -1 }, "arguments provided");1997 grid.fields[0].insertControl.val("test");1998 grid.insertItem();1999 equal(invalidNotifyCalled, 1, "invalidNotify was not called, when data is valid");2000 });2001 test("invalidMessage", function() {2002 var $element = $("#jsGrid");2003 var invalidMessage;2004 var originalAlert = window.alert;2005 window.alert = function(message) {2006 invalidMessage = message;2007 };2008 try {2009 Grid.prototype.invalidMessage = "InvalidTest";2010 Grid.prototype.invalidNotify({ errors: [{ message: "Message1" }, { message: "Message2" }] });2011 var expectedInvalidMessage = ["InvalidTest", "Message1", "Message2"].join("\n");2012 equal(invalidMessage, expectedInvalidMessage, "message contains invalidMessage and field error messages");2013 } finally {2014 window.alert = originalAlert;2015 }2016 });2017 test("updateItem should call validation.validate", function() {2018 var $element = $("#jsGrid");2019 var validatingArgs;2020 var gridOptions = {2021 data: [{ Name: "" }],2022 editing: true,2023 invalidNotify: $.noop,2024 validation: {2025 validate: function(args) {2026 validatingArgs = args;2027 return ["Error"];2028 }2029 },2030 fields: [2031 { type: "text", name: "Name", validate: "required" }2032 ]2033 };2034 var grid = new Grid($element, gridOptions);2035 grid.editItem(gridOptions.data[0]);2036 grid.fields[0].editControl.val("test");2037 grid.updateItem();2038 deepEqual(validatingArgs, { value: "test", item: { Name: "test" }, itemIndex: 0,2039 row: grid._getEditRow(), rules: "required" }, "validating args is provided");2040 });2041 test("invalidClass is attached on invalid cell on updating", function() {2042 var $element = $("#jsGrid");2043 var gridOptions = {2044 data: [{}],2045 editing: true,2046 invalidNotify: $.noop,2047 validation: {2048 validate: function() {2049 return ["Error"];2050 }2051 },2052 fields: [2053 { type: "text", name: "Name", validate: true }2054 ]2055 };2056 var grid = new Grid($element, gridOptions);2057 grid.editItem(gridOptions.data[0]);2058 var $editCell = grid._getEditRow().children().eq(0);2059 grid.updateItem();2060 ok($editCell.hasClass(grid.invalidClass), "invalid class is attached");2061 equal($editCell.attr("title"), "Error", "cell tooltip contains error message");2062 });2063 test("validation should ignore not editable fields", function() {2064 var invalidNotifyCalled = 0;2065 var $element = $("#jsGrid");2066 var validatingArgs;2067 var gridOptions = {2068 data: [{ Name: "" }],2069 editing: true,2070 invalidNotify: function() {2071 invalidNotifyCalled++;2072 },2073 validation: {2074 validate: function() {2075 return ["Error"];2076 }2077 },2078 fields: [2079 { type: "text", name: "Name", editing: false, validate: "required" }2080 ]2081 };2082 var grid = new Grid($element, gridOptions);2083 grid.editItem(gridOptions.data[0]);2084 grid.updateItem();2085 equal(invalidNotifyCalled, 0, "data is valid");2086 });2087 module("api");2088 test("reset method should go the first page when pageLoading is truned on", function() {2089 var items = [{ Name: "1" }, { Name: "2" }];2090 var $element = $("#jsGrid");2091 var gridOptions = {2092 paging: true,2093 pageSize: 1,2094 pageLoading: true,2095 autoload: true,2096 controller: {2097 loadData: function(args) {2098 return {2099 data: [items[args.pageIndex - 1]],2100 itemsCount: items.length2101 };2102 }2103 },2104 fields: [2105 { type: "text", name: "Name" }2106 ]2107 };2108 var grid = new Grid($element, gridOptions);2109 grid.openPage(2);2110 grid.reset();2111 equal(grid._bodyGrid.text(), "1", "grid content reset");2112 });2113 module("i18n");2114 test("set locale by name", function() {2115 jsGrid.locales.my_lang = {2116 grid: {2117 test: "test_text"2118 }2119 };2120 jsGrid.locale("my_lang");2121 var $element = $("#jsGrid").jsGrid({});2122 equal($element.jsGrid("option", "test"), "test_text", "option localized");2123 });2124 test("set locale by config", function() {2125 jsGrid.locale( {2126 grid: {2127 test: "test_text"2128 }2129 });2130 var $element = $("#jsGrid").jsGrid({});2131 equal($element.jsGrid("option", "test"), "test_text", "option localized");2132 });2133 test("locale throws exception for unknown locale", function() {2134 throws(function() {2135 jsGrid.locale("unknown_lang");2136 }, /unknown_lang/, "locale threw an exception");2137 });2138 module("controller promise");2139 asyncTest("should support jQuery promise success callback", 1, function() {2140 var data = [];2141 var gridOptions = {2142 autoload: false,2143 controller: {2144 loadData: function() {2145 var d = $.Deferred();2146 setTimeout(function() {2147 d.resolve(data);2148 start();2149 });2150 return d.promise();2151 }2152 }2153 };2154 var grid = new Grid($("#jsGrid"), gridOptions);2155 var promise = grid._controllerCall("loadData", {}, false, $.noop);2156 promise.done(function(result) {2157 equal(result, data, "data provided to done callback");2158 });2159 });2160 asyncTest("should support jQuery promise fail callback", 1, function() {2161 var failArgs = {};2162 var gridOptions = {2163 autoload: false,2164 controller: {2165 loadData: function() {2166 var d = $.Deferred();2167 setTimeout(function() {2168 d.reject(failArgs);2169 start();2170 });2171 return d.promise();2172 }2173 }2174 };2175 var grid = new Grid($("#jsGrid"), gridOptions);2176 var promise = grid._controllerCall("loadData", {}, false, $.noop);2177 promise.fail(function(result) {2178 equal(result, failArgs, "fail args provided to fail callback");2179 });2180 });2181 asyncTest("should support JS promise success callback", 1, function() {2182 if(typeof Promise === "undefined") {2183 ok(true, "Promise not supported");2184 start();2185 return;2186 }2187 var data = [];2188 var gridOptions = {2189 autoload: false,2190 controller: {2191 loadData: function() {2192 return new Promise(function(resolve, reject) {2193 setTimeout(function() {2194 resolve(data);2195 start();2196 });2197 });2198 }2199 }2200 };2201 var grid = new Grid($("#jsGrid"), gridOptions);2202 var promise = grid._controllerCall("loadData", {}, false, $.noop);2203 promise.done(function(result) {2204 equal(result, data, "data provided to done callback");2205 });2206 });2207 asyncTest("should support JS promise fail callback", 1, function() {2208 if(typeof Promise === "undefined") {2209 ok(true, "Promise not supported");2210 start();2211 return;2212 }2213 var failArgs = {};2214 var gridOptions = {2215 autoload: false,2216 controller: {2217 loadData: function() {2218 return new Promise(function(resolve, reject) {2219 setTimeout(function() {2220 reject(failArgs);2221 start();2222 });2223 });2224 }2225 }2226 };2227 var grid = new Grid($("#jsGrid"), gridOptions);2228 var promise = grid._controllerCall("loadData", {}, false, $.noop);2229 promise.fail(function(result) {2230 equal(result, failArgs, "fail args provided to fail callback");2231 });2232 });2233 test("should support non-promise result", 1, function() {2234 var data = [];2235 var gridOptions = {2236 autoload: false,2237 controller: {2238 loadData: function() {2239 return data;2240 }2241 }2242 };2243 var grid = new Grid($("#jsGrid"), gridOptions);2244 var promise = grid._controllerCall("loadData", {}, false, $.noop);2245 promise.done(function(result) {2246 equal(result, data, "data provided to done callback");2247 });2248 });2249 module("renderTemplate");2250 test("should pass undefined and null arguments to the renderer", function() {2251 var rendererArgs;2252 var rendererContext;2253 var context = {};2254 var renderer = function() {2255 rendererArgs = arguments;2256 rendererContext = this;2257 };2258 Grid.prototype.renderTemplate(renderer, context, { arg1: undefined, arg2: null, arg3: "test" });2259 equal(rendererArgs.length, 3);2260 strictEqual(rendererArgs[0], undefined, "undefined passed");2261 strictEqual(rendererArgs[1], null, "null passed");2262 strictEqual(rendererArgs[2], "test", "null passed");2263 strictEqual(rendererContext, context, "context is preserved");2264 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4 videoParams: {5 }6};7wpt.runTest(url, options, function(err, data) {8 if (err) return console.error(err);9 console.log('Test submitted to WebPagetest for %s', url);10 console.log('View your test results at: %s', data.data.userUrl);11});

Full Screen

Using AI Code Generation

copy

Full Screen

1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3}, function(err, data) {4 if (err) {5 console.log('Error: ' + err.message);6 } else {7 console.log('Test submitted. You can check the status at:');8 console.log('Test ID: ' + data.data.testId);9 }10});11wpt.getTestResults('160101_5D_1f3b', function(err, data) {12 if (err) {13 console.log('Error: ' + err.message);14 } else {15 console.log('Test status: ' + data.data.statusText);16 console.log('Test results: ' + data.data.summary);17 }18});19wpt.getLocations(function(err, data) {20 if (err) {21 console.log('Error: ' + err.message);22 } else {23 console.log('Locations: ' + JSON.stringify(data));24 }25});26wpt.getTesters(function(err, data) {27 if (err) {28 console.log('Error: ' + err.message);29 } else {30 console.log('Testers: ' + JSON.stringify(data));31 }32});33wpt.getTesters('Dulles:Chrome', function(err, data) {34 if (err) {35 console.log('Error: ' + err.message);36 } else {37 console.log('Testers: ' + JSON.stringify(data));38 }39});40wpt.getTesters('Dulles:Chrome', true, function(err, data) {41 if (err) {42 console.log('Error: ' + err.message);43 } else {44 console.log('Testers: ' + JSON.stringify(data));45 }46});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var fs = require('fs');3var async = require('async');4var request = require('request');5var cheerio = require('cheerio');6var url = require('url');7var csv = require('fast-csv');8var stream = fs.createReadStream("test.csv");9var csvStream = csv()10 .on("data", function(data){11 var grid = new wptools.grid(data[0]);12 grid.get(function(err, resp) {13 if (err) {14 console.log('error');15 console.log(err);16 }17 else {18 console.log('success');19 console.log(resp);20 }21 });22 })23 .on("end", function(){24 console.log("done");25 });26stream.pipe(csvStream);27 at Error (native)28 at Socket.socketOnData (_http_client.js:309:20)29 at emitOne (events.js:77:13)30 at Socket.emit (events.js:169:7)31 at readableAddChunk (_stream_readable.js:146:16)32 at Socket.Readable.push (_stream_readable.js:110:10)33 at TCP.onread (net.js:523:20)

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest')('www.webpagetest.org');2var location = "Dulles:Chrome";3var options = {4};5wpt.runTest(url, options, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8});9{ statusCode: 400,10 data: 'Invalid test options: firstViewOnly' }

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful