test_component.py 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  1. from contextlib import nullcontext
  2. from typing import Any, ClassVar
  3. import pytest
  4. import reflex as rx
  5. from reflex.base import Base
  6. from reflex.compiler.utils import compile_custom_component
  7. from reflex.components.base.bare import Bare
  8. from reflex.components.base.fragment import Fragment
  9. from reflex.components.component import (
  10. CUSTOM_COMPONENTS,
  11. Component,
  12. CustomComponent,
  13. StatefulComponent,
  14. custom_component,
  15. )
  16. from reflex.components.radix.themes.layout.box import Box
  17. from reflex.constants import EventTriggers
  18. from reflex.event import (
  19. EventChain,
  20. EventHandler,
  21. JavascriptInputEvent,
  22. input_event,
  23. no_args_event_spec,
  24. parse_args_spec,
  25. passthrough_event_spec,
  26. )
  27. from reflex.state import BaseState
  28. from reflex.style import Style
  29. from reflex.utils import imports
  30. from reflex.utils.exceptions import (
  31. ChildrenTypeError,
  32. EventFnArgMismatchError,
  33. EventHandlerArgTypeMismatchError,
  34. )
  35. from reflex.utils.imports import ImportDict, ImportVar, ParsedImportDict, parse_imports
  36. from reflex.vars import VarData
  37. from reflex.vars.base import LiteralVar, Var
  38. from reflex.vars.object import ObjectVar
  39. @pytest.fixture
  40. def test_state():
  41. class TestState(BaseState):
  42. num: int
  43. def do_something(self):
  44. pass
  45. def do_something_arg(self, arg):
  46. pass
  47. def do_something_with_bool(self, arg: bool):
  48. pass
  49. def do_something_with_int(self, arg: int):
  50. pass
  51. def do_something_with_list_int(self, arg: list[int]):
  52. pass
  53. def do_something_with_list_str(self, arg: list[str]):
  54. pass
  55. def do_something_required_optional(
  56. self, required_arg: int, optional_arg: int | None = None
  57. ):
  58. pass
  59. return TestState
  60. @pytest.fixture
  61. def component1() -> type[Component]:
  62. """A test component.
  63. Returns:
  64. A test component.
  65. """
  66. class TestComponent1(Component):
  67. # A test string prop.
  68. text: Var[str]
  69. # A test number prop.
  70. number: Var[int]
  71. # A test string/number prop.
  72. text_or_number: Var[int | str]
  73. def _get_imports(self) -> ParsedImportDict:
  74. return {"react": [ImportVar(tag="Component")]}
  75. def _get_custom_code(self) -> str:
  76. return "console.log('component1')"
  77. return TestComponent1
  78. @pytest.fixture
  79. def component2() -> type[Component]:
  80. """A test component.
  81. Returns:
  82. A test component.
  83. """
  84. def on_prop_event_spec(e0: Any):
  85. return [e0]
  86. class TestComponent2(Component):
  87. # A test list prop.
  88. arr: Var[list[str]]
  89. on_prop_event: EventHandler[on_prop_event_spec]
  90. def get_event_triggers(self) -> dict[str, Any]:
  91. """Test controlled triggers.
  92. Returns:
  93. Test controlled triggers.
  94. """
  95. return {
  96. **super().get_event_triggers(),
  97. "on_open": passthrough_event_spec(bool),
  98. "on_close": passthrough_event_spec(bool),
  99. "on_user_visited_count_changed": passthrough_event_spec(int),
  100. "on_two_args": passthrough_event_spec(int, int),
  101. "on_user_list_changed": passthrough_event_spec(list[str]),
  102. }
  103. def _get_imports(self) -> ParsedImportDict:
  104. return {"react-redux": [ImportVar(tag="connect")]}
  105. def _get_custom_code(self) -> str:
  106. return "console.log('component2')"
  107. return TestComponent2
  108. @pytest.fixture
  109. def component3() -> type[Component]:
  110. """A test component with hook defined.
  111. Returns:
  112. A test component.
  113. """
  114. class TestComponent3(Component):
  115. def _get_hooks(self) -> str:
  116. return "const a = () => true"
  117. return TestComponent3
  118. @pytest.fixture
  119. def component4() -> type[Component]:
  120. """A test component with hook defined.
  121. Returns:
  122. A test component.
  123. """
  124. class TestComponent4(Component):
  125. def _get_hooks(self) -> str:
  126. return "const b = () => false"
  127. return TestComponent4
  128. @pytest.fixture
  129. def component5() -> type[Component]:
  130. """A test component.
  131. Returns:
  132. A test component.
  133. """
  134. class TestComponent5(Component):
  135. tag = "RandomComponent"
  136. _invalid_children: ClassVar[list[str]] = ["Text"]
  137. _valid_children: ClassVar[list[str]] = ["Text"]
  138. _valid_parents: ClassVar[list[str]] = ["Text"]
  139. return TestComponent5
  140. @pytest.fixture
  141. def component6() -> type[Component]:
  142. """A test component.
  143. Returns:
  144. A test component.
  145. """
  146. class TestComponent6(Component):
  147. tag = "RandomComponent"
  148. _invalid_children: ClassVar[list[str]] = ["Text"]
  149. return TestComponent6
  150. @pytest.fixture
  151. def component7() -> type[Component]:
  152. """A test component.
  153. Returns:
  154. A test component.
  155. """
  156. class TestComponent7(Component):
  157. tag = "RandomComponent"
  158. _valid_children: ClassVar[list[str]] = ["Text"]
  159. return TestComponent7
  160. @pytest.fixture
  161. def on_click1() -> EventHandler:
  162. """A sample on click function.
  163. Returns:
  164. A sample on click function.
  165. """
  166. def on_click1():
  167. pass
  168. return EventHandler(fn=on_click1)
  169. @pytest.fixture
  170. def on_click2() -> EventHandler:
  171. """A sample on click function.
  172. Returns:
  173. A sample on click function.
  174. """
  175. def on_click2():
  176. pass
  177. return EventHandler(fn=on_click2)
  178. @pytest.fixture
  179. def my_component():
  180. """A test component function.
  181. Returns:
  182. A test component function.
  183. """
  184. def my_component(prop1: Var[str], prop2: Var[int]):
  185. return Box.create(prop1, prop2)
  186. return my_component
  187. def test_set_style_attrs(component1):
  188. """Test that style attributes are set in the dict.
  189. Args:
  190. component1: A test component.
  191. """
  192. component = component1.create(color="white", text_align="center")
  193. assert str(component.style["color"]) == '"white"'
  194. assert str(component.style["textAlign"]) == '"center"'
  195. def test_custom_attrs(component1):
  196. """Test that custom attributes are set in the dict.
  197. Args:
  198. component1: A test component.
  199. """
  200. component = component1.create(custom_attrs={"attr1": "1", "attr2": "attr2"})
  201. assert component.custom_attrs == {"attr1": "1", "attr2": "attr2"}
  202. def test_create_component(component1):
  203. """Test that the component is created correctly.
  204. Args:
  205. component1: A test component.
  206. """
  207. children = [component1.create() for _ in range(3)]
  208. attrs = {"color": "white", "text_align": "center"}
  209. c = component1.create(*children, **attrs)
  210. assert isinstance(c, component1)
  211. assert c.children == children
  212. assert (
  213. str(LiteralVar.create(c.style))
  214. == '({ ["color"] : "white", ["textAlign"] : "center" })'
  215. )
  216. @pytest.mark.parametrize(
  217. "prop_name,var,expected",
  218. [
  219. pytest.param(
  220. "text",
  221. LiteralVar.create("hello"),
  222. None,
  223. id="text",
  224. ),
  225. pytest.param(
  226. "text",
  227. Var(_js_expr="hello", _var_type=str | None),
  228. None,
  229. id="text-optional",
  230. ),
  231. pytest.param(
  232. "text",
  233. Var(_js_expr="hello", _var_type=str | None),
  234. None,
  235. id="text-union-str-none",
  236. ),
  237. pytest.param(
  238. "text",
  239. Var(_js_expr="hello", _var_type=None | str),
  240. None,
  241. id="text-union-none-str",
  242. ),
  243. pytest.param(
  244. "text",
  245. LiteralVar.create(1),
  246. TypeError,
  247. id="text-int",
  248. ),
  249. pytest.param(
  250. "number",
  251. LiteralVar.create(1),
  252. None,
  253. id="number",
  254. ),
  255. pytest.param(
  256. "number",
  257. Var(_js_expr="1", _var_type=int | None),
  258. None,
  259. id="number-optional",
  260. ),
  261. pytest.param(
  262. "number",
  263. Var(_js_expr="1", _var_type=int | None),
  264. None,
  265. id="number-union-int-none",
  266. ),
  267. pytest.param(
  268. "number",
  269. Var(_js_expr="1", _var_type=None | int),
  270. None,
  271. id="number-union-none-int",
  272. ),
  273. pytest.param(
  274. "number",
  275. LiteralVar.create("1"),
  276. TypeError,
  277. id="number-str",
  278. ),
  279. pytest.param(
  280. "text_or_number",
  281. LiteralVar.create("hello"),
  282. None,
  283. id="text_or_number-str",
  284. ),
  285. pytest.param(
  286. "text_or_number",
  287. LiteralVar.create(1),
  288. None,
  289. id="text_or_number-int",
  290. ),
  291. pytest.param(
  292. "text_or_number",
  293. Var(_js_expr="hello", _var_type=str | None),
  294. None,
  295. id="text_or_number-optional-str",
  296. ),
  297. pytest.param(
  298. "text_or_number",
  299. Var(_js_expr="hello", _var_type=str | None),
  300. None,
  301. id="text_or_number-union-str-none",
  302. ),
  303. pytest.param(
  304. "text_or_number",
  305. Var(_js_expr="hello", _var_type=None | str),
  306. None,
  307. id="text_or_number-union-none-str",
  308. ),
  309. pytest.param(
  310. "text_or_number",
  311. Var(_js_expr="1", _var_type=int | None),
  312. None,
  313. id="text_or_number-optional-int",
  314. ),
  315. pytest.param(
  316. "text_or_number",
  317. Var(_js_expr="1", _var_type=int | None),
  318. None,
  319. id="text_or_number-union-int-none",
  320. ),
  321. pytest.param(
  322. "text_or_number",
  323. Var(_js_expr="1", _var_type=None | int),
  324. None,
  325. id="text_or_number-union-none-int",
  326. ),
  327. pytest.param(
  328. "text_or_number",
  329. LiteralVar.create(1.0),
  330. TypeError,
  331. id="text_or_number-float",
  332. ),
  333. pytest.param(
  334. "text_or_number",
  335. Var(_js_expr="hello", _var_type=str | int | None),
  336. None,
  337. id="text_or_number-optional-union-str-int",
  338. ),
  339. ],
  340. )
  341. def test_create_component_prop_validation(
  342. component1: type[Component],
  343. prop_name: str,
  344. var: Var | str | int,
  345. expected: type[Exception],
  346. ):
  347. """Test that component props are validated correctly.
  348. Args:
  349. component1: A test component.
  350. prop_name: The name of the prop.
  351. var: The value of the prop.
  352. expected: The expected exception.
  353. """
  354. ctx = pytest.raises(expected) if expected else nullcontext()
  355. kwargs = {prop_name: var}
  356. with ctx:
  357. c = component1.create(**kwargs)
  358. assert isinstance(c, component1)
  359. assert c.children == []
  360. assert c.style == {}
  361. def test_add_style(component1, component2):
  362. """Test adding a style to a component.
  363. Args:
  364. component1: A test component.
  365. component2: A test component.
  366. """
  367. style = {
  368. component1: Style({"color": "white"}),
  369. component2: Style({"color": "black"}),
  370. }
  371. c1 = component1.create()._add_style_recursive(style)
  372. c2 = component2.create()._add_style_recursive(style)
  373. assert str(c1.style["color"]) == '"white"'
  374. assert str(c2.style["color"]) == '"black"'
  375. def test_add_style_create(component1, component2):
  376. """Test that adding style works with the create method.
  377. Args:
  378. component1: A test component.
  379. component2: A test component.
  380. """
  381. style = {
  382. component1.create: Style({"color": "white"}),
  383. component2.create: Style({"color": "black"}),
  384. }
  385. c1 = component1.create()._add_style_recursive(style)
  386. c2 = component2.create()._add_style_recursive(style)
  387. assert str(c1.style["color"]) == '"white"'
  388. assert str(c2.style["color"]) == '"black"'
  389. def test_get_imports(component1, component2):
  390. """Test getting the imports of a component.
  391. Args:
  392. component1: A test component.
  393. component2: A test component.
  394. """
  395. c1 = component1.create()
  396. c2 = component2.create(c1)
  397. assert c1._get_all_imports() == {"react": [ImportVar(tag="Component")]}
  398. assert c2._get_all_imports() == {
  399. "react-redux": [ImportVar(tag="connect")],
  400. "react": [ImportVar(tag="Component")],
  401. }
  402. def test_get_custom_code(component1, component2):
  403. """Test getting the custom code of a component.
  404. Args:
  405. component1: A test component.
  406. component2: A test component.
  407. """
  408. # Check that the code gets compiled correctly.
  409. c1 = component1.create()
  410. c2 = component2.create()
  411. assert c1._get_all_custom_code() == {"console.log('component1')"}
  412. assert c2._get_all_custom_code() == {"console.log('component2')"}
  413. # Check that nesting components compiles both codes.
  414. c1 = component1.create(c2)
  415. assert c1._get_all_custom_code() == {
  416. "console.log('component1')",
  417. "console.log('component2')",
  418. }
  419. # Check that code is not duplicated.
  420. c1 = component1.create(c2, c2, c1, c1)
  421. assert c1._get_all_custom_code() == {
  422. "console.log('component1')",
  423. "console.log('component2')",
  424. }
  425. def test_get_props(component1, component2):
  426. """Test that the props are set correctly.
  427. Args:
  428. component1: A test component.
  429. component2: A test component.
  430. """
  431. assert component1.get_props() == {"text", "number", "text_or_number"}
  432. assert component2.get_props() == {"arr", "on_prop_event"}
  433. @pytest.mark.parametrize(
  434. "text,number",
  435. [
  436. ("", 0),
  437. ("test", 1),
  438. ("hi", -13),
  439. ],
  440. )
  441. def test_valid_props(component1, text: str, number: int):
  442. """Test that we can construct a component with valid props.
  443. Args:
  444. component1: A test component.
  445. text: A test string.
  446. number: A test number.
  447. """
  448. c = component1.create(text=text, number=number)
  449. assert c.text._decode() == text
  450. assert c.number._decode() == number
  451. @pytest.mark.parametrize(
  452. "text,number", [("", "bad_string"), (13, 1), ("test", [1, 2, 3])]
  453. )
  454. def test_invalid_prop_type(component1, text: str, number: int):
  455. """Test that an invalid prop type raises an error.
  456. Args:
  457. component1: A test component.
  458. text: A test string.
  459. number: A test number.
  460. """
  461. # Check that
  462. with pytest.raises(TypeError):
  463. component1.create(text=text, number=number)
  464. def test_var_props(component1, test_state):
  465. """Test that we can set a Var prop.
  466. Args:
  467. component1: A test component.
  468. test_state: A test state.
  469. """
  470. c1 = component1.create(text="hello", number=test_state.num)
  471. assert c1.number.equals(test_state.num)
  472. def test_get_event_triggers(component1, component2):
  473. """Test that we can get the triggers of a component.
  474. Args:
  475. component1: A test component.
  476. component2: A test component.
  477. """
  478. default_triggers = {
  479. EventTriggers.ON_FOCUS,
  480. EventTriggers.ON_BLUR,
  481. EventTriggers.ON_CLICK,
  482. EventTriggers.ON_CONTEXT_MENU,
  483. EventTriggers.ON_DOUBLE_CLICK,
  484. EventTriggers.ON_MOUSE_DOWN,
  485. EventTriggers.ON_MOUSE_ENTER,
  486. EventTriggers.ON_MOUSE_LEAVE,
  487. EventTriggers.ON_MOUSE_MOVE,
  488. EventTriggers.ON_MOUSE_OUT,
  489. EventTriggers.ON_MOUSE_OVER,
  490. EventTriggers.ON_MOUSE_UP,
  491. EventTriggers.ON_SCROLL,
  492. EventTriggers.ON_MOUNT,
  493. EventTriggers.ON_UNMOUNT,
  494. }
  495. assert component1.create().get_event_triggers().keys() == default_triggers
  496. assert (
  497. component2.create().get_event_triggers().keys()
  498. == {
  499. "on_open",
  500. "on_close",
  501. "on_prop_event",
  502. "on_user_visited_count_changed",
  503. "on_two_args",
  504. "on_user_list_changed",
  505. }
  506. | default_triggers
  507. )
  508. @pytest.fixture
  509. def test_component() -> type[Component]:
  510. """A test component.
  511. Returns:
  512. A test component.
  513. """
  514. class TestComponent(Component):
  515. pass
  516. return TestComponent
  517. # Write a test case to check if the create method filters out None props
  518. def test_create_filters_none_props(test_component):
  519. child1 = test_component.create()
  520. child2 = test_component.create()
  521. props = {
  522. "prop1": "value1",
  523. "prop2": None,
  524. "prop3": "value3",
  525. "prop4": None,
  526. "style": {"color": "white", "text-align": "center"}, # Adding a style prop
  527. }
  528. component = test_component.create(child1, child2, **props)
  529. # Assert that None props are not present in the component's props
  530. assert "prop2" not in component.get_props()
  531. assert "prop4" not in component.get_props()
  532. # Assert that the style prop is present in the component's props
  533. assert str(component.style["color"]) == '"white"'
  534. assert str(component.style["textAlign"]) == '"center"'
  535. @pytest.mark.parametrize(
  536. "children",
  537. [
  538. ({"foo": "bar"},),
  539. ],
  540. )
  541. def test_component_create_unallowed_types(children, test_component):
  542. with pytest.raises(ChildrenTypeError):
  543. test_component.create(*children)
  544. @pytest.mark.parametrize(
  545. "element, expected",
  546. [
  547. (
  548. (rx.text("first_text"),),
  549. {
  550. "name": "Fragment",
  551. "props": [],
  552. "contents": "",
  553. "special_props": [],
  554. "children": [
  555. {
  556. "name": "RadixThemesText",
  557. "props": ['as:"p"'],
  558. "contents": "",
  559. "special_props": [],
  560. "children": [
  561. {
  562. "name": "",
  563. "props": [],
  564. "contents": '"first_text"',
  565. "special_props": [],
  566. "children": [],
  567. "autofocus": False,
  568. }
  569. ],
  570. "autofocus": False,
  571. }
  572. ],
  573. "autofocus": False,
  574. },
  575. ),
  576. (
  577. (rx.text("first_text"), rx.text("second_text")),
  578. {
  579. "autofocus": False,
  580. "children": [
  581. {
  582. "autofocus": False,
  583. "children": [
  584. {
  585. "autofocus": False,
  586. "children": [],
  587. "contents": '"first_text"',
  588. "name": "",
  589. "props": [],
  590. "special_props": [],
  591. }
  592. ],
  593. "contents": "",
  594. "name": "RadixThemesText",
  595. "props": ['as:"p"'],
  596. "special_props": [],
  597. },
  598. {
  599. "autofocus": False,
  600. "children": [
  601. {
  602. "autofocus": False,
  603. "children": [],
  604. "contents": '"second_text"',
  605. "name": "",
  606. "props": [],
  607. "special_props": [],
  608. }
  609. ],
  610. "contents": "",
  611. "name": "RadixThemesText",
  612. "props": ['as:"p"'],
  613. "special_props": [],
  614. },
  615. ],
  616. "contents": "",
  617. "name": "Fragment",
  618. "props": [],
  619. "special_props": [],
  620. },
  621. ),
  622. (
  623. (rx.text("first_text"), rx.box((rx.text("second_text"),))),
  624. {
  625. "autofocus": False,
  626. "children": [
  627. {
  628. "autofocus": False,
  629. "children": [
  630. {
  631. "autofocus": False,
  632. "children": [],
  633. "contents": '"first_text"',
  634. "name": "",
  635. "props": [],
  636. "special_props": [],
  637. }
  638. ],
  639. "contents": "",
  640. "name": "RadixThemesText",
  641. "props": ['as:"p"'],
  642. "special_props": [],
  643. },
  644. {
  645. "autofocus": False,
  646. "children": [
  647. {
  648. "autofocus": False,
  649. "children": [
  650. {
  651. "autofocus": False,
  652. "children": [
  653. {
  654. "autofocus": False,
  655. "children": [],
  656. "contents": '"second_text"',
  657. "name": "",
  658. "props": [],
  659. "special_props": [],
  660. }
  661. ],
  662. "contents": "",
  663. "name": "RadixThemesText",
  664. "props": ['as:"p"'],
  665. "special_props": [],
  666. }
  667. ],
  668. "contents": "",
  669. "name": "Fragment",
  670. "props": [],
  671. "special_props": [],
  672. }
  673. ],
  674. "contents": "",
  675. "name": "RadixThemesBox",
  676. "props": [],
  677. "special_props": [],
  678. },
  679. ],
  680. "contents": "",
  681. "name": "Fragment",
  682. "props": [],
  683. "special_props": [],
  684. },
  685. ),
  686. ],
  687. )
  688. def test_component_create_unpack_tuple_child(test_component, element, expected):
  689. """Test that component in tuples are unwrapped into an rx.Fragment.
  690. Args:
  691. test_component: Component fixture.
  692. element: The children to pass to the component.
  693. expected: The expected render dict.
  694. """
  695. comp = test_component.create(element)
  696. assert len(comp.children) == 1
  697. fragment_wrapper = comp.children[0]
  698. assert isinstance(fragment_wrapper, Fragment)
  699. assert fragment_wrapper.render() == expected
  700. class _Obj(Base):
  701. custom: int = 0
  702. class C1State(BaseState):
  703. """State for testing C1 component."""
  704. def mock_handler(self, _e: JavascriptInputEvent, _bravo: dict, _charlie: _Obj):
  705. """Mock handler."""
  706. pass
  707. def test_component_event_trigger_arbitrary_args():
  708. """Test that we can define arbitrary types for the args of an event trigger."""
  709. def on_foo_spec(
  710. _e: ObjectVar[JavascriptInputEvent],
  711. alpha: Var[str],
  712. bravo: dict[str, Any],
  713. charlie: ObjectVar[_Obj],
  714. ):
  715. return [_e.target.value, bravo["nested"], charlie.custom.to(int) + 42]
  716. class C1(Component):
  717. library = "/local"
  718. tag = "C1"
  719. def get_event_triggers(self) -> dict[str, Any]:
  720. return {
  721. **super().get_event_triggers(),
  722. "on_foo": on_foo_spec,
  723. }
  724. C1.create(on_foo=C1State.mock_handler)
  725. def test_create_custom_component(my_component):
  726. """Test that we can create a custom component.
  727. Args:
  728. my_component: A test custom component.
  729. """
  730. component = rx.memo(my_component)(prop1="test", prop2=1)
  731. assert component.tag == "MyComponent"
  732. assert component.get_props() == {"prop1", "prop2"}
  733. assert component.tag in CUSTOM_COMPONENTS
  734. def test_custom_component_hash(my_component):
  735. """Test that the hash of a custom component is correct.
  736. Args:
  737. my_component: A test custom component.
  738. """
  739. component1 = rx.memo(my_component)(prop1="test", prop2=1)
  740. component2 = rx.memo(my_component)(prop1="test", prop2=2)
  741. assert {component1, component2} == {component1}
  742. def test_custom_component_wrapper():
  743. """Test that the wrapper of a custom component is correct."""
  744. @custom_component
  745. def my_component(width: Var[int], color: Var[str]):
  746. return rx.box(
  747. width=width,
  748. color=color,
  749. )
  750. from reflex.components.radix.themes.typography.text import Text
  751. ccomponent = my_component(
  752. rx.text("child"), width=LiteralVar.create(1), color=LiteralVar.create("red")
  753. )
  754. assert isinstance(ccomponent, CustomComponent)
  755. assert len(ccomponent.children) == 1
  756. assert isinstance(ccomponent.children[0], Text)
  757. component = ccomponent.get_component(ccomponent)
  758. assert isinstance(component, Box)
  759. def test_invalid_event_handler_args(component2, test_state):
  760. """Test that an invalid event handler raises an error.
  761. Args:
  762. component2: A test component.
  763. test_state: A test state.
  764. """
  765. # EventHandler args must match
  766. with pytest.raises(EventFnArgMismatchError):
  767. component2.create(on_click=test_state.do_something_arg)
  768. # EventHandler args must have at least as many default args as the spec.
  769. with pytest.raises(EventFnArgMismatchError):
  770. component2.create(on_click=test_state.do_something_required_optional)
  771. # Multiple EventHandler args: all must match
  772. with pytest.raises(EventFnArgMismatchError):
  773. component2.create(
  774. on_click=[test_state.do_something_arg, test_state.do_something]
  775. )
  776. # # Event Handler types must match
  777. with pytest.raises(EventHandlerArgTypeMismatchError):
  778. component2.create(
  779. on_user_visited_count_changed=test_state.do_something_with_bool
  780. )
  781. with pytest.raises(EventHandlerArgTypeMismatchError):
  782. component2.create(on_user_list_changed=test_state.do_something_with_int)
  783. with pytest.raises(EventHandlerArgTypeMismatchError):
  784. component2.create(on_user_list_changed=test_state.do_something_with_list_int)
  785. component2.create(on_open=test_state.do_something_with_int)
  786. component2.create(on_open=test_state.do_something_with_bool)
  787. component2.create(on_user_visited_count_changed=test_state.do_something_with_int)
  788. component2.create(on_user_list_changed=test_state.do_something_with_list_str)
  789. # lambda cannot return weird values.
  790. with pytest.raises(ValueError):
  791. component2.create(on_click=lambda: 1)
  792. with pytest.raises(ValueError):
  793. component2.create(on_click=lambda: [1])
  794. with pytest.raises(ValueError):
  795. component2.create(
  796. on_click=lambda: (test_state.do_something_arg(1), test_state.do_something)
  797. )
  798. # lambda signature must match event trigger.
  799. with pytest.raises(EventFnArgMismatchError):
  800. component2.create(on_click=lambda _: test_state.do_something_arg(1))
  801. # lambda returning EventHandler must match spec
  802. with pytest.raises(EventFnArgMismatchError):
  803. component2.create(on_click=lambda: test_state.do_something_arg)
  804. # Mixed EventSpec and EventHandler must match spec.
  805. with pytest.raises(EventFnArgMismatchError):
  806. component2.create(
  807. on_click=lambda: [
  808. test_state.do_something_arg(1),
  809. test_state.do_something_arg,
  810. ]
  811. )
  812. def test_valid_event_handler_args(component2, test_state):
  813. """Test that an valid event handler args do not raise exception.
  814. Args:
  815. component2: A test component.
  816. test_state: A test state.
  817. """
  818. # Uncontrolled event handlers should not take args.
  819. component2.create(on_click=test_state.do_something)
  820. component2.create(on_click=test_state.do_something_arg(1))
  821. # Does not raise because event handlers are allowed to have less args than the spec.
  822. component2.create(on_open=test_state.do_something)
  823. component2.create(on_prop_event=test_state.do_something)
  824. # Does not raise because event handlers can have optional args.
  825. component2.create(
  826. on_user_visited_count_changed=test_state.do_something_required_optional
  827. )
  828. component2.create(on_two_args=test_state.do_something_required_optional)
  829. # Controlled event handlers should take args.
  830. component2.create(on_open=test_state.do_something_arg)
  831. component2.create(on_prop_event=test_state.do_something_arg)
  832. # Using a partial event spec bypasses arg validation (ignoring the args).
  833. component2.create(on_open=test_state.do_something())
  834. component2.create(on_prop_event=test_state.do_something())
  835. # Multiple EventHandler args: all must match
  836. component2.create(on_open=[test_state.do_something_arg, test_state.do_something])
  837. component2.create(
  838. on_prop_event=[test_state.do_something_arg, test_state.do_something]
  839. )
  840. # lambda returning EventHandler is okay if the spec matches.
  841. component2.create(on_click=lambda: test_state.do_something)
  842. component2.create(on_open=lambda _: test_state.do_something_arg)
  843. component2.create(on_prop_event=lambda _: test_state.do_something_arg)
  844. component2.create(on_open=lambda: test_state.do_something)
  845. component2.create(on_prop_event=lambda: test_state.do_something)
  846. component2.create(on_open=lambda _: test_state.do_something)
  847. component2.create(on_prop_event=lambda _: test_state.do_something)
  848. # lambda can always return an EventSpec.
  849. component2.create(on_click=lambda: test_state.do_something_arg(1))
  850. component2.create(on_open=lambda _: test_state.do_something_arg(1))
  851. component2.create(on_prop_event=lambda _: test_state.do_something_arg(1))
  852. # Return EventSpec and EventHandler (no arg).
  853. component2.create(
  854. on_click=lambda: [test_state.do_something_arg(1), test_state.do_something]
  855. )
  856. component2.create(
  857. on_click=lambda: [test_state.do_something_arg(1), test_state.do_something()]
  858. )
  859. # Return 2 EventSpec.
  860. component2.create(
  861. on_open=lambda _: [test_state.do_something_arg(1), test_state.do_something()]
  862. )
  863. component2.create(
  864. on_prop_event=lambda _: [
  865. test_state.do_something_arg(1),
  866. test_state.do_something(),
  867. ]
  868. )
  869. # Return EventHandler (1 arg) and EventSpec.
  870. component2.create(
  871. on_open=lambda _: [test_state.do_something_arg, test_state.do_something()]
  872. )
  873. component2.create(
  874. on_prop_event=lambda _: [test_state.do_something_arg, test_state.do_something()]
  875. )
  876. component2.create(
  877. on_open=lambda _: [test_state.do_something_arg(1), test_state.do_something]
  878. )
  879. component2.create(
  880. on_prop_event=lambda _: [
  881. test_state.do_something_arg(1),
  882. test_state.do_something,
  883. ]
  884. )
  885. def test_get_hooks_nested(component1, component2, component3):
  886. """Test that a component returns hooks from child components.
  887. Args:
  888. component1: test component.
  889. component2: another component.
  890. component3: component with hooks defined.
  891. """
  892. c = component1.create(
  893. component2.create(arr=[]),
  894. component3.create(),
  895. component3.create(),
  896. component3.create(),
  897. text="a",
  898. number=1,
  899. )
  900. assert c._get_all_hooks() == component3.create()._get_all_hooks()
  901. def test_get_hooks_nested2(component3, component4):
  902. """Test that a component returns both when parent and child have hooks.
  903. Args:
  904. component3: component with hooks defined.
  905. component4: component with different hooks defined.
  906. """
  907. exp_hooks = {
  908. **component3.create()._get_all_hooks(),
  909. **component4.create()._get_all_hooks(),
  910. }
  911. assert component3.create(component4.create())._get_all_hooks() == exp_hooks
  912. assert component4.create(component3.create())._get_all_hooks() == exp_hooks
  913. assert (
  914. component4.create(
  915. component3.create(),
  916. component4.create(),
  917. component3.create(),
  918. )._get_all_hooks()
  919. == exp_hooks
  920. )
  921. @pytest.mark.parametrize("fixture", ["component5", "component6"])
  922. def test_unsupported_child_components(fixture, request):
  923. """Test that a value error is raised when an unsupported component (a child component found in the
  924. component's invalid children list) is provided as a child.
  925. Args:
  926. fixture: the test component as a fixture.
  927. request: Pytest request.
  928. """
  929. component = request.getfixturevalue(fixture)
  930. with pytest.raises(ValueError) as err:
  931. comp = component.create(rx.text("testing component"))
  932. comp.render()
  933. assert (
  934. err.value.args[0]
  935. == f"The component `{component.__name__}` cannot have `Text` as a child component"
  936. )
  937. def test_unsupported_parent_components(component5):
  938. """Test that a value error is raised when an component is not in _valid_parents of one of its children.
  939. Args:
  940. component5: component with valid parent of "Text" only
  941. """
  942. with pytest.raises(ValueError) as err:
  943. rx.box(component5.create())
  944. assert (
  945. err.value.args[0]
  946. == f"The component `{component5.__name__}` can only be a child of the components: `{component5._valid_parents[0]}`. Got `Box` instead."
  947. )
  948. @pytest.mark.parametrize("fixture", ["component5", "component7"])
  949. def test_component_with_only_valid_children(fixture, request):
  950. """Test that a value error is raised when an unsupported component (a child component not found in the
  951. component's valid children list) is provided as a child.
  952. Args:
  953. fixture: the test component as a fixture.
  954. request: Pytest request.
  955. """
  956. component = request.getfixturevalue(fixture)
  957. with pytest.raises(ValueError) as err:
  958. comp = component.create(rx.box("testing component"))
  959. comp.render()
  960. assert (
  961. err.value.args[0]
  962. == f"The component `{component.__name__}` only allows the components: `Text` as children. "
  963. f"Got `Box` instead."
  964. )
  965. @pytest.mark.parametrize(
  966. "component,rendered",
  967. [
  968. (rx.text("hi"), 'jsx(\nRadixThemesText,\n{as:"p"},\n"hi"\n,)'),
  969. (
  970. rx.box(rx.heading("test", size="3")),
  971. 'jsx(\nRadixThemesBox,\n{},\njsx(\nRadixThemesHeading,\n{size:"3"},\n"test"\n,),)',
  972. ),
  973. ],
  974. )
  975. def test_format_component(component, rendered):
  976. """Test that a component is formatted correctly.
  977. Args:
  978. component: The component to format.
  979. rendered: The expected rendered component.
  980. """
  981. assert str(component) == rendered
  982. def test_stateful_component(test_state):
  983. """Test that a stateful component is created correctly.
  984. Args:
  985. test_state: A test state.
  986. """
  987. text_component = rx.text(test_state.num)
  988. stateful_component = StatefulComponent.compile_from(text_component)
  989. assert isinstance(stateful_component, StatefulComponent)
  990. assert stateful_component.tag is not None
  991. assert stateful_component.tag.startswith("Text_")
  992. assert stateful_component.references == 1
  993. sc2 = StatefulComponent.compile_from(rx.text(test_state.num))
  994. assert isinstance(sc2, StatefulComponent)
  995. assert stateful_component.references == 2
  996. assert sc2.references == 2
  997. def test_stateful_component_memoize_event_trigger(test_state):
  998. """Test that a stateful component is created correctly with events.
  999. Args:
  1000. test_state: A test state.
  1001. """
  1002. button_component = rx.button("Click me", on_click=test_state.do_something)
  1003. stateful_component = StatefulComponent.compile_from(button_component)
  1004. assert isinstance(stateful_component, StatefulComponent)
  1005. # No event trigger? No StatefulComponent
  1006. assert not isinstance(
  1007. StatefulComponent.compile_from(rx.button("Click me")), StatefulComponent
  1008. )
  1009. def test_stateful_banner():
  1010. """Test that a stateful component is created correctly with events."""
  1011. connection_modal_component = rx.connection_modal()
  1012. stateful_component = StatefulComponent.compile_from(connection_modal_component)
  1013. assert isinstance(stateful_component, StatefulComponent)
  1014. TEST_VAR = LiteralVar.create("test")._replace(
  1015. merge_var_data=VarData(
  1016. hooks={"useTest": None},
  1017. imports={"test": [ImportVar(tag="test")]},
  1018. state="Test",
  1019. )
  1020. )
  1021. FORMATTED_TEST_VAR = LiteralVar.create(f"foo{TEST_VAR}bar")
  1022. STYLE_VAR = TEST_VAR._replace(_js_expr="style")
  1023. EVENT_CHAIN_VAR = TEST_VAR.to(EventChain)
  1024. ARG_VAR = Var(_js_expr="arg")
  1025. TEST_VAR_DICT_OF_DICT = LiteralVar.create({"a": {"b": "test"}})._replace(
  1026. merge_var_data=TEST_VAR._var_data
  1027. )
  1028. FORMATTED_TEST_VAR_DICT_OF_DICT = LiteralVar.create(
  1029. {"a": {"b": "footestbar"}}
  1030. )._replace(merge_var_data=TEST_VAR._var_data)
  1031. TEST_VAR_LIST_OF_LIST = LiteralVar.create([["test"]])._replace(
  1032. merge_var_data=TEST_VAR._var_data
  1033. )
  1034. FORMATTED_TEST_VAR_LIST_OF_LIST = LiteralVar.create([["footestbar"]])._replace(
  1035. merge_var_data=TEST_VAR._var_data
  1036. )
  1037. TEST_VAR_LIST_OF_LIST_OF_LIST = LiteralVar.create([[["test"]]])._replace(
  1038. merge_var_data=TEST_VAR._var_data
  1039. )
  1040. FORMATTED_TEST_VAR_LIST_OF_LIST_OF_LIST = LiteralVar.create(
  1041. [[["footestbar"]]]
  1042. )._replace(merge_var_data=TEST_VAR._var_data)
  1043. TEST_VAR_LIST_OF_DICT = LiteralVar.create([{"a": "test"}])._replace(
  1044. merge_var_data=TEST_VAR._var_data
  1045. )
  1046. FORMATTED_TEST_VAR_LIST_OF_DICT = LiteralVar.create([{"a": "footestbar"}])._replace(
  1047. merge_var_data=TEST_VAR._var_data
  1048. )
  1049. class ComponentNestedVar(Component):
  1050. """A component with nested Var types."""
  1051. dict_of_dict: Var[dict[str, dict[str, str]]]
  1052. list_of_list: Var[list[list[str]]]
  1053. list_of_list_of_list: Var[list[list[list[str]]]]
  1054. list_of_dict: Var[list[dict[str, str]]]
  1055. class EventState(rx.State):
  1056. """State for testing event handlers with _get_vars."""
  1057. v: int = 42
  1058. @rx.event
  1059. def handler(self):
  1060. """A handler that does nothing."""
  1061. def handler2(self, arg):
  1062. """A handler that takes an arg.
  1063. Args:
  1064. arg: An arg.
  1065. """
  1066. @pytest.mark.parametrize(
  1067. ("component", "exp_vars"),
  1068. (
  1069. pytest.param(
  1070. Bare.create(TEST_VAR),
  1071. [TEST_VAR],
  1072. id="direct-bare",
  1073. ),
  1074. pytest.param(
  1075. Bare.create(f"foo{TEST_VAR}bar"),
  1076. [FORMATTED_TEST_VAR],
  1077. id="fstring-bare",
  1078. ),
  1079. pytest.param(
  1080. rx.text(as_=TEST_VAR),
  1081. [TEST_VAR],
  1082. id="direct-prop",
  1083. ),
  1084. pytest.param(
  1085. rx.heading(as_=f"foo{TEST_VAR}bar"),
  1086. [FORMATTED_TEST_VAR],
  1087. id="fstring-prop",
  1088. ),
  1089. pytest.param(
  1090. rx.fragment(id=TEST_VAR),
  1091. [TEST_VAR],
  1092. id="direct-id",
  1093. ),
  1094. pytest.param(
  1095. rx.fragment(id=f"foo{TEST_VAR}bar"),
  1096. [FORMATTED_TEST_VAR],
  1097. id="fstring-id",
  1098. ),
  1099. pytest.param(
  1100. rx.fragment(key=TEST_VAR),
  1101. [TEST_VAR],
  1102. id="direct-key",
  1103. ),
  1104. pytest.param(
  1105. rx.fragment(key=f"foo{TEST_VAR}bar"),
  1106. [FORMATTED_TEST_VAR],
  1107. id="fstring-key",
  1108. ),
  1109. pytest.param(
  1110. rx.fragment(class_name=TEST_VAR),
  1111. [TEST_VAR],
  1112. id="direct-class_name",
  1113. ),
  1114. pytest.param(
  1115. rx.fragment(class_name=f"foo{TEST_VAR}bar"),
  1116. [FORMATTED_TEST_VAR],
  1117. id="fstring-class_name",
  1118. ),
  1119. pytest.param(
  1120. rx.fragment(class_name=f"foo{TEST_VAR}bar other-class"),
  1121. [LiteralVar.create(f"{FORMATTED_TEST_VAR} other-class")],
  1122. id="fstring-dual-class_name",
  1123. ),
  1124. pytest.param(
  1125. rx.fragment(class_name=[TEST_VAR, "other-class"]),
  1126. [Var.create([TEST_VAR, "other-class"]).join(" ")],
  1127. id="fstring-dual-class_name",
  1128. ),
  1129. pytest.param(
  1130. rx.fragment(special_props=[TEST_VAR]),
  1131. [TEST_VAR],
  1132. id="direct-special_props",
  1133. ),
  1134. pytest.param(
  1135. rx.fragment(special_props=[LiteralVar.create(f"foo{TEST_VAR}bar")]),
  1136. [FORMATTED_TEST_VAR],
  1137. id="fstring-special_props",
  1138. ),
  1139. pytest.param(
  1140. # custom_attrs cannot accept a Var directly as a value
  1141. rx.fragment(custom_attrs={"href": f"{TEST_VAR}"}),
  1142. [TEST_VAR],
  1143. id="fstring-custom_attrs-nofmt",
  1144. ),
  1145. pytest.param(
  1146. rx.fragment(custom_attrs={"href": f"foo{TEST_VAR}bar"}),
  1147. [FORMATTED_TEST_VAR],
  1148. id="fstring-custom_attrs",
  1149. ),
  1150. pytest.param(
  1151. rx.fragment(background_color=TEST_VAR),
  1152. [STYLE_VAR],
  1153. id="direct-background_color",
  1154. ),
  1155. pytest.param(
  1156. rx.fragment(background_color=f"foo{TEST_VAR}bar"),
  1157. [STYLE_VAR],
  1158. id="fstring-background_color",
  1159. ),
  1160. pytest.param(
  1161. rx.fragment(style={"background_color": TEST_VAR}),
  1162. [STYLE_VAR],
  1163. id="direct-style-background_color",
  1164. ),
  1165. pytest.param(
  1166. rx.fragment(style={"background_color": f"foo{TEST_VAR}bar"}),
  1167. [STYLE_VAR],
  1168. id="fstring-style-background_color",
  1169. ),
  1170. pytest.param(
  1171. rx.fragment(on_click=EVENT_CHAIN_VAR),
  1172. [EVENT_CHAIN_VAR],
  1173. id="direct-event-chain",
  1174. ),
  1175. pytest.param(
  1176. rx.fragment(on_click=EventState.handler),
  1177. [],
  1178. id="direct-event-handler",
  1179. ),
  1180. pytest.param(
  1181. rx.fragment(on_click=EventState.handler2(TEST_VAR)), # pyright: ignore [reportCallIssue]
  1182. [ARG_VAR, TEST_VAR],
  1183. id="direct-event-handler-arg",
  1184. ),
  1185. pytest.param(
  1186. rx.fragment(on_click=EventState.handler2(EventState.v)), # pyright: ignore [reportCallIssue]
  1187. [ARG_VAR, EventState.v],
  1188. id="direct-event-handler-arg2",
  1189. ),
  1190. pytest.param(
  1191. rx.fragment(on_click=lambda: EventState.handler2(TEST_VAR)), # pyright: ignore [reportCallIssue]
  1192. [ARG_VAR, TEST_VAR],
  1193. id="direct-event-handler-lambda",
  1194. ),
  1195. pytest.param(
  1196. ComponentNestedVar.create(dict_of_dict={"a": {"b": TEST_VAR}}),
  1197. [TEST_VAR_DICT_OF_DICT],
  1198. id="direct-dict_of_dict",
  1199. ),
  1200. pytest.param(
  1201. ComponentNestedVar.create(dict_of_dict={"a": {"b": f"foo{TEST_VAR}bar"}}),
  1202. [FORMATTED_TEST_VAR_DICT_OF_DICT],
  1203. id="fstring-dict_of_dict",
  1204. ),
  1205. pytest.param(
  1206. ComponentNestedVar.create(list_of_list=[[TEST_VAR]]),
  1207. [TEST_VAR_LIST_OF_LIST],
  1208. id="direct-list_of_list",
  1209. ),
  1210. pytest.param(
  1211. ComponentNestedVar.create(list_of_list=[[f"foo{TEST_VAR}bar"]]),
  1212. [FORMATTED_TEST_VAR_LIST_OF_LIST],
  1213. id="fstring-list_of_list",
  1214. ),
  1215. pytest.param(
  1216. ComponentNestedVar.create(list_of_list_of_list=[[[TEST_VAR]]]),
  1217. [TEST_VAR_LIST_OF_LIST_OF_LIST],
  1218. id="direct-list_of_list_of_list",
  1219. ),
  1220. pytest.param(
  1221. ComponentNestedVar.create(list_of_list_of_list=[[[f"foo{TEST_VAR}bar"]]]),
  1222. [FORMATTED_TEST_VAR_LIST_OF_LIST_OF_LIST],
  1223. id="fstring-list_of_list_of_list",
  1224. ),
  1225. pytest.param(
  1226. ComponentNestedVar.create(list_of_dict=[{"a": TEST_VAR}]),
  1227. [TEST_VAR_LIST_OF_DICT],
  1228. id="direct-list_of_dict",
  1229. ),
  1230. pytest.param(
  1231. ComponentNestedVar.create(list_of_dict=[{"a": f"foo{TEST_VAR}bar"}]),
  1232. [FORMATTED_TEST_VAR_LIST_OF_DICT],
  1233. id="fstring-list_of_dict",
  1234. ),
  1235. ),
  1236. )
  1237. def test_get_vars(component, exp_vars):
  1238. comp_vars = sorted(component._get_vars(), key=lambda v: v._js_expr)
  1239. assert len(comp_vars) == len(exp_vars)
  1240. print(comp_vars, exp_vars)
  1241. for comp_var, exp_var in zip(
  1242. comp_vars,
  1243. sorted(exp_vars, key=lambda v: v._js_expr),
  1244. strict=True,
  1245. ):
  1246. assert comp_var.equals(exp_var)
  1247. def test_instantiate_all_components():
  1248. """Test that all components can be instantiated."""
  1249. # These components all have required arguments and cannot be trivially instantiated.
  1250. untested_components = {
  1251. "Card",
  1252. "Cond",
  1253. "DebounceInput",
  1254. "Foreach",
  1255. "FormControl",
  1256. "Html",
  1257. "Icon",
  1258. "Match",
  1259. "Markdown",
  1260. "MultiSelect",
  1261. "Option",
  1262. "Popover",
  1263. "Radio",
  1264. "Script",
  1265. "Tag",
  1266. "Tfoot",
  1267. "Thead",
  1268. }
  1269. component_nested_list = [
  1270. *rx.RADIX_MAPPING.values(),
  1271. *rx.COMPONENTS_BASE_MAPPING.values(),
  1272. *rx.COMPONENTS_CORE_MAPPING.values(),
  1273. ]
  1274. for component_name in [
  1275. comp_name
  1276. for submodule_list in component_nested_list
  1277. for comp_name in submodule_list
  1278. ]:
  1279. if component_name in untested_components:
  1280. continue
  1281. component = getattr(
  1282. rx,
  1283. (
  1284. component_name
  1285. if not isinstance(component_name, tuple)
  1286. else component_name[1]
  1287. ),
  1288. )
  1289. if isinstance(component, type) and issubclass(component, Component):
  1290. component.create()
  1291. class InvalidParentComponent(Component):
  1292. """Invalid Parent Component."""
  1293. ...
  1294. class ValidComponent1(Component):
  1295. """Test valid component."""
  1296. _valid_children = ["ValidComponent2"]
  1297. class ValidComponent2(Component):
  1298. """Test valid component."""
  1299. ...
  1300. class ValidComponent3(Component):
  1301. """Test valid component."""
  1302. _valid_parents = ["ValidComponent2"]
  1303. class ValidComponent4(Component):
  1304. """Test valid component."""
  1305. _invalid_children = ["InvalidComponent"]
  1306. class InvalidComponent(Component):
  1307. """Test invalid component."""
  1308. ...
  1309. valid_component1 = ValidComponent1.create
  1310. valid_component2 = ValidComponent2.create
  1311. invalid_component = InvalidComponent.create
  1312. valid_component3 = ValidComponent3.create
  1313. invalid_parent = InvalidParentComponent.create
  1314. valid_component4 = ValidComponent4.create
  1315. def test_validate_valid_children():
  1316. valid_component1(valid_component2())
  1317. valid_component1(
  1318. rx.fragment(valid_component2()),
  1319. )
  1320. valid_component1(
  1321. rx.fragment(
  1322. rx.fragment(
  1323. rx.fragment(valid_component2()),
  1324. ),
  1325. ),
  1326. )
  1327. valid_component1(
  1328. rx.cond(
  1329. True,
  1330. rx.fragment(valid_component2()),
  1331. rx.fragment(
  1332. rx.foreach(LiteralVar.create([1, 2, 3]), lambda x: valid_component2(x))
  1333. ),
  1334. )
  1335. )
  1336. valid_component1(
  1337. rx.cond(
  1338. True,
  1339. valid_component2(),
  1340. rx.fragment(
  1341. rx.match(
  1342. "condition",
  1343. ("first", valid_component2()),
  1344. rx.fragment(valid_component2(rx.text("default"))),
  1345. )
  1346. ),
  1347. )
  1348. )
  1349. valid_component1(
  1350. rx.match(
  1351. "condition",
  1352. ("first", valid_component2()),
  1353. ("second", "third", rx.fragment(valid_component2())),
  1354. (
  1355. "fourth",
  1356. rx.cond(True, valid_component2(), rx.fragment(valid_component2())),
  1357. ),
  1358. (
  1359. "fifth",
  1360. rx.match(
  1361. "nested_condition",
  1362. ("nested_first", valid_component2()),
  1363. rx.fragment(valid_component2()),
  1364. ),
  1365. valid_component2(),
  1366. ),
  1367. )
  1368. )
  1369. def test_validate_valid_parents():
  1370. valid_component2(valid_component3())
  1371. valid_component2(
  1372. rx.fragment(valid_component3()),
  1373. )
  1374. valid_component1(
  1375. rx.fragment(
  1376. valid_component2(
  1377. rx.fragment(valid_component3()),
  1378. ),
  1379. ),
  1380. )
  1381. valid_component2(
  1382. rx.cond(
  1383. True,
  1384. rx.fragment(valid_component3()),
  1385. rx.fragment(
  1386. rx.foreach(
  1387. LiteralVar.create([1, 2, 3]),
  1388. lambda x: valid_component2(valid_component3(x)),
  1389. )
  1390. ),
  1391. )
  1392. )
  1393. valid_component2(
  1394. rx.cond(
  1395. True,
  1396. valid_component3(),
  1397. rx.fragment(
  1398. rx.match(
  1399. "condition",
  1400. ("first", valid_component3()),
  1401. rx.fragment(valid_component3(rx.text("default"))),
  1402. )
  1403. ),
  1404. )
  1405. )
  1406. valid_component2(
  1407. rx.match(
  1408. "condition",
  1409. ("first", valid_component3()),
  1410. ("second", "third", rx.fragment(valid_component3())),
  1411. (
  1412. "fourth",
  1413. rx.cond(True, valid_component3(), rx.fragment(valid_component3())),
  1414. ),
  1415. (
  1416. "fifth",
  1417. rx.match(
  1418. "nested_condition",
  1419. ("nested_first", valid_component3()),
  1420. rx.fragment(valid_component3()),
  1421. ),
  1422. valid_component3(),
  1423. ),
  1424. )
  1425. )
  1426. def test_validate_invalid_children():
  1427. with pytest.raises(ValueError):
  1428. valid_component4(invalid_component())
  1429. with pytest.raises(ValueError):
  1430. valid_component4(
  1431. rx.fragment(invalid_component()),
  1432. )
  1433. with pytest.raises(ValueError):
  1434. rx.el.p(rx.el.p("what"))
  1435. with pytest.raises(ValueError):
  1436. rx.el.p(rx.el.div("what"))
  1437. with pytest.raises(ValueError):
  1438. rx.el.button(rx.el.button("what"))
  1439. with pytest.raises(ValueError):
  1440. rx.el.p(rx.el.ol(rx.el.li("what")))
  1441. with pytest.raises(ValueError):
  1442. rx.el.p(rx.el.ul(rx.el.li("what")))
  1443. with pytest.raises(ValueError):
  1444. rx.el.a(rx.el.a("what"))
  1445. with pytest.raises(ValueError):
  1446. valid_component2(
  1447. rx.fragment(
  1448. valid_component4(
  1449. rx.fragment(invalid_component()),
  1450. ),
  1451. ),
  1452. )
  1453. with pytest.raises(ValueError):
  1454. valid_component4(
  1455. rx.cond(
  1456. True,
  1457. rx.fragment(invalid_component()),
  1458. rx.fragment(
  1459. rx.foreach(
  1460. LiteralVar.create([1, 2, 3]), lambda x: invalid_component(x)
  1461. )
  1462. ),
  1463. )
  1464. )
  1465. with pytest.raises(ValueError):
  1466. valid_component4(
  1467. rx.cond(
  1468. True,
  1469. invalid_component(),
  1470. rx.fragment(
  1471. rx.match(
  1472. "condition",
  1473. ("first", invalid_component()),
  1474. rx.fragment(invalid_component(rx.text("default"))),
  1475. )
  1476. ),
  1477. )
  1478. )
  1479. with pytest.raises(ValueError):
  1480. valid_component4(
  1481. rx.match(
  1482. "condition",
  1483. ("first", invalid_component()),
  1484. ("second", "third", rx.fragment(invalid_component())),
  1485. (
  1486. "fourth",
  1487. rx.cond(True, invalid_component(), rx.fragment(valid_component2())),
  1488. ),
  1489. (
  1490. "fifth",
  1491. rx.match(
  1492. "nested_condition",
  1493. ("nested_first", invalid_component()),
  1494. rx.fragment(invalid_component()),
  1495. ),
  1496. invalid_component(),
  1497. ),
  1498. )
  1499. )
  1500. def test_rename_props():
  1501. """Test that _rename_props works and is inherited."""
  1502. class C1(Component):
  1503. tag = "C1"
  1504. prop1: Var[str]
  1505. prop2: Var[str]
  1506. _rename_props = {"prop1": "renamed_prop1", "prop2": "renamed_prop2"}
  1507. class C2(C1):
  1508. tag = "C2"
  1509. prop3: Var[str]
  1510. _rename_props = {"prop2": "subclass_prop2", "prop3": "renamed_prop3"}
  1511. c1 = C1.create(prop1="prop1_1", prop2="prop2_1")
  1512. rendered_c1 = c1.render()
  1513. assert 'renamed_prop1:"prop1_1"' in rendered_c1["props"]
  1514. assert 'renamed_prop2:"prop2_1"' in rendered_c1["props"]
  1515. c2 = C2.create(prop1="prop1_2", prop2="prop2_2", prop3="prop3_2")
  1516. rendered_c2 = c2.render()
  1517. assert 'renamed_prop1:"prop1_2"' in rendered_c2["props"]
  1518. assert 'subclass_prop2:"prop2_2"' in rendered_c2["props"]
  1519. assert 'renamed_prop3:"prop3_2"' in rendered_c2["props"]
  1520. def test_custom_component_get_imports():
  1521. class Inner(Component):
  1522. tag = "Inner"
  1523. library = "inner"
  1524. class Other(Component):
  1525. tag = "Other"
  1526. library = "other"
  1527. @rx.memo
  1528. def wrapper():
  1529. return Inner.create()
  1530. @rx.memo
  1531. def outer(c: Component):
  1532. return Other.create(c)
  1533. custom_comp = wrapper()
  1534. # Inner is not imported directly, but it is imported by the custom component.
  1535. assert "inner" not in custom_comp._get_all_imports()
  1536. assert "outer" not in custom_comp._get_all_imports()
  1537. # The imports are only resolved during compilation.
  1538. custom_comp.get_component(custom_comp)
  1539. _, imports_inner = compile_custom_component(custom_comp)
  1540. assert "inner" in imports_inner
  1541. assert "outer" not in imports_inner
  1542. outer_comp = outer(c=wrapper())
  1543. # Libraries are not imported directly, but are imported by the custom component.
  1544. assert "inner" not in outer_comp._get_all_imports()
  1545. assert "other" not in outer_comp._get_all_imports()
  1546. # The imports are only resolved during compilation.
  1547. _, imports_outer = compile_custom_component(outer_comp)
  1548. assert "inner" not in imports_outer
  1549. assert "other" in imports_outer
  1550. def test_custom_component_declare_event_handlers_in_fields():
  1551. class ReferenceComponent(Component):
  1552. def get_event_triggers(self) -> dict[str, Any]:
  1553. """Test controlled triggers.
  1554. Returns:
  1555. Test controlled triggers.
  1556. """
  1557. return {
  1558. **super().get_event_triggers(),
  1559. "on_b": input_event,
  1560. "on_d": lambda: [],
  1561. "on_e": lambda: [],
  1562. }
  1563. class TestComponent(Component):
  1564. on_b: EventHandler[input_event]
  1565. on_d: EventHandler[no_args_event_spec]
  1566. on_e: EventHandler
  1567. custom_component = ReferenceComponent.create()
  1568. test_component = TestComponent.create()
  1569. custom_triggers = custom_component.get_event_triggers()
  1570. test_triggers = test_component.get_event_triggers()
  1571. assert custom_triggers.keys() == test_triggers.keys()
  1572. for trigger_name in custom_component.get_event_triggers():
  1573. for v1, v2 in zip(
  1574. parse_args_spec(test_triggers[trigger_name]),
  1575. parse_args_spec(custom_triggers[trigger_name]),
  1576. strict=True,
  1577. ):
  1578. assert v1.equals(v2)
  1579. def test_invalid_event_trigger():
  1580. class TriggerComponent(Component):
  1581. on_push: Var[bool]
  1582. def get_event_triggers(self) -> dict[str, Any]:
  1583. """Test controlled triggers.
  1584. Returns:
  1585. Test controlled triggers.
  1586. """
  1587. return {
  1588. **super().get_event_triggers(),
  1589. "on_a": lambda: [],
  1590. }
  1591. trigger_comp = TriggerComponent.create
  1592. # test that these do not throw errors.
  1593. trigger_comp(on_push=True)
  1594. trigger_comp(on_a=rx.console_log("log"))
  1595. with pytest.raises(ValueError):
  1596. trigger_comp(on_b=rx.console_log("log"))
  1597. @pytest.mark.parametrize(
  1598. "tags",
  1599. (
  1600. ["Component"],
  1601. ["Component", "useState"],
  1602. [ImportVar(tag="Component")],
  1603. [ImportVar(tag="Component"), ImportVar(tag="useState")],
  1604. ["Component", ImportVar(tag="useState")],
  1605. ),
  1606. )
  1607. def test_component_add_imports(tags):
  1608. class BaseComponent(Component):
  1609. def _get_imports(self) -> ImportDict: # pyright: ignore [reportIncompatibleMethodOverride]
  1610. return {}
  1611. class Reference(Component):
  1612. def _get_imports(self) -> ParsedImportDict:
  1613. return imports.merge_imports(
  1614. super()._get_imports(),
  1615. parse_imports({"react": tags}),
  1616. {"foo": [ImportVar(tag="bar")]},
  1617. )
  1618. class TestBase(Component):
  1619. def add_imports( # pyright: ignore [reportIncompatibleMethodOverride]
  1620. self,
  1621. ) -> dict[str, str | ImportVar | list[str] | list[ImportVar]]:
  1622. return {"foo": "bar"}
  1623. class Test(TestBase):
  1624. def add_imports(
  1625. self,
  1626. ) -> dict[str, str | ImportVar | list[str] | list[ImportVar]]:
  1627. return {"react": (tags[0] if len(tags) == 1 else tags)}
  1628. baseline = Reference.create()
  1629. test = Test.create()
  1630. assert baseline._get_all_imports() == parse_imports(
  1631. {
  1632. "react": tags,
  1633. "foo": [ImportVar(tag="bar")],
  1634. }
  1635. )
  1636. assert test._get_all_imports() == baseline._get_all_imports()
  1637. def test_component_add_hooks():
  1638. class BaseComponent(Component):
  1639. def _get_hooks(self):
  1640. return "const hook1 = 42"
  1641. class ChildComponent1(BaseComponent):
  1642. pass
  1643. class GrandchildComponent1(ChildComponent1):
  1644. def add_hooks(self): # pyright: ignore [reportIncompatibleMethodOverride]
  1645. return [
  1646. "const hook2 = 43",
  1647. "const hook3 = 44",
  1648. ]
  1649. class GreatGrandchildComponent1(GrandchildComponent1):
  1650. def add_hooks(self):
  1651. return [
  1652. "const hook4 = 45",
  1653. ]
  1654. class GrandchildComponent2(ChildComponent1):
  1655. def _get_hooks(self): # pyright: ignore [reportIncompatibleMethodOverride]
  1656. return "const hook5 = 46"
  1657. class GreatGrandchildComponent2(GrandchildComponent2):
  1658. def add_hooks(self): # pyright: ignore [reportIncompatibleMethodOverride]
  1659. return [
  1660. "const hook2 = 43",
  1661. "const hook6 = 47",
  1662. ]
  1663. assert list(BaseComponent.create()._get_all_hooks()) == ["const hook1 = 42"]
  1664. assert list(ChildComponent1.create()._get_all_hooks()) == ["const hook1 = 42"]
  1665. assert list(GrandchildComponent1.create()._get_all_hooks()) == [
  1666. "const hook1 = 42",
  1667. "const hook2 = 43",
  1668. "const hook3 = 44",
  1669. ]
  1670. assert list(GreatGrandchildComponent1.create()._get_all_hooks()) == [
  1671. "const hook1 = 42",
  1672. "const hook2 = 43",
  1673. "const hook3 = 44",
  1674. "const hook4 = 45",
  1675. ]
  1676. assert list(GrandchildComponent2.create()._get_all_hooks()) == ["const hook5 = 46"]
  1677. assert list(GreatGrandchildComponent2.create()._get_all_hooks()) == [
  1678. "const hook5 = 46",
  1679. "const hook2 = 43",
  1680. "const hook6 = 47",
  1681. ]
  1682. assert list(
  1683. BaseComponent.create(
  1684. GrandchildComponent1.create(GreatGrandchildComponent2.create()),
  1685. GreatGrandchildComponent1.create(),
  1686. )._get_all_hooks(),
  1687. ) == [
  1688. "const hook1 = 42",
  1689. "const hook2 = 43",
  1690. "const hook3 = 44",
  1691. "const hook5 = 46",
  1692. "const hook6 = 47",
  1693. "const hook4 = 45",
  1694. ]
  1695. assert list(
  1696. Fragment.create(
  1697. GreatGrandchildComponent2.create(),
  1698. GreatGrandchildComponent1.create(),
  1699. )._get_all_hooks()
  1700. ) == [
  1701. "const hook5 = 46",
  1702. "const hook2 = 43",
  1703. "const hook6 = 47",
  1704. "const hook1 = 42",
  1705. "const hook3 = 44",
  1706. "const hook4 = 45",
  1707. ]
  1708. def test_component_add_custom_code():
  1709. class BaseComponent(Component):
  1710. def _get_custom_code(self):
  1711. return "const custom_code1 = 42"
  1712. class ChildComponent1(BaseComponent):
  1713. pass
  1714. class GrandchildComponent1(ChildComponent1):
  1715. def add_custom_code(self):
  1716. return [
  1717. "const custom_code2 = 43",
  1718. "const custom_code3 = 44",
  1719. ]
  1720. class GreatGrandchildComponent1(GrandchildComponent1):
  1721. def add_custom_code(self):
  1722. return [
  1723. "const custom_code4 = 45",
  1724. ]
  1725. class GrandchildComponent2(ChildComponent1):
  1726. def _get_custom_code(self): # pyright: ignore [reportIncompatibleMethodOverride]
  1727. return "const custom_code5 = 46"
  1728. class GreatGrandchildComponent2(GrandchildComponent2):
  1729. def add_custom_code(self):
  1730. return [
  1731. "const custom_code2 = 43",
  1732. "const custom_code6 = 47",
  1733. ]
  1734. assert BaseComponent.create()._get_all_custom_code() == {"const custom_code1 = 42"}
  1735. assert ChildComponent1.create()._get_all_custom_code() == {
  1736. "const custom_code1 = 42"
  1737. }
  1738. assert GrandchildComponent1.create()._get_all_custom_code() == {
  1739. "const custom_code1 = 42",
  1740. "const custom_code2 = 43",
  1741. "const custom_code3 = 44",
  1742. }
  1743. assert GreatGrandchildComponent1.create()._get_all_custom_code() == {
  1744. "const custom_code1 = 42",
  1745. "const custom_code2 = 43",
  1746. "const custom_code3 = 44",
  1747. "const custom_code4 = 45",
  1748. }
  1749. assert GrandchildComponent2.create()._get_all_custom_code() == {
  1750. "const custom_code5 = 46"
  1751. }
  1752. assert GreatGrandchildComponent2.create()._get_all_custom_code() == {
  1753. "const custom_code2 = 43",
  1754. "const custom_code5 = 46",
  1755. "const custom_code6 = 47",
  1756. }
  1757. assert BaseComponent.create(
  1758. GrandchildComponent1.create(GreatGrandchildComponent2.create()),
  1759. GreatGrandchildComponent1.create(),
  1760. )._get_all_custom_code() == {
  1761. "const custom_code1 = 42",
  1762. "const custom_code2 = 43",
  1763. "const custom_code3 = 44",
  1764. "const custom_code4 = 45",
  1765. "const custom_code5 = 46",
  1766. "const custom_code6 = 47",
  1767. }
  1768. assert Fragment.create(
  1769. GreatGrandchildComponent2.create(),
  1770. GreatGrandchildComponent1.create(),
  1771. )._get_all_custom_code() == {
  1772. "const custom_code1 = 42",
  1773. "const custom_code2 = 43",
  1774. "const custom_code3 = 44",
  1775. "const custom_code4 = 45",
  1776. "const custom_code5 = 46",
  1777. "const custom_code6 = 47",
  1778. }
  1779. def test_component_add_hooks_var():
  1780. class HookComponent(Component):
  1781. def add_hooks(self):
  1782. return [
  1783. "const hook3 = useRef(null)",
  1784. "const hook1 = 42",
  1785. Var(
  1786. _js_expr="useEffect(() => () => {}, [])",
  1787. _var_data=VarData(
  1788. hooks={
  1789. "const hook2 = 43": None,
  1790. "const hook3 = useRef(null)": None,
  1791. },
  1792. imports={"react": [ImportVar(tag="useEffect")]},
  1793. ),
  1794. ),
  1795. Var(
  1796. _js_expr="const hook3 = useRef(null)",
  1797. _var_data=VarData(imports={"react": [ImportVar(tag="useRef")]}),
  1798. ),
  1799. ]
  1800. assert list(HookComponent.create()._get_all_hooks()) == [
  1801. "const hook3 = useRef(null)",
  1802. "const hook1 = 42",
  1803. "const hook2 = 43",
  1804. "useEffect(() => () => {}, [])",
  1805. ]
  1806. imports = HookComponent.create()._get_all_imports()
  1807. assert len(imports) == 1
  1808. assert "react" in imports
  1809. assert len(imports["react"]) == 2
  1810. assert ImportVar(tag="useRef") in imports["react"]
  1811. assert ImportVar(tag="useEffect") in imports["react"]
  1812. def test_add_style_embedded_vars(test_state: BaseState):
  1813. """Test that add_style works with embedded vars when returning a plain dict.
  1814. Args:
  1815. test_state: A test state.
  1816. """
  1817. v0 = LiteralVar.create("parent")._replace(
  1818. merge_var_data=VarData(hooks={"useParent": None}),
  1819. )
  1820. v1 = rx.color("plum", 10)
  1821. v2 = LiteralVar.create("text")._replace(
  1822. merge_var_data=VarData(hooks={"useText": None}),
  1823. )
  1824. class ParentComponent(Component):
  1825. def add_style(self):
  1826. return Style(
  1827. {
  1828. "fake_parent": v0,
  1829. }
  1830. )
  1831. class StyledComponent(ParentComponent):
  1832. tag = "StyledComponent"
  1833. def add_style(self): # pyright: ignore [reportIncompatibleMethodOverride]
  1834. return {
  1835. "color": v1,
  1836. "fake": v2,
  1837. "margin": f"{test_state.num}%",
  1838. }
  1839. page = rx.vstack(StyledComponent.create())
  1840. page._add_style_recursive(Style())
  1841. assert (
  1842. f"const {test_state.get_name()} = useContext(StateContexts.{test_state.get_name()})"
  1843. in page._get_all_hooks_internal()
  1844. )
  1845. assert "useText" in page._get_all_hooks_internal()
  1846. assert "useParent" in page._get_all_hooks_internal()
  1847. assert (
  1848. str(page).count(
  1849. f'css:({{ ["fakeParent"] : "parent", ["color"] : "var(--plum-10)", ["fake"] : "text", ["margin"] : ({test_state.get_name()}.num+"%") }})'
  1850. )
  1851. == 1
  1852. )
  1853. def test_add_style_foreach():
  1854. class StyledComponent(Component):
  1855. tag = "StyledComponent"
  1856. ix: Var[int]
  1857. def add_style(self):
  1858. return Style({"color": "red"})
  1859. page = rx.vstack(rx.foreach(Var.range(3), lambda i: StyledComponent.create(i)))
  1860. page._add_style_recursive(Style())
  1861. # Expect only a single child of the foreach on the python side
  1862. assert len(page.children[0].children) == 1
  1863. # Expect the style to be added to the child of the foreach
  1864. assert 'css:({ ["color"] : "red" })' in str(page.children[0].children[0])
  1865. # Expect only one instance of this CSS dict in the rendered page
  1866. assert str(page).count('css:({ ["color"] : "red" })') == 1
  1867. class TriggerState(rx.State):
  1868. """Test state with event handlers."""
  1869. @rx.event
  1870. def do_something(self):
  1871. """Sample event handler."""
  1872. pass
  1873. @pytest.mark.parametrize(
  1874. "component, output",
  1875. [
  1876. (rx.box(rx.text("random text")), False),
  1877. (
  1878. rx.box(rx.text("random text", on_click=rx.console_log("log"))),
  1879. False,
  1880. ),
  1881. (
  1882. rx.box(
  1883. rx.text("random text", on_click=TriggerState.do_something),
  1884. rx.text(
  1885. "random text",
  1886. on_click=Var(_js_expr="toggleColorMode").to(EventChain),
  1887. ),
  1888. ),
  1889. True,
  1890. ),
  1891. (
  1892. rx.box(
  1893. rx.text("random text", on_click=rx.console_log("log")),
  1894. rx.text(
  1895. "random text",
  1896. on_click=Var(_js_expr="toggleColorMode").to(EventChain),
  1897. ),
  1898. ),
  1899. False,
  1900. ),
  1901. (
  1902. rx.box(rx.text("random text", on_click=TriggerState.do_something)),
  1903. True,
  1904. ),
  1905. (
  1906. rx.box(
  1907. rx.text(
  1908. "random text",
  1909. on_click=[rx.console_log("log"), rx.window_alert("alert")],
  1910. ),
  1911. ),
  1912. False,
  1913. ),
  1914. (
  1915. rx.box(
  1916. rx.text(
  1917. "random text",
  1918. on_click=[rx.console_log("log"), TriggerState.do_something],
  1919. ),
  1920. ),
  1921. True,
  1922. ),
  1923. (
  1924. rx.box(
  1925. rx.text(
  1926. "random text",
  1927. on_blur=lambda: TriggerState.do_something,
  1928. ),
  1929. ),
  1930. True,
  1931. ),
  1932. ],
  1933. )
  1934. def test_has_state_event_triggers(component, output):
  1935. assert component._has_stateful_event_triggers() == output
  1936. class SpecialComponent(Box):
  1937. """A special component with custom attributes."""
  1938. data_prop: Var[str]
  1939. aria_prop: Var[str]
  1940. @pytest.mark.parametrize(
  1941. ("component_kwargs", "exp_custom_attrs", "exp_style"),
  1942. [
  1943. (
  1944. {"data_test": "test", "aria_test": "test"},
  1945. {"data-test": "test", "aria-test": "test"},
  1946. {},
  1947. ),
  1948. (
  1949. {"data-test": "test", "aria-test": "test"},
  1950. {"data-test": "test", "aria-test": "test"},
  1951. {},
  1952. ),
  1953. (
  1954. {"custom_attrs": {"data-existing": "test"}, "data_new": "test"},
  1955. {"data-existing": "test", "data-new": "test"},
  1956. {},
  1957. ),
  1958. (
  1959. {"data_test": "test", "data_prop": "prop"},
  1960. {"data-test": "test"},
  1961. {},
  1962. ),
  1963. (
  1964. {"aria_test": "test", "aria_prop": "prop"},
  1965. {"aria-test": "test"},
  1966. {},
  1967. ),
  1968. ],
  1969. )
  1970. def test_special_props(component_kwargs, exp_custom_attrs, exp_style):
  1971. """Test that data_ and aria_ special props are correctly added to the component.
  1972. Args:
  1973. component_kwargs: The component kwargs.
  1974. exp_custom_attrs: The expected custom attributes.
  1975. exp_style: The expected style.
  1976. """
  1977. component = SpecialComponent.create(**component_kwargs)
  1978. assert component.custom_attrs == exp_custom_attrs
  1979. assert component.style == exp_style
  1980. for prop in SpecialComponent.get_props():
  1981. if prop in component_kwargs:
  1982. assert getattr(component, prop)._var_value == component_kwargs[prop]
  1983. def test_ref():
  1984. """Test that the ref prop is correctly added to the component."""
  1985. custom_ref = Var("custom_ref")
  1986. ref_component = rx.box(ref=custom_ref)
  1987. assert ref_component._render().props["ref"].equals(custom_ref)
  1988. id_component = rx.box(id="custom_id")
  1989. assert id_component._render().props["ref"].equals(Var("ref_custom_id"))
  1990. assert "ref" not in rx.box()._render().props