-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cap default take from front but allow back
for #1269 add cap mixin tests too
- Loading branch information
1 parent
533748c
commit fc69f54
Showing
2 changed files
with
197 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* global loadDateFixture */ | ||
describe('dc.capMixin', function () { | ||
var data, dimension, group; | ||
var mixin, total; | ||
|
||
beforeEach(function () { | ||
data = crossfilter(loadDateFixture()); | ||
dimension = data.dimension(function (d) { return +d.value; }); | ||
group = dimension.group().reduceSum(function (d) {return Math.abs(+d.nvalue);}); | ||
total = d3.sum(group.all().map(dc.pluck('value'))); | ||
|
||
mixin = dc.capMixin(dc.baseMixin({})); | ||
|
||
mixin.dimension(dimension) | ||
.group(group); | ||
}); | ||
|
||
describe('with no capping and default ordering', function () { | ||
it('should include everything', function () { | ||
expect(mixin.data().length).toBe(5); | ||
}); | ||
it('should be in key order', function () { | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([22, 33, 44, 55, 66]); | ||
}); | ||
it('should sum to total', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(total); | ||
}); | ||
}); | ||
|
||
describe('with no capping and descending key ordering', function () { | ||
beforeEach(function () { | ||
mixin.ordering(function (kv) { return -kv.key; }); | ||
}); | ||
it('should include everything', function () { | ||
expect(mixin.data().length).toBe(5); | ||
}); | ||
it('should be in reverse key order', function () { | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([66, 55, 44, 33, 22]); | ||
}); | ||
}); | ||
|
||
describe('with descending value ordering', function () { | ||
beforeEach(function () { | ||
mixin.ordering(function (kv) { return -kv.value; }); | ||
}); | ||
describe('and no capping', function () { | ||
it('should include everything', function () { | ||
expect(mixin.data().length).toBe(5); | ||
}); | ||
it('should be in reverse key order', function () { | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([22, 44, 55, 66, 33]); | ||
}); | ||
it('should sum to total', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(total); | ||
}); | ||
}); | ||
describe('and cap(3)', function () { | ||
beforeEach(function () { | ||
mixin.cap(3); | ||
}); | ||
it('should have 3 front elements plus others element', function () { | ||
expect(mixin.data().length).toBe(4); | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([22, 44, 55, 'Others']); | ||
}); | ||
it('should sum to total', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(total); | ||
}); | ||
|
||
describe('and taking from back', function () { | ||
beforeEach(function () { | ||
mixin.takeFront(false); | ||
}); | ||
it('should have 3 back elements plus others element', function () { | ||
expect(mixin.data().length).toBe(4); | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([55, 66, 33, 'Others']); | ||
}); | ||
it('should sum to total', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(total); | ||
}); | ||
}); | ||
|
||
describe('and no othersGrouper', function () { | ||
beforeEach(function () { | ||
mixin.othersGrouper(null); | ||
}); | ||
it('should have 3 front elements', function () { | ||
expect(mixin.data().length).toBe(3); | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([22, 44, 55]); | ||
}); | ||
it('should sum to correct number', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(29); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with ascending value ordering', function () { | ||
beforeEach(function () { | ||
mixin.ordering(function (kv) { return kv.value; }); | ||
}); | ||
describe('and no capping', function () { | ||
it('should include everything', function () { | ||
expect(mixin.data().length).toBe(5); | ||
}); | ||
it('should be in reverse key order', function () { | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([33, 66, 55, 44, 22]); | ||
}); | ||
it('should sum to total', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(total); | ||
}); | ||
}); | ||
describe('and cap(3)', function () { | ||
beforeEach(function () { | ||
mixin.cap(3); | ||
}); | ||
it('should have 3 front elements plus others element', function () { | ||
expect(mixin.data().length).toBe(4); | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([33, 66, 55, 'Others']); | ||
}); | ||
it('should sum to total', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(total); | ||
}); | ||
|
||
describe('and taking from back', function () { | ||
beforeEach(function () { | ||
mixin.takeFront(false); | ||
}); | ||
it('should have 3 back elements plus others element', function () { | ||
expect(mixin.data().length).toBe(4); | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([55, 44, 22, 'Others']); | ||
}); | ||
it('should sum to total', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(total); | ||
}); | ||
}); | ||
|
||
describe('and no othersGrouper', function () { | ||
beforeEach(function () { | ||
mixin.othersGrouper(null); | ||
}); | ||
it('should have 3 front elements', function () { | ||
expect(mixin.data().length).toBe(3); | ||
expect(mixin.data().map(dc.pluck('key'))).toEqual([33, 66, 55]); | ||
}); | ||
it('should sum to correct number', function () { | ||
expect(d3.sum(mixin.data().map(dc.pluck('value')))).toEqual(14); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fc69f54
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning this up Gordon.