Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Add tests for query params
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Engel committed Apr 12, 2019
1 parent 34a2659 commit 559c188
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
13 changes: 8 additions & 5 deletions test/src/App.js
@@ -1,5 +1,6 @@
import React from 'react';
import {useRoutes, useTitle, useRedirect, useQueryParams, useInterceptor, A, useLink} from '../../dist';
import {useRoutes, useTitle, useRedirect, useQueryParams, useInterceptor, A, setLinkProps} from '../../dist';
import QPTest from './QueryParamTest';

const products = {
"1": "Rainbow Fish",
Expand Down Expand Up @@ -74,11 +75,11 @@ const Products = () => {
? -1
: 1
)
// Note: the link uses the useLink method, but you should prefer using
// Note: the link uses the setLinkProps method, but you should prefer using
// the hookrouter 'A' component if you are not using a framework that
// requires href / onClick to be provided to it
.map(([id, title]) => (
<li key={id}><a {...useLink({ href: `/product/${id}` })}>{title}</a></li>
<li key={id}><a {...setLinkProps({ href: `/product/${id}` })}>{title}</a></li>
))}
</ul>
</React.Fragment>
Expand Down Expand Up @@ -107,7 +108,8 @@ const routes = {
'/about': () => <About/>,
'/prison': () => <LockIn/>,
'/product': () => <Products/>,
'/product/:id*': ({id}) => <Product id={id}/>
'/product/:id*': ({id}) => <Product id={id}/>,
'/qpTest': () => <QPTest/>
};

const App = () => {
Expand All @@ -121,7 +123,8 @@ const App = () => {
<A href="/">Home</A><br/>
<A href="/about">About</A><br/>
<A href="/prison">Route with Lock-In</A><br/>
<A href="/product">Products</A>
<A href="/product">Products</A><br />
<A href="/qpTest">Query Params Test</A>
</nav>
<main>
{routeResult || '404 - Not found'}
Expand Down
33 changes: 33 additions & 0 deletions test/src/QueryParamTest.js
@@ -0,0 +1,33 @@
import React from "react";
import { useQueryParams } from '../../dist';

const getRandomString = () => {
const r = Math.random() * 1235172132118424 + 12351241;
return r.toString(32);
};

const QPTest = () => {
const [params, setParams] = useQueryParams();

const setSome = () => setParams({ some: getRandomString() });

const setOthers = () => setParams({ others: getRandomString() });

const unsetSome = () => setParams({ some: undefined });

const unsetOthers = () => setParams({ others: undefined });

return (
<div>
<div>{"Current Params: " + JSON.stringify(params, null, 2)}</div>

<button onClick={setSome}>Set some</button>
<button onClick={setOthers}>Set others</button>
<br />
<button onClick={unsetSome}>Unset some</button>
<button onClick={unsetOthers}>Unset others</button>
</div>
);
};

export default QPTest;

0 comments on commit 559c188

Please sign in to comment.