|
| 1 | +describe('dc.selectMenu', function() { |
| 2 | + var id, chart; |
| 3 | + var data, regionDimension, regionGroup; |
| 4 | + var stateDimension, stateGroup; |
| 5 | + |
| 6 | + beforeEach(function () { |
| 7 | + data = crossfilter(loadDateFixture()); |
| 8 | + regionDimension = data.dimension(function(d) { return d.region; }); |
| 9 | + stateDimension = data.dimension(function(d) { return d.state; }); |
| 10 | + |
| 11 | + regionGroup = regionDimension.group(); |
| 12 | + stateGroup = stateDimension.group(); |
| 13 | + |
| 14 | + id = 'seclect-menu'; |
| 15 | + appendChartID(id); |
| 16 | + |
| 17 | + chart = dc.selectMenu("#" + id); |
| 18 | + chart.dimension(stateDimension) |
| 19 | + .group(stateGroup) |
| 20 | + .transitionDuration(0); |
| 21 | + chart.render(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('generation', function () { |
| 25 | + it('we get something', function() { |
| 26 | + expect(chart).not.toBeNull(); |
| 27 | + }); |
| 28 | + it('should be registered', function() { |
| 29 | + expect(dc.hasChart(chart)).toBeTruthy(); |
| 30 | + }); |
| 31 | + it('sets order', function() { |
| 32 | + expect(chart.order()).toBeDefined(); |
| 33 | + }); |
| 34 | + it('sets prompt text', function() { |
| 35 | + expect(chart.promptText()).toBe("Select all"); |
| 36 | + }); |
| 37 | + it('creates select tag', function() { |
| 38 | + expect(chart.selectAll("select").length).toEqual(1); |
| 39 | + }); |
| 40 | + it('creates prompt option with empty value', function() { |
| 41 | + var option = chart.selectAll("option")[0][0]; |
| 42 | + expect(option).toBeTruthy(); |
| 43 | + expect(option.value).toEqual(""); |
| 44 | + }); |
| 45 | + it('creates prompt option with default prompt text', function() { |
| 46 | + var option = chart.selectAll("option")[0][0]; |
| 47 | + expect(option.text).toEqual("Select all"); |
| 48 | + }); |
| 49 | + it('creates correct number of options', function() { |
| 50 | + expect(chart.selectAll("option.dc-select-option")[0].length).toEqual(stateGroup.all().length); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('select options', function(){ |
| 55 | + var first_option, last_option, last_index; |
| 56 | + beforeEach(function () { |
| 57 | + last_index = stateGroup.all().length - 1; |
| 58 | + first_option = getOption(chart,0); |
| 59 | + last_option = getOption(chart,last_index); |
| 60 | + }); |
| 61 | + it('display title as default option text', function() { |
| 62 | + expect(first_option.text).toEqual("California: 3"); |
| 63 | + }); |
| 64 | + it('text property can be changed by changing title', function() { |
| 65 | + chart.title(function(d){ return d.key }).redraw(); |
| 66 | + first_option = getOption(chart,0); |
| 67 | + expect(first_option.text).toEqual("California"); |
| 68 | + }); |
| 69 | + it('are ordered by ascending group key by default', function(){ |
| 70 | + expect(first_option.text).toEqual("California: 3"); |
| 71 | + expect(last_option.text).toEqual("Ontario: 2"); |
| 72 | + }); |
| 73 | + it('order can be changed by changing order function', function(){ |
| 74 | + chart.order(function(a,b) { return a.key.length - b.key.length; }); |
| 75 | + chart.redraw(); |
| 76 | + last_option = getOption(chart,last_index); |
| 77 | + expect(last_option.text).toEqual("Mississippi: 2"); |
| 78 | + }); |
| 79 | + }) |
| 80 | + |
| 81 | + describe('selecting an option', function () { |
| 82 | + it('filters dimension based on selected option\'s value', function(){ |
| 83 | + chart.onChange(stateGroup.all()[0].key); |
| 84 | + expect(chart.filter()).toEqual("California"); |
| 85 | + }); |
| 86 | + it('replaces filter on second selection', function(){ |
| 87 | + chart.onChange(stateGroup.all()[0].key); |
| 88 | + chart.onChange(stateGroup.all()[1].key); |
| 89 | + expect(chart.filter()).toEqual("Colorado"); |
| 90 | + expect(chart.filters().length).toEqual(1); |
| 91 | + }); |
| 92 | + it('actually filters dimension', function(){ |
| 93 | + chart.onChange(stateGroup.all()[0].key); |
| 94 | + expect(regionGroup.all()[0].value).toEqual(0); |
| 95 | + expect(regionGroup.all()[3].value).toEqual(2); |
| 96 | + }); |
| 97 | + it('removes filter when prompt option is selected', function(){ |
| 98 | + chart.onChange(''); |
| 99 | + expect(chart.hasFilter()).not.toBeTruthy(); |
| 100 | + expect(regionGroup.all()[0].value).toEqual(1); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('redraw with existing filter', function () { |
| 105 | + it('selects option corresponding to active filter', function(){ |
| 106 | + chart.onChange(stateGroup.all()[0].key); |
| 107 | + chart.redraw(); |
| 108 | + expect(chart.selectAll("select")[0][0].value).toEqual("California"); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('filterDisplayed', function () { |
| 113 | + it('only displays options whose value > 0 by default', function(){ |
| 114 | + regionDimension.filter('South'); |
| 115 | + chart.redraw(); |
| 116 | + expect(chart.selectAll("option.dc-select-option")[0].length).toEqual(1); |
| 117 | + expect(getOption(chart,0).text).toEqual("California: 2"); |
| 118 | + }); |
| 119 | + it('can be overridden', function(){ |
| 120 | + regionDimension.filter('South'); |
| 121 | + chart.filterDisplayed(function(d) { return true; }).redraw(); |
| 122 | + expect(chart.selectAll("option.dc-select-option")[0].length).toEqual(stateGroup.all().length); |
| 123 | + expect(getOption(chart, stateGroup.all().length - 1).text).toEqual("Ontario: 0"); |
| 124 | + }); |
| 125 | + it('retains order with filtered options', function(){ |
| 126 | + regionDimension.filter('Central'); |
| 127 | + chart.redraw(); |
| 128 | + expect(getOption(chart,0).text).toEqual('Mississippi: 2'); |
| 129 | + expect(getOption(chart,1).text).toEqual('Ontario: 1'); |
| 130 | + }); |
| 131 | + afterEach(function(){ |
| 132 | + regionDimension.filterAll(); |
| 133 | + }) |
| 134 | + }); |
| 135 | + |
| 136 | + function getOption(chart, i){ |
| 137 | + return chart.selectAll("option.dc-select-option")[0][i]; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | +}); |
| 142 | + |
0 commit comments