From 57df36fe670048b74422080c4f8e85aaaf05ea47 Mon Sep 17 00:00:00 2001 From: Trinh Ho Date: Tue, 14 May 2013 15:03:00 -0700 Subject: [PATCH] Fix wysiwyg-export plugin. Add jasmine specs for plugins. Fix plugin initialization when passing in function literal.. In order for "getInstanceType" to work correctly, function literal must be declared in form "function SomeClass () {}". --- GruntFile.js | 2 +- config/karma.conf.js | 3 + plugins/ng-grid-csv-export.js | 2 +- plugins/ng-grid-flexible-height.js | 2 +- plugins/ng-grid-layout.js | 2 +- plugins/ng-grid-reorderable.js | 2 +- plugins/ng-grid-wysiwyg-export.js | 22 +++--- src/directives/ng-grid.js | 4 +- test/unit/pluginsSpec.js | 104 +++++++++++++++++++++++++++++ 9 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 test/unit/pluginsSpec.js diff --git a/GruntFile.js b/GruntFile.js index b256fb1692..f85a6d7a6d 100644 --- a/GruntFile.js +++ b/GruntFile.js @@ -129,7 +129,7 @@ watch: { // Run unit test with karma karma: { - files: ['build/ng-grid.debug.js', 'test/unit/**/*.js'], + files: ['build/ng-grid.debug.js', 'test/unit/**/*.js', 'plugins/*.js'], tasks: ['karma:watch:run'] }, // Auto-build ng-grid.debug.js when source files change diff --git a/config/karma.conf.js b/config/karma.conf.js index a5d65c586c..2e9261fa0a 100644 --- a/config/karma.conf.js +++ b/config/karma.conf.js @@ -12,6 +12,9 @@ files = [ // App code 'build/ng-grid.debug.js', + + // Plugins + 'plugins/*.js', // Test specs 'test/unit/**/*.js' diff --git a/plugins/ng-grid-csv-export.js b/plugins/ng-grid-csv-export.js index 813d1a3514..f8f42d62ac 100644 --- a/plugins/ng-grid-csv-export.js +++ b/plugins/ng-grid-csv-export.js @@ -5,7 +5,7 @@ // download from a data-uri link // // Notes: This has not been adequately tested and is very much a proof of concept at this point -ngGridCsvExportPlugin = function(opts) { +function ngGridCsvExportPlugin (opts) { var self = this; self.grid = null; self.scope = null; diff --git a/plugins/ng-grid-flexible-height.js b/plugins/ng-grid-flexible-height.js index fa86ee1cb1..dd5d66c2f6 100644 --- a/plugins/ng-grid-flexible-height.js +++ b/plugins/ng-grid-flexible-height.js @@ -1,4 +1,4 @@ -ngGridFlexibleHeightPlugin = function (opts) { +function ngGridFlexibleHeightPlugin (opts) { var self = this; self.grid = null; self.scope = null; diff --git a/plugins/ng-grid-layout.js b/plugins/ng-grid-layout.js index 139d7b2d8f..aa430eb756 100644 --- a/plugins/ng-grid-layout.js +++ b/plugins/ng-grid-layout.js @@ -1,4 +1,4 @@ -ngGridLayoutPlugin = function() { +function ngGridLayoutPlugin () { var self = this; this.grid = null; this.scope = null; diff --git a/plugins/ng-grid-reorderable.js b/plugins/ng-grid-reorderable.js index 9cdb531bf8..dc4d797e1c 100644 --- a/plugins/ng-grid-reorderable.js +++ b/plugins/ng-grid-reorderable.js @@ -2,7 +2,7 @@ DO NOT USE THIS PLUGIN. IT IS ONLY AN EXAMPLE FOR INSTRUCTIONAL PURPOSES. */ -ngGridReorderable = function(options) { +function ngGridReorderable (options) { var defaults = { enableHeader: true, enableRow: true diff --git a/plugins/ng-grid-wysiwyg-export.js b/plugins/ng-grid-wysiwyg-export.js index d401bb5531..9d930170de 100644 --- a/plugins/ng-grid-wysiwyg-export.js +++ b/plugins/ng-grid-wysiwyg-export.js @@ -1,11 +1,13 @@ -ngGridWYSIWYGPlugin = function (filter) { +function ngGridWYSIWYGPlugin (filter) { var self = this; self.grid = null; self.scope = null; + self.services = null; - self.init = function (scope, grid) { + self.init = function (scope, grid, services) { self.grid = grid; self.scope = scope; + self.services = services; }; self.export = function () { @@ -15,27 +17,23 @@ gridWidth: self.scope.totalRowWidth(), data: [] }; + angular.forEach(self.scope.columns, function (col) { if (col.visible) { ret.columns.push(col.displayName); ret.columnWidths.push(col.width); } }); - angular.forEach(self.grid.filteredRows, function (item) { + angular.forEach(self.grid.filteredRows, function (row) { + var item = row.entity; angular.forEach(self.scope.columns, function (col) { if (col.visible) { - var obj = ng.utils.evalProperty(item, col.field); - var val = null; - if (col.cellFilter) { - val = filter(col.cellFilter)(obj); - } else { - val = obj; - } + var obj = self.services.UtilityService.evalProperty(item, col.field); + var val = col.cellFilter && filter ? filter(col.cellFilter)(obj) : obj; ret.data.push(val ? val.toString() : ''); } }); }); return ret; }; - return self; -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/directives/ng-grid.js b/src/directives/ng-grid.js index ce13f6c00b..944f6608dc 100644 --- a/src/directives/ng-grid.js +++ b/src/directives/ng-grid.js @@ -134,7 +134,7 @@ options.gridId = grid.gridId; options.ngGrid = grid; options.$gridScope = $scope; - options.$gridServices = { SortService: sortService, DomUtilityService: domUtilityService }; + options.$gridServices = { SortService: sortService, DomUtilityService: domUtilityService, UtilityService: $utils }; $scope.$on('ngGridEventDigestGrid', function(){ domUtilityService.digest($scope.$parent); }); @@ -149,7 +149,7 @@ //initialize plugins. angular.forEach(options.plugins, function (p) { if (typeof p === 'function') { - p = p.call(this); + p = new p(); //If p is a function, then we assume it is a class. } p.init($scope.$new(), grid, options.$gridServices); options.plugins[$utils.getInstanceType(p)] = p; diff --git a/test/unit/pluginsSpec.js b/test/unit/pluginsSpec.js new file mode 100644 index 0000000000..a5a3522cda --- /dev/null +++ b/test/unit/pluginsSpec.js @@ -0,0 +1,104 @@ +'use strict'; + +/* jasmine specs for plugins go here */ + +describe('plugins', function () { + var elm, scope; + + var initalizeHelper = function(gridOptions) { + return inject(function($rootScope, $compile) { + elm = angular.element( + '
' + ); + + scope = $rootScope; + scope.myData = [{ name: "Moroni", age: 50 }, + { name: "Tiancum", age: 43 }, + { name: "Jacob", age: 27 }, + { name: "Nephi", age: 29 }, + { name: "Enos", age: 34 }]; + + scope.gridOptions = $.extend(true, { data: 'myData' }, gridOptions); + + $compile(elm)(scope); + + scope.$digest(); + }); + }; + // Load the ngGrid module + beforeEach(module('ngGrid')); + + describe('ng-grid-wysiwyg-export', function () { + describe('initialization', function () { + var testData = function(data) { + expect(data.columns.length).toBe(2); + expect(data.columns[0]).toBe('name'); + expect(data.columns[1]).toBe('age'); + + expect(data.data.length / data.columns.length).toBe(scope.myData.length); + expect(data.data.length % data.columns.length).toBe(0); + }; + + describe('function-literal', function() { + beforeEach(initalizeHelper({ plugins: [ngGridWYSIWYGPlugin] })); + + it('should work if the plugin being passed in is a function literal', function() { + testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export()); + }); + }); + + describe('instance', function() { + beforeEach(initalizeHelper({ plugins: [new ngGridWYSIWYGPlugin()] })); + + it('should work if the plugin being passed in is an instance', function() { + testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export()); + }); + }); + }); + //describe('filtering', function () { + // var testData = function (data) { + // expect(data.columns.length).toBe(2); + // expect(data.columns[0]).toBe('name'); + // expect(data.columns[1]).toBe('age'); + + // expect(data.data.length).toBe(2); + // }; + + // describe('function-literal', function () { + // beforeEach(initalizeHelper({ + // plugins: [ngGridWYSIWYGPlugin], + // filterOptions: { filterText: '', useExternalFilter: false }, + // })); + + // it('should work if the plugin being passed in is a function literal', function () { + // testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export()); + // }); + // }); + + // describe('instance', function () { + // beforeEach(initalizeHelper({ + // plugins: [new ngGridWYSIWYGPlugin()], + // filterOptions: { filterText: 'Jacob', useExternalFilter: false }, + // })); + + // it('should work if the plugin being passed in is an instance', function () { + // var flag = false; + // runs(function () { + // scope.$digest(); + // setTimeout(function () { + // flag = true; + // scope.$digest(); + // }, 500); + // }); + // waitsFor(function () { + // return flag; + // }); + // runs(function () { + // testData(scope.gridOptions.plugins['ngGridWYSIWYGPlugin'].export()); + // }); + // }); + // }); + //}); + }); + +});