Skip to content
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

Expand tests coverage #30

Merged
merged 1 commit into from
Jul 28, 2024
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
12 changes: 6 additions & 6 deletions examples/tutorial/06_components/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
# Using the @t.component decorator registers the component for use elsewhere
@t.component()
class Card(Component):
props = ["type"]
props = ["type", "button_text"]
default_classes = ["card"]

def populate(self):
with t.h2(classes=[self.type]):
self.insert_slot("card-header")
with t.p():
self.insert_slot() # If you don't pass a name, it defaults to the main slot
t.button("Understood", on_click=self.on_button_click)
t.button(self.button_text, on_click=self.on_button_click)

def on_button_click(self, event):
self.trigger_event("my-custom-event", detail={"type": self.type})
Expand All @@ -28,26 +28,26 @@ def initial(self):
def populate(self):
t.h1("Components are useful")

with t.card(type="success", on_my_custom_event=self.handle_custom_event) as card:
with t.card(type="success", button_text="Okay Then", on_my_custom_event=self.handle_custom_event) as card:
with card.slot("card-header"):
t("Success!")
with card.slot():
t("Your operation worked")

with t.card(type="warning", on_my_custom_event=self.handle_custom_event) as card:
with t.card(type="warning", button_text="Got It", on_my_custom_event=self.handle_custom_event) as card:
with card.slot("card-header"):
t("Warning!")
with card.slot():
t("Your operation may not work")

with t.card(type="error", on_my_custom_event=self.handle_custom_event) as card:
with t.card(type="error", button_text="Understood", on_my_custom_event=self.handle_custom_event) as card:
with card.slot("card-header"):
t("Failure!")
with card.slot():
t("Your operation failed")

if self.state["message"]:
t.p(self.state["message"])
t.p(self.state["message"], id="result")

def handle_custom_event(self, event):
self.state["message"] = f"Custom event from card with type {event.detail.get('type')}"
Expand Down
96 changes: 92 additions & 4 deletions tests/integration/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_hello_world(http_server, page: Page):
expect(page.locator("h1")).to_contain_text("Hello, World!")


def test_hello_name(page: Page) -> None:
def test_hello_name(page: Page):
page.goto(f"http://localhost:{PORT}/")
page.get_by_role("link", name="Example 2: Hello, Name").click()
page.get_by_placeholder("name").click()
Expand All @@ -65,7 +65,7 @@ def test_hello_name(page: Page) -> None:
expect(page.locator("h1")).to_contain_text("Hello, Jack!")


def test_counter(page: Page) -> None:
def test_counter(page: Page):
page.goto(f"http://localhost:{PORT}/")
page.get_by_role("link", name="Example 3: Counter").click()
page.get_by_role("button", name="+").click()
Expand All @@ -80,7 +80,7 @@ def test_counter(page: Page) -> None:
expect(page.locator(".count")).to_contain_text("-1")


def test_refs_problem(page: Page) -> None:
def test_refs_problem(page: Page):
def input_is_active():
return page.evaluate("document.querySelector(\"[placeholder='Type a word']\") === document.activeElement")

Expand All @@ -98,7 +98,7 @@ def input_is_active():
assert not input_is_active()


def test_refs_solution(page: Page) -> None:
def test_refs_solution(page: Page):
def input_is_active():
return page.evaluate("document.querySelector(\"[placeholder='Type a word']\") === document.activeElement")

Expand All @@ -109,3 +109,91 @@ def input_is_active():
assert input_is_active()
page.get_by_placeholder("Type a word").fill("foobar")
assert input_is_active()


def test_watchers(page: Page):
page.goto(f"http://localhost:{PORT}/")
page.get_by_role("link", name="Example 5: Watchers").click()
page.get_by_placeholder("Enter a guess").click()
page.get_by_placeholder("Enter a guess").fill("3")
page.get_by_text("Keep trying...").click()
page.get_by_placeholder("Enter a guess").click()
page.get_by_placeholder("Enter a guess").fill("4")
page.get_by_text("You guessed the number!").click()


def test_components(page: Page):
page.goto(f"http://localhost:{PORT}/")
page.get_by_role("link", name="Example 6: Components").click()

page.get_by_role("button", name="Okay Then").click()
expect(page.locator("#result")).to_contain_text("Custom event from card with type success")
page.get_by_role("button", name="Got It").click()
expect(page.locator("#result")).to_contain_text("Custom event from card with type warning")
page.get_by_role("button", name="Understood").click()
expect(page.locator("#result")).to_contain_text("Custom event from card with type error")


def test_routing(page: Page):
page.goto(f"http://localhost:{PORT}/")
page.get_by_role("link", name="Example 7: Routing").click()

page.get_by_role("heading", name="PuePy Routing Demo: Pet").click()
page.get_by_role("link", name="Scooby-Doo").click()
page.get_by_text("Scooby-Doo").click()
page.get_by_role("link", name="Back to Homepage").click()
page.get_by_role("link", name="Garfield").click()
page.get_by_text("Garfield").click()
page.get_by_role("link", name="Back to Homepage").click()
page.get_by_role("link", name="Snoopy").click()
page.get_by_text("Snoopy").click()
page.get_by_role("link", name="Back to Homepage").click()
page.get_by_role("heading", name="PuePy Routing Demo: Pet").click()


def test_pypi_libraries(page: Page):
page.goto(f"http://localhost:{PORT}/")
page.get_by_role("link", name="Example 8: PyPi Libraries").click()
page.get_by_role("textbox").first.click()
page.get_by_role("textbox").first.fill("<html>\n<body>\n<h1>Hello, World!</h1>\n</body>\n</html>\n")
page.get_by_role("button", name="Convert").click()
expect(page.locator("#pp-b")).to_have_value(
"from puepy import Application, Page, t\n\napp = Application()\n\n@app.page()\nclass DefaultPage(Page):\n "
"def populate(self):\n with t.html():\n with t.body():\n with t.h1():\n "
" t('Hello, World!')"
)
page.get_by_text("Generate full file").click()
page.get_by_role("button", name="Convert").click()
expect(page.locator("#pp-b")).to_have_value(
"with t.html():\n with t.body():\n with t.h1():\n t('Hello, World!')"
)


def test_webcomponents(page: Page):
page.goto(f"http://localhost:{PORT}/")
page.get_by_role("link", name="Example 9: WebComponents").click()
page.get_by_role("button", name="Open Dialog").click()
page.locator("sl-button").filter(has_text="Close").get_by_role("button").click()


def test_a_full_app(page: Page):
page.goto(f"http://localhost:{PORT}/")

page.get_by_role("link", name="Example 10: A full-blown app").click()
page.get_by_label("Username").click()
page.get_by_label("Username").fill("foo")
page.get_by_label("Username").press("Tab")
page.get_by_label("Password").fill("bar")
page.get_by_role("button", name="Login").click()
page.get_by_role("heading", name="Hello, you are authenticated").click()
page.get_by_label("User Settings").click()
page.get_by_role("menuitem", name="Profile").locator("path").nth(3).click()
page.get_by_role("link", name="Dashboard").click()
page.get_by_role("heading", name="Hello, you are authenticated").click()
page.get_by_role("link", name="Charts").click()
page.get_by_role("link", name="Forms").click()
page.get_by_label("Name").click()
page.get_by_label("Name").fill("Hello")
page.get_by_role("button", name="Submit").click()
page.get_by_role("link", name="Dashboard").click()
page.get_by_role("heading", name="Hello, you are authenticated").click()
Loading