Skip to content

Commit 425d54f

Browse files
author
Alex Lohr
committed
fix router setup
1 parent 349c957 commit 425d54f

4 files changed

Lines changed: 45 additions & 26 deletions

File tree

package-lock.json

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solidjs/testing-library",
3-
"version": "0.8.0",
3+
"version": "0.8.1",
44
"description": "Simple and complete Solid testing utilities that encourage good testing practices.",
55
"type": "module",
66
"main": "./dist/index.cjs",
@@ -57,10 +57,10 @@
5757
"report:coverage": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
5858
},
5959
"dependencies": {
60-
"@solidjs/router": "^0.8.2",
6160
"@testing-library/dom": "^9.3.0"
6261
},
6362
"devDependencies": {
63+
"@solidjs/router": "^0.8.2",
6464
"@testing-library/jest-dom": "^5.16.5",
6565
"@testing-library/user-event": "^14.4.3",
6666
"@vitest/coverage-c8": "^0.32.2",

src/__tests__/routes.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ describe("location option", () => {
1010
</Routes>
1111
);
1212

13-
it("can render the main route", () => {
14-
const { getByText } = render(() => <App />, { location: "/" });
15-
expect(getByText("Start")).not.toBeFalsy();
13+
it("can render the main route", async () => {
14+
const { findByText } = render(() => <App />, { location: "/" });
15+
expect(await findByText("Start")).not.toBeFalsy();
1616
});
1717

1818
it("can render a route with the id", async () => {

src/index.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import { getQueriesForElement, prettyDOM } from "@testing-library/dom";
22
import {
33
Accessor,
4-
children,
54
createComponent,
65
createRoot,
76
createSignal,
87
getOwner,
8+
lazy,
99
onError,
1010
onMount,
1111
Owner,
12-
runWithOwner,
12+
runWithOwner
1313
} from "solid-js";
1414
import { hydrate as solidHydrate, render as solidRender } from "solid-js/web";
1515

16-
import { Router, Navigate, memoryIntegration, useParams } from "@solidjs/router";
17-
1816
import type {
1917
Ui,
2018
Result,
@@ -86,19 +84,36 @@ function render(ui: Ui, options: Options = {}): Result {
8684
})
8785
: ui;
8886

89-
const routedUi: Ui = () => {
90-
return !location ? wrappedUi() : createComponent(Router, {
91-
get children() {
92-
return [
93-
createComponent(Navigate, { href: location || "" }),
94-
createComponent(wrappedUi, {})
95-
]
96-
},
97-
get source() {
98-
return memoryIntegration();
99-
}
100-
});
101-
};
87+
const routedUi: Ui =
88+
typeof location === "string"
89+
? lazy(async () => {
90+
try {
91+
const { memoryIntegration, useNavigate, Router } = await import("@solidjs/router");
92+
return {
93+
default: () =>
94+
createComponent(Router, {
95+
get children() {
96+
return [
97+
createComponent(
98+
() => (useNavigate()(location || "", { replace: true, scroll: false }), null),
99+
{}
100+
),
101+
createComponent(wrappedUi, {})
102+
];
103+
},
104+
get source() {
105+
return memoryIntegration();
106+
}
107+
})
108+
};
109+
} catch (e) {
110+
console.error(
111+
"It appears you want to use the location option without having @solidjs/router installed."
112+
);
113+
return { default: () => createComponent(wrappedUi, {}) };
114+
}
115+
})
116+
: wrappedUi;
102117

103118
const dispose = hydrate
104119
? (solidHydrate(routedUi, container) as unknown as () => void)

0 commit comments

Comments
 (0)