Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to non existing row key when getting aggregator #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ class PivotData {
} else if (colKey.length === 0) {
agg = this.rowTotals[flatRowKey];
} else {
agg = this.tree[flatRowKey][flatColKey];
agg = (this.tree[flatRowKey] || {})[flatColKey];
}
return (
agg || {
Expand Down
34 changes: 31 additions & 3 deletions src/__tests__/Utilities-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ const fixtureData = [
describe(' utils', function() {
describe('.PivotData()', function() {
describe('with no options', function() {
const aoaInput = [['a', 'b'], [1, 2], [3, 4]];
const aoaInput = [
['a', 'b'],
[1, 2],
[3, 4],
];
const pd = new utils.PivotData({data: aoaInput});

it('has the correct grand total value', () =>
expect(pd.getAggregator([], []).value()).toBe(2));
});

describe('with array-of-array input', function() {
const aoaInput = [['a', 'b'], [1, 2], [3, 4]];
const aoaInput = [
['a', 'b'],
[1, 2],
[3, 4],
];
const pd = new utils.PivotData({
data: aoaInput,
aggregatorName: 'Sum over Sum',
Expand All @@ -39,7 +47,10 @@ describe(' utils', function() {
});

describe('with array-of-object input', function() {
const aosInput = [{a: 1, b: 2}, {a: 3, b: 4}];
const aosInput = [
{a: 1, b: 2},
{a: 3, b: 4},
];
const pd = new utils.PivotData({
data: aosInput,
aggregatorName: 'Sum over Sum',
Expand Down Expand Up @@ -129,6 +140,23 @@ describe(' utils', function() {
expect(agg.format(val)).toBe('1');
});

it('returns empty aggregator when key not found', function() {
// inexistent row key, empty col key
let agg = pd.getAggregator(['x', 'y'], []);
let val = agg.value();
expect(val).toBe(null);

// empty row key, inexistent col key
agg = pd.getAggregator([], [1, 2]);
val = agg.value();
expect(val).toBe(null);

// inexistent row key, non empty col key
agg = pd.getAggregator(['x', 'y'], [1, 2]);
val = agg.value();
expect(val).toBe(null);
});

it('has a correct grand total aggregator', function() {
const agg = pd.getAggregator([], []);
const val = agg.value();
Expand Down