Answers for "how to test routes-react-router-dom-in-react-using-jest"

0

how to test routes-react-router-dom-in-react-using-jest

import React from 'react';
import { shallow, mount } from 'enzyme';
import Routes, { Home, News, NoMatch } from './Routes';
import { MemoryRouter
} from 'react-router'
import { Route } from 'react-router-dom';

let pathMap = {};
describe('routes using array of routers', () => {
  beforeAll(() => {
    const component = shallow(<Routes/>);
    pathMap = component.find(Route).reduce((pathMap, route) => {
        const routeProps = route.props();
        pathMap[routeProps.path] = routeProps.component;
        return pathMap;
      }, {});
      console.log(pathMap)
  })
  it('should show Home component for / router (getting array of routes)', () => {

    expect(pathMap['/']).toBe(Home);
  })
  it('should show News Feed component for /news router', () => {
    expect(pathMap['/news']).toBe(News);
  })
  it('should show News Feed component techdomain for /news router', () => {
    expect(pathMap['/news/techdomain']).toBe(News);
  })
  it('should show No match component for route not defined', ()=>{
      expect(pathMap['undefined']).toBe(NoMatch);
  })
})
Posted by: Guest on June-02-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language