add test for default factory with rx.foreach (#4515)

This commit is contained in:
benedikt-bartscher 2024-12-12 03:22:59 +01:00 committed by GitHub
parent ce364e0bce
commit b4722c4f8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,8 +1,10 @@
from typing import Dict, List, Set, Tuple, Union from typing import Dict, List, Set, Tuple, Union
import pydantic.v1
import pytest import pytest
from reflex import el from reflex import el
from reflex.base import Base
from reflex.components.component import Component from reflex.components.component import Component
from reflex.components.core.foreach import ( from reflex.components.core.foreach import (
Foreach, Foreach,
@ -18,6 +20,12 @@ from reflex.vars.number import NumberVar
from reflex.vars.sequence import ArrayVar from reflex.vars.sequence import ArrayVar
class ForEachTag(Base):
"""A tag for testing the ForEach component."""
name: str = ""
class ForEachState(BaseState): class ForEachState(BaseState):
"""A state for testing the ForEach component.""" """A state for testing the ForEach component."""
@ -46,6 +54,8 @@ class ForEachState(BaseState):
bad_annotation_list: list = [["red", "orange"], ["yellow", "blue"]] bad_annotation_list: list = [["red", "orange"], ["yellow", "blue"]]
color_index_tuple: Tuple[int, str] = (0, "red") color_index_tuple: Tuple[int, str] = (0, "red")
default_factory_list: list[ForEachTag] = pydantic.v1.Field(default_factory=list)
class ComponentStateTest(ComponentState): class ComponentStateTest(ComponentState):
"""A test component state.""" """A test component state."""
@ -290,3 +300,11 @@ def test_foreach_component_state():
ForEachState.colors_list, ForEachState.colors_list,
ComponentStateTest.create, ComponentStateTest.create,
) )
def test_foreach_default_factory():
"""Test that the default factory is called."""
_ = Foreach.create(
ForEachState.default_factory_list,
lambda tag: text(tag.name),
)