Skip to content

Commit

Permalink
added unit test for csv-label case'
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Oct 6, 2015
1 parent 67a88a4 commit 4ed3e82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/ng-csv/services/csv-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ angular.module('ngCsv.services').
csvContent += headerString + EOL;
}

var arrData = [];

if (angular.isArray(responseData)) {
arrData = responseData;
}
else if (angular.isFunction(responseData)) {
arrData = responseData();
}

// Check if using keys as labels
if (angular.isDefined(options.label) && options.label && typeof options.label === 'boolean') {
var encodingArray, labelString;
Expand All @@ -99,15 +108,6 @@ angular.module('ngCsv.services').
csvContent += labelString + EOL;
}

var arrData = [];

if (angular.isArray(responseData)) {
arrData = responseData;
}
else if (angular.isFunction(responseData)) {
arrData = responseData();
}

angular.forEach(arrData, function (oldRow, index) {
var row = angular.copy(arrData[index]);
var dataString, infoArray;
Expand Down
21 changes: 21 additions & 0 deletions test/unit/ngCsv/directives/ngCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ describe('ngCsv directive', function () {
scope.$apply();
});

it('Creates a header row using keys if csv-label sets to true', function (done) {
// Compile a piece of HTML containing the directive
$rootScope.testDelim = [ {a:1, b:2, c:3}, {a:4, b:5, c:6} ];
var element = $compile(
'<div ng-csv="testDelim" csv-label="true" filename="custom.csv"></div>')($rootScope);

$rootScope.$digest();

var scope = element.isolateScope();

// Check that the compiled element contains the templated content
expect(scope.$eval(scope.data)).toEqual($rootScope.testDelim);


scope.buildCSV(scope.data).then(function() {
expect(scope.csv).toBe('a,b,c\r\n1,2,3\r\n4,5,6\r\n');
done();
});
scope.$apply();
});

it('Accepts optional csv-column-order attribute (input array)', function (done) {
$rootScope.testDelim = [ {a:1, b:2, c:3}, {a:4, b:5, c:6} ];
$rootScope.order = [ 'b', 'a', 'c' ];
Expand Down

0 comments on commit 4ed3e82

Please sign in to comment.