Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ members = [
# Playwright tests
"packages/playwright-tests/liveview",
"packages/playwright-tests/web",
"packages/playwright-tests/web-routing",
"packages/playwright-tests/barebones-template",
"packages/playwright-tests/fullstack",
"packages/playwright-tests/fullstack-mounted",
Expand Down
9 changes: 9 additions & 0 deletions packages/playwright-tests/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ module.exports = defineConfig({
reuseExistingServer: !process.env.CI,
stdout: "pipe",
},
{
cwd: path.join(process.cwd(), "web-routing"),
command:
'cargo run --package dioxus-cli --release -- serve --force-sequential --platform web --addr "127.0.0.1" --port 2020',
port: 2020,
timeout: 50 * 60 * 1000,
reuseExistingServer: !process.env.CI,
stdout: "pipe",
},
{
cwd: path.join(process.cwd(), "fullstack"),
command:
Expand Down
32 changes: 32 additions & 0 deletions packages/playwright-tests/web-routing.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @ts-check
const { test, expect } = require("@playwright/test");

test("redirect", async ({ page }) => {
// Going to the root url should redirect to /other.
await page.goto("http://localhost:2020");

// Expect the page to the text Other
const main = page.locator("#other");
await expect(main).toContainText("Other");

// Expect the url to be /other
await expect(page).toHaveURL("http://localhost:2020/other");
});

test("links", async ({ page }) => {
await page.goto("http://localhost:2020/other");

// Expect clicking the link to /other/123 to navigate to /other/123
const link = page.locator("a[href='/other/123']");
await link.click();
await expect(page).toHaveURL("http://localhost:2020/other/123");
});

test("fallback", async ({ page }) => {
await page.goto("http://localhost:2020/my/404/route");

// Expect the page to contain the text Fallback
const main = page.locator("#not-found");
await expect(main).toContainText("NotFound");

});
2 changes: 2 additions & 0 deletions packages/playwright-tests/web-routing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
target
10 changes: 10 additions & 0 deletions packages/playwright-tests/web-routing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "dioxus-playwright-web-routing-test"
version = "0.0.1"
edition = "2021"
description = "Playwright test for Dioxus Web Routing"
license = "MIT OR Apache-2.0"
publish = false

[dependencies]
dioxus = { workspace = true, features = ["web", "router"]}
57 changes: 57 additions & 0 deletions packages/playwright-tests/web-routing/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use dioxus::prelude::*;

fn main() {
launch(|| {
rsx! {
Router::<Route> {}
}
})
}

#[derive(Routable, Clone, PartialEq)]
#[rustfmt::skip]
enum Route {
#[redirect("/",|| Route::Other)]
#[route("/other")]
Other,
#[route("/other/:id")]
OtherId { id: String },
#[route("/:..segments")]
NotFound { segments: Vec<String> },
}

#[component]
fn Other() -> Element {
rsx! {
div {
id: "other",
"Other"
}

Link {
id: "other-id-link",
to: Route::OtherId { id: "123".to_string() },
"go to OtherId"
}
}
}

#[component]
fn OtherId(id: String) -> Element {
rsx! {
div {
id: "other-id",
"OtherId {id}"
}
}
}

#[component]
fn NotFound(segments: Vec<String>) -> Element {
rsx! {
div {
id: "not-found",
"NotFound {segments:?}"
}
}
}
15 changes: 13 additions & 2 deletions packages/router/src/contexts/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,27 @@ impl RouterContext {
site_map: R::SITE_MAP,
};

let history = history();

// set the updater
history().updater(Arc::new(move || {
history.updater(Arc::new(move || {
for &rc in subscribers.lock().unwrap().iter() {
rc.mark_dirty();
}
}));

Self {
let myself = Self {
inner: CopyValue::new_in_scope(myself, ScopeId::ROOT),
};

// If the current route is different from the one in the browser, replace the current route
let current_route: R = myself.current();

if current_route.to_string() != history.current_route() {
myself.replace(current_route);
}

myself
}

/// Check if the router is running in a liveview context
Expand Down
Loading