|
| 1 | +import { jsx, Canvas, Component } from '../../../src'; |
| 2 | +import { computeComponentBBox } from '../../../src/canvas/render'; |
| 3 | +import { createContext, delay } from '@antv/f-test-utils'; |
| 4 | + |
| 5 | +class View extends Component { |
| 6 | + willMount(): void {} |
| 7 | + render() { |
| 8 | + const { position } = this.props; |
| 9 | + return ( |
| 10 | + <group |
| 11 | + style={{ |
| 12 | + fill: 'red', |
| 13 | + }} |
| 14 | + > |
| 15 | + <circle |
| 16 | + style={{ |
| 17 | + cx: position[0], |
| 18 | + cy: position[1], |
| 19 | + r: 20, |
| 20 | + fill: 'red', |
| 21 | + }} |
| 22 | + /> |
| 23 | + </group> |
| 24 | + ); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class ViewWithDisplayGroup extends Component { |
| 29 | + willMount(): void {} |
| 30 | + render() { |
| 31 | + const { style, position = [20, 0] } = this.props; |
| 32 | + return ( |
| 33 | + <group |
| 34 | + style={{ |
| 35 | + fill: 'blue', |
| 36 | + display: 'flex', |
| 37 | + x: position[0], |
| 38 | + y: position[1], |
| 39 | + ...style, |
| 40 | + alignItems: 'center', |
| 41 | + justifyContent: 'center', |
| 42 | + }} |
| 43 | + > |
| 44 | + <text |
| 45 | + style={{ |
| 46 | + text: 'hello', |
| 47 | + textAlign: 'start', |
| 48 | + }} |
| 49 | + /> |
| 50 | + </group> |
| 51 | + ); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class ViewText extends Component { |
| 56 | + willMount(): void {} |
| 57 | + render() { |
| 58 | + const { position } = this.props; |
| 59 | + return ( |
| 60 | + <text |
| 61 | + style={{ |
| 62 | + x: position[0], |
| 63 | + y: position[1], |
| 64 | + text: 'hello', |
| 65 | + }} |
| 66 | + /> |
| 67 | + ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +class GuideGroup extends Component { |
| 72 | + bbox = []; |
| 73 | + |
| 74 | + willMount(): void { |
| 75 | + const { children } = this.props; |
| 76 | + const { updater, context } = this; |
| 77 | + const childArray = Array.isArray(children) ? children : [children]; |
| 78 | + childArray.map((child) => { |
| 79 | + const component = new child.type(child.props, context, updater); |
| 80 | + component.context = context; |
| 81 | + |
| 82 | + const bbox = computeComponentBBox(this, component.render()); |
| 83 | + this.bbox.push(bbox); |
| 84 | + }); |
| 85 | + } |
| 86 | + render() { |
| 87 | + const { children } = this.props; |
| 88 | + return <group>{children}</group>; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +describe('computeComponent', () => { |
| 93 | + it('组件bbox计算', async () => { |
| 94 | + const context = createContext(); |
| 95 | + const ref = { current: null }; |
| 96 | + const guideGroupRef = { current: null }; |
| 97 | + const { props } = ( |
| 98 | + <Canvas context={context}> |
| 99 | + <GuideGroup ref={guideGroupRef}> |
| 100 | + <ViewText position={[40, 40]} ref={ref} /> |
| 101 | + <View position={[70, 70]} /> |
| 102 | + </GuideGroup> |
| 103 | + </Canvas> |
| 104 | + ); |
| 105 | + |
| 106 | + const canvas = new Canvas(props); |
| 107 | + await canvas.render(); |
| 108 | + |
| 109 | + await delay(100); |
| 110 | + |
| 111 | + const bbox = guideGroupRef.current.bbox; |
| 112 | + expect(bbox[0].x).toEqual(40); |
| 113 | + expect(bbox[0].y).toEqual(28); |
| 114 | + expect(bbox[0].width).toBeGreaterThan(0); |
| 115 | + expect(bbox[0].height).toBeGreaterThan(0); |
| 116 | + |
| 117 | + expect(bbox[1].x).toEqual(50); |
| 118 | + expect(bbox[1].y).toEqual(50); |
| 119 | + expect(bbox[1].width).toEqual(40); |
| 120 | + expect(bbox[1].height).toEqual(40); |
| 121 | + }); |
| 122 | + |
| 123 | + it('View内的group带display样式', async () => { |
| 124 | + const context = createContext(); |
| 125 | + const guideGroupRef = { current: null }; |
| 126 | + const { props } = ( |
| 127 | + <Canvas context={context}> |
| 128 | + <GuideGroup ref={guideGroupRef}> |
| 129 | + <ViewWithDisplayGroup /> |
| 130 | + </GuideGroup> |
| 131 | + </Canvas> |
| 132 | + ); |
| 133 | + |
| 134 | + const canvas = new Canvas(props); |
| 135 | + await canvas.render(); |
| 136 | + |
| 137 | + await delay(100); |
| 138 | + |
| 139 | + const bbox = guideGroupRef.current.bbox; |
| 140 | + expect(bbox[0].x).toEqual(20); |
| 141 | + expect(bbox[0].y).toEqual(0); |
| 142 | + expect(bbox[0].width).toBeGreaterThan(20); |
| 143 | + expect(bbox[0].height).toBeGreaterThan(10); |
| 144 | + }); |
| 145 | + |
| 146 | + it('View内的group带padding样式', async () => { |
| 147 | + const context = createContext(); |
| 148 | + |
| 149 | + const guideGroupRef = { current: null }; |
| 150 | + const { props } = ( |
| 151 | + <Canvas context={context}> |
| 152 | + <GuideGroup ref={guideGroupRef}> |
| 153 | + <ViewWithDisplayGroup position={[0, 0]} style={{ padding: [20, 20, 20, 20] }} /> |
| 154 | + </GuideGroup> |
| 155 | + </Canvas> |
| 156 | + ); |
| 157 | + |
| 158 | + const canvas = new Canvas(props); |
| 159 | + await canvas.render(); |
| 160 | + |
| 161 | + await delay(100); |
| 162 | + |
| 163 | + const bbox = guideGroupRef.current.bbox; |
| 164 | + |
| 165 | + expect(bbox[0].x).toEqual(0); |
| 166 | + expect(bbox[0].y).toEqual(0); |
| 167 | + expect(bbox[0].width).toBeGreaterThan(40); |
| 168 | + expect(bbox[0].height).toBeGreaterThan(40); |
| 169 | + }); |
| 170 | +}); |
0 commit comments