如何测试可调整大小的圆形 SVG 元素?

发布于 2025-01-20 20:01:08 字数 1392 浏览 0 评论 0 原文

试图弄清楚如何正确测试可分解的< 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.

enter image description here

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.

enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好的 - 终于使用React Testing库和用户events@14 有一个解决方案!

希望这会帮助别人。

用户事件必须为14.0或更高。

导入测试文件中的USEREREFENT:

import userEvent from '@testing-library/user-event'

测试:

describe('Circle tool', function () {
  let mark
  beforeEach(function () {
    mark = CircleMark.create({
      id: 'circle1',
      toolType: 'circle',
      x_center: 200,
      y_center: 200,
      r: 50
    })
  })

  it('should change the radius when drag handle is moved', async () => {
    const user = userEvent.setup()

    render(<Circle mark={mark} />)

    expect(mark.x_center).to.equal(200)
    expect(mark.y_center).to.equal(200)
    expect(mark.r).to.equal(50)

    // click on dragHandle
    // move dragHandle
    // release mouse button
    const circleDragHandle = screen.getByTestId('draghandle')
    await user.pointer([
      { keys: '[MouseLeft>]', target: circleDragHandle },
      { coords: { x: 300, y: 200 } },
      { keys: '[/MouseLeft]' }
    ])

    expect(mark.x_center).to.equal(200)
    expect(mark.y_center).to.equal(200)
    expect(mark.r).to.equal(100)
  })
})

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:

import userEvent from '@testing-library/user-event'

The test:

describe('Circle tool', function () {
  let mark
  beforeEach(function () {
    mark = CircleMark.create({
      id: 'circle1',
      toolType: 'circle',
      x_center: 200,
      y_center: 200,
      r: 50
    })
  })

  it('should change the radius when drag handle is moved', async () => {
    const user = userEvent.setup()

    render(<Circle mark={mark} />)

    expect(mark.x_center).to.equal(200)
    expect(mark.y_center).to.equal(200)
    expect(mark.r).to.equal(50)

    // click on dragHandle
    // move dragHandle
    // release mouse button
    const circleDragHandle = screen.getByTestId('draghandle')
    await user.pointer([
      { keys: '[MouseLeft>]', target: circleDragHandle },
      { coords: { x: 300, y: 200 } },
      { keys: '[/MouseLeft]' }
    ])

    expect(mark.x_center).to.equal(200)
    expect(mark.y_center).to.equal(200)
    expect(mark.r).to.equal(100)
  })
})
征棹 2025-01-27 20:01:08

拖动测试可能很难实现,如果使用 Cypress,我建议添加 cypress-real-events 图书馆。

cy.get('[data-testid="circle-draghandle"]')
  .realClick()
  .realMouseMove(200, 100)  // drag 100 right
  .realMouseUp()

cy.getByTestId('circle-element')   // from cypress-testing-library add-on
  .should('have.attr', 'r', '50')

结果可能很接近但不准确(比如 49),在这种情况下测试日志会告诉您。

如果是这样,您可以使用 closeTo 断言

cy.getByTestId('circle-element')
  .should('have.attr', 'r')
  .and(radius => {
    expect(radius).to.be.closeTo(50, 1)
  })

注意

以上仅在您的图形为 时才有效,但不能用于

Tests for dragging can be difficult to implement, if using Cypress I recommend adding cypress-real-events library.

cy.get('[data-testid="circle-draghandle"]')
  .realClick()
  .realMouseMove(200, 100)  // drag 100 right
  .realMouseUp()

cy.getByTestId('circle-element')   // from cypress-testing-library add-on
  .should('have.attr', 'r', '50')

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 assertion

cy.getByTestId('circle-element')
  .should('have.attr', 'r')
  .and(radius => {
    expect(radius).to.be.closeTo(50, 1)
  })

Note

The above would only work if your graphic is <svg>, but it can't be used for <canvas>.

才能让你更想念 2025-01-27 20:01:08

尝试下面的无库解决方案进行拖放:

cy.get('[data-testid="circle-draghandle"]')
.trigger('mousedown', { //simulating hold click
    button: 0
}).wait(700)
.trigger('mousemove', { //simulating drag
    pageX: 200, 
    pageY: 100,
    force: true
}).wait(250)
.trigger('mouseup', {  //simulating drop
    force: true
});

然后您可以使用 Fody 建议的 closeTo 解决方案来检查结果

Try below library-free solution to drag and drop:

cy.get('[data-testid="circle-draghandle"]')
.trigger('mousedown', { //simulating hold click
    button: 0
}).wait(700)
.trigger('mousemove', { //simulating drag
    pageX: 200, 
    pageY: 100,
    force: true
}).wait(250)
.trigger('mouseup', {  //simulating drop
    force: true
});

Then you could use Fody's suggested closeTo solution to check the result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文