如何编写条件语句的测试用例

huangapple go评论49阅读模式
英文:

How to write test case for conditional statement

问题

以下是您提供的代码的中文翻译:

我有下面的条件语句,如何为条件运算符编写测试用例?

const SomeComponent = () => {
    return (
        <>
            {(usersApiSuccess && users.id !== null) ?
                <Dashboard />
                : (users.id === null) &&
                <ErrorPage content="用户不存在" />}
        </>
    )
}
英文:

I have below conditional statement, how I can write test case for conditional operator

const SomeComponent = () =&gt; {
    return (
        &lt;&gt;
            {(usersApiSuccess &amp;&amp; users.id !== null) ?
                &lt;Dashboard /&gt;
                : (users.id === null) &amp;&amp;
                &lt;ErrorPage content=&quot;User does not exist&quot; /&gt;}
        &lt;/&gt;)
}

答案1

得分: 0

可以使用React Testing LibraryJest

像这样

import { render } from '@testing-library/react';

describe('SomeComponent', () => {
  it('如果 usersApiSuccess 为 true 并且 users.id 不为 null,则渲染 Dashboard 组件', () => {
    const { getByTestId } = render(<SomeComponent usersApiSuccess={true} users={{ id: 1 }} />);
    expect(getByTestId('dashboard')).toBeInTheDocument();
  });

  it('如果 usersApiSuccess 为 true 并且 users.id 为 null,则渲染 ErrorPage 组件', () => {
    const { getByText } = render(<SomeComponent usersApiSuccess={true} users={{ id: null }} />);
    expect(getByText('User does not exist')).toBeInTheDocument();
  });

  it('如果 usersApiSuccess 为 false,则不渲染任何组件', () => {
    const { queryByTestId, queryByText } = render(<SomeComponent usersApiSuccess={false} users={{ id: 1 }} />);
    expect(queryByTestId('dashboard')).not.toBeInTheDocument();
    expect(queryByText('User does not exist')).not.toBeInTheDocument();
  });
});

[供参考][2]
英文:

You can use React Testing Library with Jest

Like this

import { render } from &#39;@testing-library/react&#39;;

describe(&#39;SomeComponent&#39;, () =&gt; {
  it(&#39;renders the Dashboard component if usersApiSuccess is true and users.id is not null&#39;, () =&gt; {
    const { getByTestId } = render(&lt;SomeComponent usersApiSuccess={true} users={{ id: 1 }} /&gt;);
    expect(getByTestId(&#39;dashboard&#39;)).toBeInTheDocument();
  });

  it(&#39;renders the ErrorPage component if usersApiSuccess is true and users.id is null&#39;, () =&gt; {
    const { getByText } = render(&lt;SomeComponent usersApiSuccess={true} users={{ id: null }} /&gt;);
    expect(getByText(&#39;User does not exist&#39;)).toBeInTheDocument();
  });

  it(&#39;does not render any component if usersApiSuccess is false&#39;, () =&gt; {
    const { queryByTestId, queryByText } = render(&lt;SomeComponent usersApiSuccess={false} users={{ id: 1 }} /&gt;);
    expect(queryByTestId(&#39;dashboard&#39;)).not.toBeInTheDocument();
    expect(queryByText(&#39;User does not exist&#39;)).not.toBeInTheDocument();
  });
});

For Reference

huangapple
  • 本文由 发表于 2023年4月17日 19:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定