首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jest & Enzyme测试switch语句

使用Jest & Enzyme测试switch语句
EN

Stack Overflow用户
提问于 2019-06-05 09:54:16
回答 1查看 3.3K关注 0票数 1

我正在使用switch语句来有条件地呈现组件,我已经花了相当多的时间来尝试测试,但我就是不知道如何去做,也没有在网上找到任何对此有帮助的资源。感谢您的关注!

代码语言:javascript
复制
 componentToRender = (currentPath) => {
    const { years, tours, songs, shows, venues } = this.props;

    switch (currentPath) {
      case "/Years":
        return years.map(year => <Years key={year.date} year={year} />);
      case "/Tours":
        return tours.map(tour => <Tours key={tour.id} tour={tour} />);
      case "/Songs":
        return songs.map(song => <Songs key={song.id} song={song} />);
      case "/Venues":
        return venues.map(venue => <Venues key={venue.id} venue={venue} />);
      case "/Shows":
        return shows.map(show => <Shows key={show.id} show={show} />);
      case "/SetList":
        return <SetLists />;
      case "/UserStats":
        return <UserStats />;
      default:
        return <HomePage />;
    }
  };
EN

回答 1

Stack Overflow用户

发布于 2019-09-26 13:20:43

这里有一个解决方案:

index.tsx

代码语言:javascript
复制
import React, { Component } from 'react';

const Years = ({ key, year }) => (
  <div>
    {key}, {year}
  </div>
);

const Tours = ({ key, tour }) => (
  <div>
    {key}, {tour}
  </div>
);

const Songs = ({ key, song }) => (
  <div>
    {key}, {song}
  </div>
);

const Venues = ({ key, venue }) => (
  <div>
    {key}, {venue}
  </div>
);

const Shows = ({ key, show }) => (
  <div>
    {key}, {show}
  </div>
);

const SetLists = () => <div>SetLists</div>;
const UserStats = () => <div>UserStats</div>;
const HomePage = () => <div>HomePage</div>;

export interface IXComponentProps {
  years: any[];
  tours: any[];
  songs: any[];
  shows: any[];
  venues: any[];
  currentPath: string;
}

export class XComponent extends Component<IXComponentProps> {
  constructor(props) {
    super(props);
  }

  public componentToRender = currentPath => {
    const { years, tours, songs, shows, venues } = this.props;

    switch (currentPath) {
      case '/Years':
        return years.map(year => <Years key={year.date} year={year} />);
      case '/Tours':
        return tours.map(tour => <Tours key={tour.id} tour={tour} />);
      case '/Songs':
        return songs.map(song => <Songs key={song.id} song={song} />);
      case '/Venues':
        return venues.map(venue => <Venues key={venue.id} venue={venue} />);
      case '/Shows':
        return shows.map(show => <Shows key={show.id} show={show} />);
      case '/SetList':
        return <SetLists />;
      case '/UserStats':
        return <UserStats />;
      default:
        return <HomePage />;
    }
  }

  public render() {
    const { currentPath } = this.props;
    return this.componentToRender(currentPath);
  }
}

index.spec.tsx

代码语言:javascript
复制
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import { XComponent, IXComponentProps } from './';

describe('XComponent', () => {
  let wrapper: ShallowWrapper;
  const mockedProps: IXComponentProps = {
    years: [{ date: '2019-01-01' }],
    tours: [{ id: '1' }],
    songs: [{ id: '2' }],
    shows: [{ id: '3' }],
    venues: [{ id: '4' }],
    currentPath: ''
  };
  beforeEach(() => {
    wrapper = shallow(<XComponent {...mockedProps}></XComponent>);
  });
  it.each`
    currentPath     | componentToRender
    ${'/'}          | ${'HomePage'}
    ${'/UserStats'} | ${'UserStats'}
    ${'/SetList'}   | ${'SetLists'}
    ${'/Shows'}     | ${'Shows'}
    ${'/Venues'}    | ${'Venues'}
    ${'/Songs'}     | ${'Songs'}
    ${'/Tours'}     | ${'Tours'}
    ${'/Years'}     | ${'Years'}
  `(
    'should render $componentToRender component by current path $currentPath correctly',
    ({ currentPath, componentToRender }) => {
      wrapper.setProps({ currentPath });
      expect(wrapper.find(componentToRender)).toHaveLength(1);
    }
  );
});

单元测试结果和覆盖率报告:

代码语言:javascript
复制
PASS  src/stackoverflow/56453372/index.spec.tsx (7.661s)
  XComponent
    ✓ should render HomePage component by current path / correctly (19ms)
    ✓ should render UserStats component by current path /UserStats correctly (1ms)
    ✓ should render SetLists component by current path /SetList correctly (2ms)
    ✓ should render Shows component by current path /Shows correctly (1ms)
    ✓ should render Venues component by current path /Venues correctly (1ms)
    ✓ should render Songs component by current path /Songs correctly (2ms)
    ✓ should render Tours component by current path /Tours correctly (1ms)
    ✓ should render Years component by current path /Years correctly (1ms)

-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |    67.86 |      100 |    52.94 |      100 |                   |
 index.tsx |    67.86 |      100 |    52.94 |      100 |                   |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       8 passed, 8 total
Snapshots:   0 total
Time:        9.53s

HTML覆盖率报告:

下面是完成的演示:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/56453372

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56453372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档