如何测试路由器路由?

时间:2019-09-08 15:00:40

标签: reactjs react-router

我需要测试来自App的路由,测试返回确定,但似乎有些问题。它不测试路线,我认为它仅测试是否正在获取组件。另外,我在测试NotFound页面组件时遇到问题。.如果我像上面一样放置一个测试用例,则它总是收到0。

我尝试使用InitialEntries使用MemoryRouter包装器测试路由,但是我不确定它是否可以正常工作。

//应用文件

const App: React.FC = observer(() => (
  <StoreContext.Provider value={new Store()}>
    <Router>
      <div>
        <Switch>
          <Route exact path="/" component={CampList} />
          <Route path="/camp/active" component={CampList} />
          <Route path="/camp/inactive" component={CampList} />
          <Route component={NotFound} />
        </Switch>
      </div>
    </Router>
  </StoreContext.Provider>

//测试文件

 describe('routes using memory router', () => {
   it('should show CampList component for \'/camp/active\' 
 route', () => {
     const component = mount(
       <MemoryRouter initialEntries={['/camp/active']}>
         <App />
       </MemoryRouter>,
     );
     expect(component.find(CampList)).toHaveLength(1);
   })

   it('should show CampList component for \'/camp/inactive\' 
 route', () => {
     const component = mount(
       <MemoryRouter initialEntries={['/camp/inactive']}>
         <App />
       </MemoryRouter>,
     );
     expect(component.find(CampList)).toHaveLength(1);
   })

我想测试给定的路由是否会返回预期的组件。

0 个答案:

没有答案