From f063973a3b7a960c2e100dac4f75066bdf7e8d55 Mon Sep 17 00:00:00 2001 From: Alexey Taktarov Date: Tue, 16 Apr 2019 10:44:55 +0300 Subject: [PATCH] Implement a test for custom matchers. --- index.js | 4 ++-- test/custom-matcher.test.js | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/custom-matcher.test.js diff --git a/index.js b/index.js index daa0e1a1..8ac20804 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,7 @@ const RouterCtx = createContext(); export const buildRouter = (options = {}) => { return { history: options.history || makeHistory(), - matchFn: options.matchFn || makeMatcher() + matcher: options.matcher || makeMatcher() }; }; @@ -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 => { diff --git a/test/custom-matcher.test.js b/test/custom-matcher.test.js new file mode 100644 index 00000000..a70a2d45 --- /dev/null +++ b/test/custom-matcher.test.js @@ -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( + + +

it worked!

+
+
+ ).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); +});