|
| 1 | +import { jsx, Component, Canvas, Chart, Line, Legend, Tooltip, TextGuide } from '../../../src'; |
| 2 | +import { Layout } from '../../../src/components'; |
| 3 | +import { createContext, delay } from '../../util'; |
| 4 | +const data1 = [ |
| 5 | + { x: 0, y: 1, name: 'A' }, |
| 6 | + { x: 1, y: 2, name: 'B' }, |
| 7 | + { x: 2, y: 3, name: 'C' }, |
| 8 | + { x: 3, y: 4, name: 'D' }, |
| 9 | +]; |
| 10 | + |
| 11 | +const data2 = [ |
| 12 | + { x: 0, y: 4, name: 'E' }, |
| 13 | + { x: 1, y: 3, name: 'F' }, |
| 14 | + { x: 2, y: 2, name: 'G' }, |
| 15 | + { x: 3, y: 1, name: 'H' }, |
| 16 | +]; |
| 17 | + |
| 18 | +const ChartA = (props) => { |
| 19 | + const { data, color, style } = props; |
| 20 | + return ( |
| 21 | + <Chart data={data} color={color} style={style}> |
| 22 | + <Line x="x" y="y" color={color} /> |
| 23 | + <Legend /> |
| 24 | + <Tooltip /> |
| 25 | + <TextGuide content={`textGuide`} records={[data[1]]} /> |
| 26 | + </Chart> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +describe('布局组件', () => { |
| 31 | + describe('布局类型', () => { |
| 32 | + it('横向布局', async () => { |
| 33 | + const context = createContext('横向布局', { |
| 34 | + width: '300px', |
| 35 | + height: '100px', |
| 36 | + }); |
| 37 | + const { props } = ( |
| 38 | + <Canvas context={context} pixelRatio={1}> |
| 39 | + <Layout type="horizontal"> |
| 40 | + <rect style={{ width: '50px', height: '50px', fill: '#FF5733' }} /> |
| 41 | + <rect style={{ width: '50px', height: '50px', fill: '#33FF57' }} /> |
| 42 | + <rect style={{ width: '50px', height: '50px', fill: '#3357FF' }} /> |
| 43 | + </Layout> |
| 44 | + </Canvas> |
| 45 | + ); |
| 46 | + const canvas = new Canvas(props); |
| 47 | + await canvas.render(); |
| 48 | + |
| 49 | + await delay(1000); |
| 50 | + expect(context).toMatchImageSnapshot(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('圆形布局', async () => { |
| 54 | + const context = createContext('圆形布局', { |
| 55 | + width: '300px', |
| 56 | + height: '100px', |
| 57 | + }); |
| 58 | + const { props } = ( |
| 59 | + <Canvas context={context} pixelRatio={1}> |
| 60 | + <Layout type="circular"> |
| 61 | + <circle style={{ r: '10px', fill: '#FF6B6B' }} /> |
| 62 | + <circle style={{ r: '10px', fill: '#4ECDC4' }} /> |
| 63 | + <circle style={{ r: '10px', fill: '#45B7D1' }} /> |
| 64 | + <circle style={{ r: '10px', fill: '#96CEB4' }} /> |
| 65 | + <circle style={{ r: '10px', fill: '#FFEEAD' }} /> |
| 66 | + <circle style={{ r: '10px', fill: '#D4A5A5' }} /> |
| 67 | + </Layout> |
| 68 | + </Canvas> |
| 69 | + ); |
| 70 | + const canvas = new Canvas(props); |
| 71 | + await canvas.render(); |
| 72 | + |
| 73 | + await delay(1000); |
| 74 | + expect(context).toMatchImageSnapshot(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('横向布局包含两个图表', async () => { |
| 78 | + const context = createContext('横向布局', { |
| 79 | + width: '300px', |
| 80 | + height: '100px', |
| 81 | + }); |
| 82 | + const { props } = ( |
| 83 | + <Canvas context={context}> |
| 84 | + <Layout |
| 85 | + type="horizontal" |
| 86 | + itemStyle={{ |
| 87 | + stroke: '#333333', |
| 88 | + lineWidth: 1, |
| 89 | + }} |
| 90 | + > |
| 91 | + <ChartA data={data1} color="#4ECDC4" style={{ height: '120px' }} /> |
| 92 | + <ChartA data={data2} color="#FF6B6B" style={{ height: '80px' }} /> |
| 93 | + </Layout> |
| 94 | + </Canvas> |
| 95 | + ); |
| 96 | + const canvas = new Canvas(props); |
| 97 | + await canvas.render(); |
| 98 | + |
| 99 | + await delay(1000); |
| 100 | + expect(context).toMatchImageSnapshot(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('纵向布局包含两个图表', async () => { |
| 104 | + const context = createContext('纵向布局包含两个图表', { |
| 105 | + width: '200px', |
| 106 | + height: '200px', |
| 107 | + }); |
| 108 | + |
| 109 | + const { props } = ( |
| 110 | + <Canvas context={context}> |
| 111 | + <Layout |
| 112 | + type="vertical" |
| 113 | + itemStyle={{ |
| 114 | + stroke: '#333333', |
| 115 | + lineWidth: 1, |
| 116 | + }} |
| 117 | + > |
| 118 | + <ChartA data={data1} color="#4ECDC4" /> |
| 119 | + <ChartA data={data2} color="#FF6B6B" /> |
| 120 | + </Layout> |
| 121 | + </Canvas> |
| 122 | + ); |
| 123 | + const canvas = new Canvas(props); |
| 124 | + await canvas.render(); |
| 125 | + |
| 126 | + await delay(1000); |
| 127 | + expect(context).toMatchImageSnapshot(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('Grid布局', async () => { |
| 131 | + const context = createContext('Grid布局', { |
| 132 | + width: '300px', |
| 133 | + height: '300px', |
| 134 | + }); |
| 135 | + |
| 136 | + const { props } = ( |
| 137 | + <Canvas context={context}> |
| 138 | + <Layout |
| 139 | + type="grid" |
| 140 | + columns={2} |
| 141 | + itemStyle={{ |
| 142 | + stroke: '#333333', |
| 143 | + lineWidth: 1, |
| 144 | + }} |
| 145 | + > |
| 146 | + {/* 第一行 */} |
| 147 | + <ChartA data={data1} color="#4ECDC4" style={{ height: '120px' }} /> |
| 148 | + <ChartA data={data2} color="#FF6B6B" style={{ height: '120px' }} /> |
| 149 | + {/* 第二行 */} |
| 150 | + <ChartA data={data1} color="#96CEB4" style={{ height: '120px' }} /> |
| 151 | + <ChartA data={data2} color="#FFEEAD" style={{ height: '120px' }} /> |
| 152 | + </Layout> |
| 153 | + </Canvas> |
| 154 | + ); |
| 155 | + const canvas = new Canvas(props); |
| 156 | + await canvas.render(); |
| 157 | + |
| 158 | + await delay(1000); |
| 159 | + expect(context).toMatchImageSnapshot(); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments