pshrmn / react-router-test-context Goto Github PK
View Code? Open in Web Editor NEWCreate a pseudo context to assist in testing components that render React Router's location-aware components.
License: MIT License
Create a pseudo context to assist in testing components that render React Router's location-aware components.
License: MIT License
I have the following test suite:
import React, {Component} from "react";
import createRouterContext from "react-router-test-context";
import {mount} from "enzyme";
import {PropTypes} from "prop-types";
import {Redirect, Route, Switch} from "react-router-dom";
class App extends Component {
constructor(props) {
super(props);
this.state = {permission: false};
}
render() {
return (
<Switch>
<Route exact path="/users">
<h2>A list of users</h2>
</Route>
<Route path="/users/:userId">
{({match}) => this.state.permission ? <h1>{match.params.userId}</h1> : <Redirect to="/users" />}
</Route>
</Switch>
);
}
}
describe("render()", () => {
describe("when route is /users/:userId", () => {
let app;
beforeEach(() => {
const context = createRouterContext({
location: {pathname: "/users/1"},
match: {url: "/users/1", path: "/users/:userId", params: {userId: "1"}, isExact: true},
});
const childContextTypes = {router: PropTypes.object};
app = mount(<App />, {context, childContextTypes});
});
describe("when user has permission", () => {
beforeEach(() => {
app.setState({permission: true});
});
it("should show users id", () => {
expect(app.exists("h1")).toBe(true);
});
});
describe("when user doesn't have permission", () => {
beforeEach(() => {
app.setState({permission: false});
});
it("should redirect to users list", () => {
expect(app.exists("Redirect")).toBe(true);
});
});
});
});
Both tests pass, but in the second test case I get the following warning:
Warning: You tried to redirect to the same route you're currently on: "/users"
How should I rewrite my test in order to prevent the warning from occurring?
Using options 2, however, i am getting
TypeError: router.setRouteLeaveHook is not a function
my router looks
{ history:
{ action: 'POP',
location: { pathname: '/', search: '', hash: '', key: 'ch3ac9' },
_listeners: [],
listen: [Function: listen],
push: [Function: push],
replace: [Function: replace],
_notifyListeners: [Function: _notifyListeners],
createHref: [Function: createHref] },
route:
{ match: { path: '/', url: '/', isExact: true, params: {} },
location: { pathname: '/', search: '', hash: '', key: 'ch3ac9' } },
staticContext: undefined }
which does not have setRouteLeaveHook function.
Hello!
I use typescript in my project and it would be perfect to have type definitions for your library.
http://definitelytyped.org/
Thanks.
Hi,
I was wondering if it's possible to use this module to test that a particular component is being rendered for a given URL? At the moment I'm doing a simple smoke test to check that a component renders:
import React from 'react';
import { shallow } from 'enzyme';
import createRouterContext from 'react-router-test-context';
import Layout from '../layout';
const setup = () =>
{
const context = createRouterContext();
const wrapper = shallow(<Layout />, { context });
return {
wrapper,
};
};
describe('Layout', () =>
{
test('render', () =>
{
const { wrapper } = setup();
expect(wrapper.exists()).toBe(true);
});
});
Where the component I'm testing is like:
import React from 'react';
import { Link, Switch, Route } from 'react-router-dom';
import HomeView from '../home';
import TestView from '../test';
const layout = () => (
<div>
<p>My App</p>
<Switch>
<Route path="/test" component={TestView} />
<Route path="/" component={HomeView} />
</Switch>
</div>
);
export default layout;
I'd like to extend my tests to check that individual routes are working correctly, something like:
test('"/test" route', () =>
{
const { wrapper } = setup('/test'); // Would need to set the route in the setup function
expect(wrapper.contains(TestView)).toBe(true);
});
Is that possible this this, or am I stuck with using a full mount?
Thanks :)
Hello. I'm trying to use this package to mock the router context but it still doesn't work event after context providing.
Here is my test.
Btw I also have a question. Even when I'm using the MemoryRouter and mount feature how I can test some method of my real component. enzymeWrapper.instance() doesn't work because the root component is MemoryRouter
Thanks for any help.
import * as React from "react";
// import { MemoryRouter } from "react-router-dom";
import {shallow, ShallowWrapper} from "enzyme";
import createRouterContext from 'react-router-test-context'
import Overview from "../src/modules/customer/Overview";
import { profilesStore } from "../src/store";
let overview: ShallowWrapper<undefined, undefined>;
beforeEach(function() {
overview=shallow(
<Overview profilesStore={profilesStore}/>,
{ context : createRouterContext() }
)
});
describe('Overview component test suite', () => {
it('should have an one section with id container', () => {
expect(overview.find("#container")).toHaveLength(1);
});
});
Here is the issue message
Warning: Failed context type: The context router
is marked as required in
Component
, but its value is undefined
.
in Link
in header
in section
in default_1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.