【问题标题】:Test for a component: How to access to a property of a tag with getByTestId测试组件:如何使用 getByTestId 访问标签的属性
【发布时间】:2021-07-15 06:23:38
【问题描述】:

我正在 react 中进行组件测试(我的第一个),我想验证一个数字,当我将值传递给它时,它返回 undefined 并且我删除了该值以查看它返回的内容,它很好,找到元素

import React, { useState } from 'react';
import { render, cleanup, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { NumberUpDown } from '../../components/number- 
updown/NumberUpDown';
import '@testing-library/jest-dom/extend-expect'

const UpDownNumber = () => {
 const [quantity, setQuantity] = useState<number>(1);
 const packageType = 'box'
 return (
 <NumberUpDown
  value={quantity}
  valueToShow={
    packageType === 'box' || 'pack' || 'piece' || 'bag' || 'sbox' 
  ? quantity : quantity * 12
  }
  min={1}
  max={5000}
  step={1}
  onChange={value => setQuantity(value)}
/>
);
 };

describe('Plus or minus in the product modal', () => {
afterEach(cleanup);

beforeEach(() => render(<UpDownNumber />));

it('Validate is if exists', () => {
    expect(screen.getByTestId('product-minus')).toBeInTheDocument();
    expect(screen.getByTestId('product-input')).toBeInTheDocument();
    expect(screen.getByTestId('product-plus')).toBeInTheDocument();
});

it('Validate function onclick', () => {
    const minusButton = screen.getByTestId('product-minus');
    const plusButton = screen.getByTestId('product-plus');
    const input = screen.getByTestId('product-input');
    userEvent.click(plusButton);
    userEvent.click(plusButton);
    expect(getByRole('textbox', { name: /email/i })).toHaveValue('test@email.com);
    expect((input as HTMLInputElement).value).toBe(3);
    userEvent.click(minusButton);
    expect((input as HTMLInputElement)).toBe(2);
});

});

 Expected: 3
 Received: <ion-input class="value-cell" data-testid="product-input" type="number" 
 value="3" />

 expect((input as HTMLInputElement).value).toBe(3);

 Expected: 3
 Received: undefined

当我访问标签时,我需要它,当它找到它时,获取值......

【问题讨论】:

    标签: reactjs jestjs ts-jest babel-jest


    【解决方案1】:

    您已经使用了@testing-library,所以我建议更进一步,将https://www.npmjs.com/package/@testing-library/jest-dom 添加为devDependency。如果使用基于 Create React App 的应用程序,您可以向 setupTests.js 文件添加类似导入,例如

    import '@testing-library/jest-dom/extend-expect';
    

    然后您可以编写测试来检查字段的值,使用如下:

    expect(getByRole('textbox', { name: /email/i })).toHaveValue('test@email.com);
    

    使用jest-dom 可以让您编写读起来更好的测试,但这只是我的看法。

    【讨论】:

    • 我刚刚做了一个更新,包含了测试组件的所有代码
    • 抱歉,我没有很好地解释自己。看起来您只是复制/粘贴了我的测试行,但您需要根据您的目的对其进行修改,所以可能类似于 expect(screen.getByTestId('product-input')).toHaveValue(3);。我假设单击加号按钮会增加输入元素的值。如果可能的话,最好使用getByRole 而不是getByTestId@testing-library 在显示找到的角色方面做得非常好,如果它可以找到指定的角色的话。
    • expect(element).toHaveValue(3) 期望元素具有值:3 接收:未定义
    • 同样的问题 :/ :(
    • 有你的代码我无权访问,如果可能的话,你可以用你的所有代码创建一个sandbox,这样它就可以运行并在这里发回URL。可能有助于更轻松地找出您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2020-08-14
    • 2021-05-14
    相关资源
    最近更新 更多