|
| 1 | +describe('dc.filter-dates', function() { |
| 2 | + // do date filters work correctly? |
| 3 | + // adapted from a fiddle demonstrating the problem by Matt Traynham |
| 4 | + // see it fail with 1.7.1: http://jsfiddle.net/gordonwoodhull/Q2H9C/4/ |
| 5 | + // see it win with 2.0: http://jsfiddle.net/gordonwoodhull/Q2H9C/3/ |
| 6 | + // (Thanks!!) |
| 7 | + |
| 8 | + var group, dateDim1, dateDim2, group1, group2, |
| 9 | + row1, row2; |
| 10 | + var width = 400; |
| 11 | + var height = 200; |
| 12 | + var margins = {top: 15, right: 10, bottom: 20, left: 40}; |
| 13 | + beforeEach(function () { |
| 14 | + // Months are 0 indexed... |
| 15 | + var start = new Date(2013, 10, 1); |
| 16 | + var end = new Date(2013, 11, 1); |
| 17 | + var stringLength = 2; |
| 18 | + |
| 19 | + // Generate Random Data [Date, VowelString, Random Number, Random Measure] |
| 20 | + var data = []; |
| 21 | + for(var i = 0; i < 2000; i++) { |
| 22 | + data[i] = [ |
| 23 | + randomDate(start, end), |
| 24 | + randomVowelString(stringLength), |
| 25 | + Math.floor(Math.random() * 20), |
| 26 | + Math.floor(Math.random() * 30000) |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + var ndx = crossfilter(data); |
| 31 | + dateDim1 = ndx.dimension(function(d) { return d[0]; }); |
| 32 | + dateDim2 = ndx.dimension(function(d) { return d[0]; }); |
| 33 | + |
| 34 | + group1 = dateDim1.group().reduceSum(function(d) { return d[3]; }); |
| 35 | + group2 = dateDim2.group().reduceSum(function(d) { return d[3]; }); |
| 36 | + |
| 37 | + appendChartID(row1); |
| 38 | + appendChartID(row2); |
| 39 | + |
| 40 | + row1 = dc.rowChart("row1") |
| 41 | + .width(width) |
| 42 | + .height(height) |
| 43 | + .margins(margins) |
| 44 | + .dimension(dateDim1) |
| 45 | + .group(group1) |
| 46 | + .gap(1) |
| 47 | + .render(); |
| 48 | + |
| 49 | + row2 = dc.rowChart("row2") |
| 50 | + .width(width) |
| 51 | + .height(height) |
| 52 | + .margins(margins) |
| 53 | + .dimension(dateDim2) |
| 54 | + .group(group2) |
| 55 | + .gap(1) |
| 56 | + .render(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('filtering on 11/8 should keep only that row', function() { |
| 60 | + row1.filter(new Date(2013, 10, 8)); |
| 61 | + expect(group1.all()[6].value).not.toEqual(0); |
| 62 | + expect(group2.all()[6].value).toEqual(0); |
| 63 | + expect(group2.all()[7]).toEqual(group1.all()[7]); |
| 64 | + expect(group2.all()[8].value).toEqual(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it('filtering on 11/17 should keep only that row', function() { |
| 68 | + row1.filter(new Date(2013, 10, 17)); |
| 69 | + expect(group1.all()[15].value).not.toEqual(0); |
| 70 | + expect(group2.all()[15].value).toEqual(0); |
| 71 | + expect(group2.all()[16]).toEqual(group1.all()[16]); |
| 72 | + expect(group2.all()[17].value).toEqual(0); |
| 73 | + }); |
| 74 | + |
| 75 | + // Create a Random Date |
| 76 | + function randomDate(start, end) { |
| 77 | + var d = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())); |
| 78 | + d.setHours(0,0,0,0); |
| 79 | + return d; |
| 80 | + } |
| 81 | + |
| 82 | + // Create a Random String of vowels |
| 83 | + var vowels = ["a","e","i","o","u","y"]; |
| 84 | + function randomVowelString(length) { |
| 85 | + var val = ""; |
| 86 | + for(var i = 0; i < length; i++) { |
| 87 | + val = val + vowels[Math.floor(Math.random() * (vowels.length - 1))]; |
| 88 | + } |
| 89 | + return val; |
| 90 | + } |
| 91 | +}); |
0 commit comments