From 1004077766e3e7f552b89be317f4d8cb24cc440f Mon Sep 17 00:00:00 2001 From: Leah Date: Mon, 15 Jan 2018 16:54:28 +0100 Subject: [PATCH 1/2] Add key to the element wrapped in the Route --- package.json | 5 ++++- src/Route.js | 17 ++++++++++------- test/index.test.js | 10 ++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index e242b57..58f5c2b 100644 --- a/package.json +++ b/package.json @@ -24,12 +24,15 @@ "bundle": "rollup -i src/index.js -o dist/router.js -f umd -mn router -g hyperapp:hyperapp", "minify": "uglifyjs dist/router.js -o dist/router.js -mc pure_funcs=Object.defineProperty --source-map includeSources,url=router.js.map", "prepublish": "npm run build", - "format": "prettier --semi false --write 'src/**/*.js'", + "format": "prettier --semi false --write \"src/**/*.js\"", "release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, "babel": { "presets": "env" }, + "jest": { + "testURL": "http://test/test" + }, "devDependencies": { "babel-preset-env": "^1.6.1", "hyperapp": "0.18.0", diff --git a/src/Route.js b/src/Route.js index 5ca625a..0e5dadb 100644 --- a/src/Route.js +++ b/src/Route.js @@ -6,11 +6,14 @@ export function Route(props) { exact: !props.parent }) - return ( - match && - props.render({ - match: match, - location: location - }) - ) + if (!match) return + + var child = props.render({ + match: match, + location: location + }) + + child.props.key = child.props.key || props.path + + return child } diff --git a/test/index.test.js b/test/index.test.js index 1f97da1..03ae534 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -33,3 +33,13 @@ test("router", done => { const unsubscribe = location.subscribe(main.location) }) + +test("route autokey", () => { + const route = h(Route, {path: "/test", render: () => h("div", {}, "test")}) + expect(route.props.key).toBe("/test") +}) + +test("route don't replace keys", () => { + const route = h(Route, {path: "/test", render: () => h("div", { key: "wow" }, "test")}) + expect(route.props.key).toBe("wow") +}) From 4ea78210633f27257a13d424fd30fa5c74d40890 Mon Sep 17 00:00:00 2001 From: Leah Date: Mon, 15 Jan 2018 17:11:13 +0100 Subject: [PATCH 2/2] Move tests to a separate file --- package.json | 2 +- test/index.test.js | 10 ---------- test/route.test.js | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 test/route.test.js diff --git a/package.json b/package.json index 58f5c2b..848d8ab 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "bundle": "rollup -i src/index.js -o dist/router.js -f umd -mn router -g hyperapp:hyperapp", "minify": "uglifyjs dist/router.js -o dist/router.js -mc pure_funcs=Object.defineProperty --source-map includeSources,url=router.js.map", "prepublish": "npm run build", - "format": "prettier --semi false --write \"src/**/*.js\"", + "format": "prettier --semi false --write \"src/**/*.js\" \"test/**/*.js\"", "release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, "babel": { diff --git a/test/index.test.js b/test/index.test.js index 03ae534..1f97da1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -33,13 +33,3 @@ test("router", done => { const unsubscribe = location.subscribe(main.location) }) - -test("route autokey", () => { - const route = h(Route, {path: "/test", render: () => h("div", {}, "test")}) - expect(route.props.key).toBe("/test") -}) - -test("route don't replace keys", () => { - const route = h(Route, {path: "/test", render: () => h("div", { key: "wow" }, "test")}) - expect(route.props.key).toBe("wow") -}) diff --git a/test/route.test.js b/test/route.test.js new file mode 100644 index 0000000..ae73520 --- /dev/null +++ b/test/route.test.js @@ -0,0 +1,15 @@ +import { h } from "hyperapp" +import { Route } from "../src" + +test("autokey", () => { + const route = h(Route, { path: "/test", render: () => h("div", {}, "test") }) + expect(route.props.key).toBe("/test") +}) + +test("don't replace keys", () => { + const route = h(Route, { + path: "/test", + render: () => h("div", { key: "wow" }, "test") + }) + expect(route.props.key).toBe("wow") +})