reflex/tests/components/base/test_script.py
benedikt-bartscher 3039b54a75
add module prefix to generated state names (#3214)
* add module prefix to state names

* fix state names in test_app

* update state names in test_state

* fix state names in test_var

* fix state name in test_component

* fix state names in test_format

* fix state names in test_foreach

* fix state names in test_cond

* fix state names in test_datatable

* fix state names in test_colors

* fix state names in test_script

* fix state names in test_match

* fix state name in event1 fixture

* fix pyright and darglint

* fix state names in state_tree

* fix state names in redis only test

* fix state names in test_client_storage

* fix state name in js template

* add `get_state_name` and `get_full_state_name` helpers for `AppHarness`

* fix state names in test_dynamic_routes

* use new state name helpers in test_client_storage

* fix state names in test_event_actions

* fix state names in test_event_chain

* fix state names in test_upload

* fix state name in test_login_flow

* fix state names in test_input

* fix state names in test_form_submit

* ruff

* validate state module names

* wtf is going on here?

* remove comments leftover from refactoring

* adjust new test_add_style_embedded_vars

* fix state name in state.js

* fix integration/test_client_state.py

new SessionStorage feature was added with more full state names that need to be formatted in

* fix pre-commit issues in test_client_storage.py

* adjust test_computed_vars

* adjust safe-guards

* fix redis tests with new exception state

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
2024-07-11 11:13:57 -07:00

72 lines
2.0 KiB
Python

"""Test that Script from next/script renders correctly."""
import pytest
from reflex.components.base.script import Script
from reflex.state import BaseState
def test_script_inline():
"""Test inline scripts are rendered as children."""
component = Script.create("let x = 42")
render_dict = component.render()
assert render_dict["name"] == "Script"
assert not render_dict["contents"]
assert len(render_dict["children"]) == 1
assert render_dict["children"][0]["contents"] == "{`let x = 42`}"
def test_script_src():
"""Test src prop is rendered without children."""
component = Script.create(src="foo.js")
render_dict = component.render()
assert render_dict["name"] == "Script"
assert not render_dict["contents"]
assert not render_dict["children"]
assert "src={`foo.js`}" in render_dict["props"]
def test_script_neither():
"""Specifying neither children nor src is a ValueError."""
with pytest.raises(ValueError):
Script.create()
class EvState(BaseState):
"""State for testing event handlers."""
def on_ready(self):
"""Empty event handler."""
pass
def on_load(self):
"""Empty event handler."""
pass
def on_error(self):
"""Empty event handler."""
pass
def test_script_event_handler():
"""Test event handlers are rendered as expected."""
component = Script.create(
src="foo.js",
on_ready=EvState.on_ready,
on_load=EvState.on_load,
on_error=EvState.on_error,
)
render_dict = component.render()
assert (
f'onReady={{(_e) => addEvents([Event("{EvState.get_full_name()}.on_ready", {{}})], (_e), {{}})}}'
in render_dict["props"]
)
assert (
f'onLoad={{(_e) => addEvents([Event("{EvState.get_full_name()}.on_load", {{}})], (_e), {{}})}}'
in render_dict["props"]
)
assert (
f'onError={{(_e) => addEvents([Event("{EvState.get_full_name()}.on_error", {{}})], (_e), {{}})}}'
in render_dict["props"]
)