Browse Source

test: add test of data.convert.utils

pull/2088/head
Yoshiya Hinosawa 7 years ago
parent
commit
d39f5a7bc7
  1. 4
      karma.conf.js
  2. 95
      spec/data.convert.utils.js
  3. 4
      spec/shape.line-spec.js
  4. 3
      spec/svg-helper.js
  5. 32
      src/data.convert.utils.js

4
karma.conf.js

@ -6,10 +6,10 @@ module.exports = function(config) {
files: [ files: [
'c3.css', 'c3.css',
'spec/*-helper.js', 'spec/*-helper.js',
'spec/*-spec.js' 'spec/*.js'
], ],
preprocessors: { preprocessors: {
'spec/c3-helper.js': ['browserify'] 'spec/*.js': ['browserify']
}, },
browserify: { browserify: {
debug: true, debug: true,

95
spec/data.convert.utils.js

@ -0,0 +1,95 @@
import { convertColumnsToData, convertRowsToData } from '../src/data.convert.utils';
describe('convertColumnsToData', () => {
it('converts column data to normalized data', () => {
const data = convertColumnsToData([
["cat1", "a", "b", "c", "d"],
["data1", 30, 200, 100, 400],
["cat2", "b", "a", "c", "d", "e", "f"],
["data2", 400, 60, 200, 800, 10, 10]
]);
expect(data).toEqual([{
cat1: 'a',
data1: 30,
cat2: 'b',
data2: 400
}, {
cat1: 'b',
data1: 200,
cat2: 'a',
data2: 60
}, {
cat1: 'c',
data1: 100,
cat2: 'c',
data2: 200
}, {
cat1: 'd',
data1: 400,
cat2: 'd',
data2: 800
}, {
cat2: 'e',
data2: 10
}, {
cat2: 'f',
data2: 10
}]);
});
it('throws when the column data contains undefined', () => {
expect(() => convertColumnsToData([
["cat1", "a", "b", "c", "d"],
["data1", undefined]
])).toThrowError(Error, /Source data is missing a component/);
});
});
describe('convertRowsToData', () => {
it('converts the row data to normalized data', () => {
const data = convertRowsToData([
['data1', 'data2', 'data3'],
[90, 120, 300],
[40, 160, 240],
[50, 200, 290],
[120, 160, 230],
[80, 130, 300],
[90, 220, 320]
]);
expect(data).toEqual([{
data1: 90,
data2: 120,
data3: 300
}, {
data1: 40,
data2: 160,
data3: 240
}, {
data1: 50,
data2: 200,
data3: 290
}, {
data1: 120,
data2: 160,
data3: 230
}, {
data1: 80,
data2: 130,
data3: 300
}, {
data1: 90,
data2: 220,
data3: 320
}]);
});
it('throws when the row data contains undefined', () => {
expect(() => convertRowsToData([
['data1', 'data2', 'data3'],
[40, 160, 240],
[90, 120, undefined]
])).toThrowError(Error, /Source data is missing a component/);
});
});

4
spec/shape.line-spec.js

@ -1,3 +1,5 @@
import { parseSvgPath } from './svg-helper';
describe('c3 chart shape line', function () { describe('c3 chart shape line', function () {
'use strict'; 'use strict';
@ -7,8 +9,6 @@ describe('c3 chart shape line', function () {
chart = window.initChart(chart, args, done); chart = window.initChart(chart, args, done);
}); });
var parseSvgPath = window.parseSvgPath;
describe('shape-rendering for line chart', function () { describe('shape-rendering for line chart', function () {
beforeAll(function () { beforeAll(function () {

3
spec/svg-helper.js

@ -3,8 +3,7 @@
* @param {String} d SvgPath d attribute.] * @param {String} d SvgPath d attribute.]
* @return {Array} an array of drawing commands. * @return {Array} an array of drawing commands.
*/ */
export function parseSvgPath(d) { //jshint ignore:line
function parseSvgPath(d) { //jshint ignore:line
'use strict'; 'use strict';
var commands = []; var commands = [];

32
src/data.convert.utils.js

@ -6,21 +6,20 @@ import { isUndefined } from './util';
* @return {any[][]} * @return {any[][]}
*/ */
export const convertRowsToData = (rows) => { export const convertRowsToData = (rows) => {
const new_rows = []; const newRows = [];
const keys = rows[0]; const keys = rows[0];
let new_row , i, j;
for (i = 1; i < rows.length; i++) { for (let i = 1; i < rows.length; i++) {
new_row = {}; const newRow = {};
for (j = 0; j < rows[i].length; j++) { for (let j = 0; j < rows[i].length; j++) {
if (isUndefined(rows[i][j])) { if (isUndefined(rows[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!"); throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
} }
new_row[keys[j]] = rows[i][j]; newRow[keys[j]] = rows[i][j];
} }
new_rows.push(new_row); newRows.push(newRow);
} }
return new_rows; return newRows;
}; };
/** /**
@ -29,21 +28,20 @@ export const convertRowsToData = (rows) => {
* @return {any[][]} * @return {any[][]}
*/ */
export const convertColumnsToData = (columns) => { export const convertColumnsToData = (columns) => {
const new_rows = []; const newRows = [];
let i, j, key;
for (i = 0; i < columns.length; i++) { for (let i = 0; i < columns.length; i++) {
key = columns[i][0]; const key = columns[i][0];
for (j = 1; j < columns[i].length; j++) { for (let j = 1; j < columns[i].length; j++) {
if (isUndefined(new_rows[j - 1])) { if (isUndefined(newRows[j - 1])) {
new_rows[j - 1] = {}; newRows[j - 1] = {};
} }
if (isUndefined(columns[i][j])) { if (isUndefined(columns[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!"); throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
} }
new_rows[j - 1][key] = columns[i][j]; newRows[j - 1][key] = columns[i][j];
} }
} }
return new_rows; return newRows;
}; };

Loading…
Cancel
Save