Skip to content

Commit

Permalink
docs: add tests as docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlin1228 committed Oct 14, 2024
1 parent f654625 commit 6d93148
Showing 1 changed file with 150 additions and 0 deletions.
150 changes: 150 additions & 0 deletions crates/dt_route/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,154 @@ mod tests {
],
)
}

#[test]
fn unsupported_template_literal_path() {
let module_ast = Input::Code(
r#"
const A = "A";
const B = "B";
const C = "C";
export default {
'foo': {
path: `${prefix}/route/path`,
layouts: [A, B],
page: C
},
};
"#,
)
.get_module_ast()
.unwrap();
let symbol_dependency = collect_symbol_dependency(&module_ast, MOCK_MODULE_PATH).unwrap();
let mut symbol_to_routes = SymbolToRoutes::new();
symbol_to_routes
.collect_route_dependency(&module_ast, &symbol_dependency)
.unwrap();

assert!(symbol_to_routes
.table
.get(MOCK_MODULE_PATH)
.unwrap()
.is_empty());
}

#[test]
fn unsupported_react_router() {
let module_ast = Input::Code(
// Copied from https://github.com/remix-run/react-router/blob/dev/examples/basic/src/App.tsx
r#"
import { Routes, Route, Outlet, Link } from "react-router-dom";
export default function App() {
return (
<div>
<h1>Basic Example</h1>
<p>
This example demonstrates some of the core features of React Router
including nested <code>&lt;Route&gt;</code>s,{" "}
<code>&lt;Outlet&gt;</code>s, <code>&lt;Link&gt;</code>s, and using a
"*" route (aka "splat route") to render a "not found" page when someone
visits an unrecognized URL.
</p>
{/* Routes nest inside one another. Nested route paths build upon
parent route paths, and nested route elements render inside
parent route elements. See the note about <Outlet> below. */}
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
<Route path="dashboard" element={<Dashboard />} />
{/* Using path="*"" means "match anything", so this route
acts like a catch-all for URLs that we don't have explicit
routes for. */}
<Route path="*" element={<NoMatch />} />
</Route>
</Routes>
</div>
);
}
function Layout() {
return (
<div>
{/* A "layout route" is a good place to put markup you want to
share across all the pages on your site, like navigation. */}
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/dashboard">Dashboard</Link>
</li>
<li>
<Link to="/nothing-here">Nothing Here</Link>
</li>
</ul>
</nav>
<hr />
{/* An <Outlet> renders whatever child route is currently active,
so you can think about this <Outlet> as a placeholder for
the child routes we defined above. */}
<Outlet />
</div>
);
}
function Home() {
return (
<div>
<h2>Home</h2>
</div>
);
}
function About() {
return (
<div>
<h2>About</h2>
</div>
);
}
function Dashboard() {
return (
<div>
<h2>Dashboard</h2>
</div>
);
}
function NoMatch() {
return (
<div>
<h2>Nothing to see here!</h2>
<p>
<Link to="/">Go to the home page</Link>
</p>
</div>
);
}
"#,
)
.get_module_ast()
.unwrap();
let symbol_dependency = collect_symbol_dependency(&module_ast, MOCK_MODULE_PATH).unwrap();
let mut symbol_to_routes = SymbolToRoutes::new();
symbol_to_routes
.collect_route_dependency(&module_ast, &symbol_dependency)
.unwrap();

assert!(symbol_to_routes.table.get(MOCK_MODULE_PATH).is_none());
}
}

0 comments on commit 6d93148

Please sign in to comment.