Skip to content

rename configure to create_router #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2022
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
8 changes: 4 additions & 4 deletions idom_router/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

from .router import (
Route,
RoutesConstructor,
configure,
RouterConstructor,
create_router,
link,
use_location,
use_params,
use_query,
)

__all__ = [
"configure",
"create_router",
"link",
"Route",
"RoutesConstructor",
"RouterConstructor",
"use_location",
"use_params",
"use_query",
Expand Down
10 changes: 5 additions & 5 deletions idom_router/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
from typing_extensions import Protocol # type: ignore


class RoutesConstructor(Protocol):
class RouterConstructor(Protocol):
def __call__(self, *routes: Route) -> ComponentType:
...


def configure(
def create_router(
implementation: BackendImplementation[Any] | Callable[[], Location]
) -> RoutesConstructor:
) -> RouterConstructor:
if isinstance(implementation, BackendImplementation):
use_location = implementation.use_location
elif callable(implementation):
Expand All @@ -38,7 +38,7 @@ def configure(
)

@component
def routes(*routes: Route) -> ComponentType | None:
def router(*routes: Route) -> ComponentType | None:
initial_location = use_location()
location, set_location = use_state(initial_location)
compiled_routes = use_memo(
Expand All @@ -58,7 +58,7 @@ def routes(*routes: Route) -> ComponentType | None:
)
return None

return routes
return router


@dataclass
Expand Down
36 changes: 18 additions & 18 deletions tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from idom_router import (
Route,
RoutesConstructor,
configure,
RouterConstructor,
create_router,
link,
use_location,
use_params,
Expand All @@ -14,20 +14,20 @@


@pytest.fixture
def routes(backend: BackendFixture):
return configure(backend.implementation)
def router(backend: BackendFixture):
return create_router(backend.implementation)


def test_configure(backend):
configure(backend.implementation)
configure(backend.implementation.use_location)
def test_create_router(backend):
create_router(backend.implementation)
create_router(backend.implementation.use_location)
with pytest.raises(
TypeError, match="Expected a 'BackendImplementation' or 'use_location' hook"
):
configure(None)
create_router(None)


async def test_simple_router(display: DisplayFixture, routes: RoutesConstructor):
async def test_simple_router(display: DisplayFixture, router: RouterConstructor):
def make_location_check(path, *routes):
name = path.lstrip("/").replace("/", "-")

Expand All @@ -40,7 +40,7 @@ def check_location():

@component
def sample():
return routes(
return router(
make_location_check("/a"),
make_location_check("/b"),
make_location_check("/c"),
Expand Down Expand Up @@ -68,10 +68,10 @@ def sample():
assert not await root_element.inner_html()


async def test_nested_routes(display: DisplayFixture, routes: RoutesConstructor):
async def test_nested_routes(display: DisplayFixture, router: RouterConstructor):
@component
def sample():
return routes(
return router(
Route(
"/a",
html.h1({"id": "a"}, "A"),
Expand All @@ -94,13 +94,13 @@ def sample():
await display.page.wait_for_selector(selector)


async def test_navigate_with_link(display: DisplayFixture, routes: RoutesConstructor):
async def test_navigate_with_link(display: DisplayFixture, router: RouterConstructor):
render_count = Ref(0)

@component
def sample():
render_count.current += 1
return routes(
return router(
Route("/", link({"id": "root"}, "Root", to="/a")),
Route("/a", link({"id": "a"}, "A", to="/b")),
Route("/b", link({"id": "b"}, "B", to="/c")),
Expand All @@ -121,7 +121,7 @@ def sample():
assert render_count.current == 1


async def test_use_params(display: DisplayFixture, routes: RoutesConstructor):
async def test_use_params(display: DisplayFixture, router: RouterConstructor):
expected_params = {}

@component
Expand All @@ -131,7 +131,7 @@ def check_params():

@component
def sample():
return routes(
return router(
Route(
"/first/{first:str}",
check_params(),
Expand All @@ -157,7 +157,7 @@ def sample():
await display.page.wait_for_selector("#success")


async def test_use_query(display: DisplayFixture, routes: RoutesConstructor):
async def test_use_query(display: DisplayFixture, router: RouterConstructor):
expected_query = {}

@component
Expand All @@ -167,7 +167,7 @@ def check_query():

@component
def sample():
return routes(Route("/", check_query()))
return router(Route("/", check_query()))

await display.show(sample)

Expand Down