|
| 1 | +import { BidirectionalBar } from '../../../../src'; |
| 2 | +import { data } from '../../../data/bi-directional'; |
| 3 | +import { createDiv } from '../../../utils/dom'; |
| 4 | + |
| 5 | +describe('Bidirectional', () => { |
| 6 | + it('default', () => { |
| 7 | + const bidirectional = new BidirectionalBar(createDiv('default'), { |
| 8 | + width: 400, |
| 9 | + height: 400, |
| 10 | + data, |
| 11 | + xField: 'country', |
| 12 | + yField: ['2016年耕地总面积', '2016年转基因种植面积'], |
| 13 | + }); |
| 14 | + bidirectional.render(); |
| 15 | + |
| 16 | + expect(bidirectional.type).toEqual('bidirectional-bar'); |
| 17 | + |
| 18 | + const leftView = bidirectional.chart.views[0]; |
| 19 | + const rightView = bidirectional.chart.views[1]; |
| 20 | + const leftG = bidirectional.chart.views[0].geometries[0]; |
| 21 | + const rightG = bidirectional.chart.views[1].geometries[0]; |
| 22 | + //@ts-ignore |
| 23 | + expect(leftView.options.axes.country.position).toEqual('top'); |
| 24 | + //@ts-ignore |
| 25 | + expect(rightView.options.axes.country).toEqual(false); |
| 26 | + expect(bidirectional.chart.getOptions().scales.type.sync).toEqual(true); |
| 27 | + |
| 28 | + // 类型 |
| 29 | + expect(leftG.type).toBe('interval'); |
| 30 | + expect(rightG.type).toBe('interval'); |
| 31 | + |
| 32 | + // x & y |
| 33 | + const LpositionFields = leftG.getAttribute('position').getFields(); |
| 34 | + const RpositionFields = rightG.getAttribute('position').getFields(); |
| 35 | + expect(LpositionFields).toHaveLength(2); |
| 36 | + expect(LpositionFields[0]).toBe('country'); |
| 37 | + expect(LpositionFields[1]).toBe('2016年耕地总面积'); |
| 38 | + |
| 39 | + expect(RpositionFields).toHaveLength(2); |
| 40 | + expect(RpositionFields[0]).toBe('country'); |
| 41 | + expect(RpositionFields[1]).toBe('2016年转基因种植面积'); |
| 42 | + |
| 43 | + const LcolorAttribute = leftG.getAttribute('color'); |
| 44 | + const LseriesFields = LcolorAttribute.getFields(); |
| 45 | + |
| 46 | + expect(LseriesFields).toHaveLength(1); |
| 47 | + expect(LseriesFields[0]).toBe('type'); |
| 48 | + |
| 49 | + const RcolorAttribute = rightG.getAttribute('color'); |
| 50 | + const RseriesFields = RcolorAttribute.getFields(); |
| 51 | + |
| 52 | + expect(RseriesFields).toHaveLength(1); |
| 53 | + expect(RseriesFields[0]).toBe('type'); |
| 54 | + |
| 55 | + expect(bidirectional.chart.getController('legend').visible).toEqual(true); |
| 56 | + |
| 57 | + expect(bidirectional.chart.getController('legend').getComponents()[0].direction).toEqual('bottom'); |
| 58 | + |
| 59 | + expect(bidirectional.chart.getController('legend').getComponents()[0].extra.scale.field).toEqual('type'); |
| 60 | + }); |
| 61 | + it('x*y*color', () => { |
| 62 | + const bidirectional = new BidirectionalBar(createDiv('x*y*color'), { |
| 63 | + width: 400, |
| 64 | + height: 400, |
| 65 | + data, |
| 66 | + xField: 'country', |
| 67 | + yField: ['2016年耕地总面积', '2016年转基因种植面积'], |
| 68 | + }); |
| 69 | + bidirectional.render(); |
| 70 | + |
| 71 | + const leftG = bidirectional.chart.views[0].geometries[0]; |
| 72 | + const rightG = bidirectional.chart.views[1].geometries[0]; |
| 73 | + const LseriesFields = leftG.getAttribute('color').getFields(); |
| 74 | + const RseriesFields = rightG.getAttribute('color').getFields(); |
| 75 | + |
| 76 | + expect(LseriesFields).toHaveLength(1); |
| 77 | + expect(LseriesFields[0]).toBe('type'); |
| 78 | + expect(RseriesFields).toHaveLength(1); |
| 79 | + expect(RseriesFields[0]).toBe('type'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('x*y*color with color', () => { |
| 83 | + const palette = ['red', 'green']; |
| 84 | + const bidirectional = new BidirectionalBar(createDiv('x*y*color with color'), { |
| 85 | + width: 400, |
| 86 | + height: 400, |
| 87 | + data, |
| 88 | + xField: 'country', |
| 89 | + yField: ['2016年耕地总面积', '2016年转基因种植面积'], |
| 90 | + color: palette, |
| 91 | + }); |
| 92 | + bidirectional.render(); |
| 93 | + |
| 94 | + bidirectional.render(); |
| 95 | + |
| 96 | + const leftG = bidirectional.chart.views[0].geometries[0]; |
| 97 | + const rightG = bidirectional.chart.views[1].geometries[0]; |
| 98 | + |
| 99 | + leftG.elements.forEach((element) => { |
| 100 | + const color = element.getModel().color; |
| 101 | + expect(color).toBe(palette[0]); |
| 102 | + }); |
| 103 | + |
| 104 | + rightG.elements.forEach((element) => { |
| 105 | + const color = element.getModel().color; |
| 106 | + expect(color).toBe(palette[1]); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('widthRatio', () => { |
| 111 | + const bidirectional = new BidirectionalBar(createDiv('widthRatio'), { |
| 112 | + width: 400, |
| 113 | + height: 400, |
| 114 | + data, |
| 115 | + xField: 'country', |
| 116 | + yField: ['2016年耕地总面积', '2016年转基因种植面积'], |
| 117 | + widthRatio: 0.7, |
| 118 | + }); |
| 119 | + bidirectional.render(); |
| 120 | + |
| 121 | + const leftG = bidirectional.chart.views[0].geometries[0]; |
| 122 | + const rightG = bidirectional.chart.views[1].geometries[0]; |
| 123 | + expect(leftG.theme.columnWidthRatio).toBe(0.7); |
| 124 | + expect(rightG.theme.columnWidthRatio).toBe(0.7); |
| 125 | + }); |
| 126 | +}); |
0 commit comments