Skip to content

Commit

Permalink
Implement a test for custom matchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Apr 16, 2019
1 parent bdad44d commit f063973
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const RouterCtx = createContext();
export const buildRouter = (options = {}) => {
return {
history: options.history || makeHistory(),
matchFn: options.matchFn || makeMatcher()
matcher: options.matcher || makeMatcher()
};
};

Expand Down Expand Up @@ -72,7 +72,7 @@ export const useRoute = pattern => {
const router = useRouter();
const [path] = useLocation();

return router.matchFn(pattern, path);
return router.matcher(pattern, path);
};

export const Route = props => {
Expand Down
41 changes: 41 additions & 0 deletions test/custom-matcher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from "react";
import TestRenderer from "react-test-renderer";

import { Router, Route } from "../index.js";
import memoryHistory from "./memory-history";

const customMatcher = (pattern, path) => {
const reversed = path
.replace(/^\//, "")
.split("")
.reverse()
.join("");

return [pattern.replace(/^\//, "") === reversed, {}];
};

const routeMatches = (pattern, path) => {
const history = memoryHistory(path);

const instance = TestRenderer.create(
<Router history={history} matcher={customMatcher}>
<Route path={pattern}>
<h1>it worked!</h1>
</Route>
</Router>
).root;

let phrase = null;

try {
phrase = instance.findByType("h1").props.children;
} catch (e) {}

return phrase === "it worked!";
};

it("accepts plain children", () => {
expect(routeMatches("/foo", "/oof")).toBe(true);
expect(routeMatches("/xxx", "/xxx")).toBe(true);
expect(routeMatches("/path", "/path")).toBe(false);
});

0 comments on commit f063973

Please sign in to comment.