
* Codeblock cleanup in markdown * Initial approach to getting this working with rx.memo and reflex web * abstract the map var logic * the tests are not valid + pyright fix * darglint fix * Add unit tests plus mix components * pyi run * rebase on main * fix darglint * testing different OS * revert * This should fix it. Right? * Fix tests * minor fn signature fix * use ArgsFunctionOperation * use destructured args and pass the tests * fix remaining unit tests * fix pyi files * rebase on main * move language regex on codeblock to markdown * fix tests --------- Co-authored-by: Khaleel Al-Adhami <khaleel.aladhami@gmail.com>
76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
"""Test that Script from next/script renders correctly."""
|
|
|
|
import pytest
|
|
|
|
import reflex as rx
|
|
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."""
|
|
|
|
@rx.event
|
|
def on_ready(self):
|
|
"""Empty event handler."""
|
|
pass
|
|
|
|
@rx.event
|
|
def on_load(self):
|
|
"""Empty event handler."""
|
|
pass
|
|
|
|
@rx.event
|
|
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={{((...args) => (addEvents([(Event("{EvState.get_full_name()}.on_ready", ({{ }}), ({{ }})))], args, ({{ }}))))}}'
|
|
in render_dict["props"]
|
|
)
|
|
assert (
|
|
f'onLoad={{((...args) => (addEvents([(Event("{EvState.get_full_name()}.on_load", ({{ }}), ({{ }})))], args, ({{ }}))))}}'
|
|
in render_dict["props"]
|
|
)
|
|
assert (
|
|
f'onError={{((...args) => (addEvents([(Event("{EvState.get_full_name()}.on_error", ({{ }}), ({{ }})))], args, ({{ }}))))}}'
|
|
in render_dict["props"]
|
|
)
|