test_component.py 67 KB

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