diff --git a/package.json b/package.json index e242b57..848d8ab 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\" \"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": { "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/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") +})