试图弄清楚如何正确测试可分解的< circle>
svg元素。
我是React测试库的新手,因此我不确定RTL是否可以使用(使用Mocha测试框架),或者是否应该使用柏树。
假设我有一个圆元素:
< circle data-testid ='circle-element'cx =“ 100” cy =“ 100” r =“ 50”/>
注意:0,0,0原点在左上方。
圆的中心位于x = 100,y = 100,半径为50
。 =“ https://i.sstatic.net/mpsab.png” alt =“在此处输入图像说明”>
位于circle的边缘x = 150,y = 100是 draghandle
用户可以单击并拖动以调整圆的大小。
<DragHandle
data-testid='circle-draghandle'
x={handleX}
y={handleY}
dragMove={onHandleDrag}
/>
如果用户单击 draghandle
在其原始位置的x = 150,y = 100并将拖动到x = 200,y = 100,我们希望圆半径现在为100。
请注意:注意:圆的中心没有变化;仍处于x = 100,y = 100。
我该如何测试?
我确实弄清楚了如何使用用给定的坐标和半径进行 React Testing Library </
it('should render a Circle with the coordinates provided', function () {
render(<Circle mark={{ cx: 100, cy: 100, r: 50}} />)
expect(screen.getByTestId('circle-element'))
.to.have.attr('r')
.to.equal('50')
})
code :&lt; circle&gt;
是我们的组件,其中实际&lt; circle&gt; svg元素的生活。
在测试调整大小的部分方面有任何帮助!
谢谢。
Trying to figure out how to properly test a resizable <circle>
svg element.
I am new to React Testing Library, so I'm unsure if this is possible with RTL (with the Mocha test framework) or if I should use Cypress.
Let's say I have a circle element:
<circle data-testid='circle-element' cx="100" cy="100" r="50"/>
NOTE: 0,0 origin is at the top left.
The center of the circle is located at x=100, y=100 with a radius of 50.
Located on the edge of the circle at x=150, y=100 is a Draghandle
where a user can click and drag to resize the circle.
<DragHandle
data-testid='circle-draghandle'
x={handleX}
y={handleY}
dragMove={onHandleDrag}
/>
If a user clicks on the Draghandle
at its original location of x=150, y=100 and drags to x=200, y=100, we expect the circle radius to now be 100.
NOTE: the center of the circle is unchanged; still at x=100, y=100.
How can I test this?
I did figure out how to test if the circle rendered properly using React Testing Library
with given coordinates and radius:
it('should render a Circle with the coordinates provided', function () {
render(<Circle mark={{ cx: 100, cy: 100, r: 50}} />)
expect(screen.getByTestId('circle-element'))
.to.have.attr('r')
.to.equal('50')
})
NOTE: <Circle>
is our component where the actual <circle>
svg element lives.
Any help with testing the resizing part what be amazing!
Thank you.
发布评论
评论(3)
好的 - 终于使用React Testing库和
用户events@14
有一个解决方案!希望这会帮助别人。
用户事件
必须为14.0或更高。导入测试文件中的USEREREFENT:
测试:
OK - finally have a solution using React Testing Library and
user-events@14
!Hopefully this will help someone else.
user-events
must be 14.0 or higher.import userEvent in testing file:
The test:
拖动测试可能很难实现,如果使用 Cypress,我建议添加 cypress-real-events 图书馆。
结果可能很接近但不准确(比如 49),在这种情况下测试日志会告诉您。
如果是这样,您可以使用
closeTo
断言注意
以上仅在您的图形为
时才有效,但不能用于
。
Tests for dragging can be difficult to implement, if using Cypress I recommend adding cypress-real-events library.
The result may be close but not exact (say 49), in which case the test log will tell you.
If so you can use a
closeTo
assertionNote
The above would only work if your graphic is
<svg>
, but it can't be used for<canvas>
.尝试下面的无库解决方案进行拖放:
然后您可以使用 Fody 建议的 closeTo 解决方案来检查结果
Try below library-free solution to drag and drop:
Then you could use Fody's suggested closeTo solution to check the result