Logic for removing the 'None' property along with its corresponding test case. (#2969)

This commit is contained in:
Aman Salwan 2024-04-03 00:07:05 +05:30 committed by GitHub
parent 9504700777
commit 0e221f0984
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 1 deletions

View File

@ -645,6 +645,9 @@ class Component(BaseComponent, ABC):
) )
props[prop] = props.pop(under_prop) props[prop] = props.pop(under_prop)
# Filter out None props
props = {key: value for key, value in props.items() if value is not None}
# Validate all the children. # Validate all the children.
for child in children: for child in children:
# Make sure the child is a valid type. # Make sure the child is a valid type.

View File

@ -363,7 +363,7 @@ def test_valid_props(component1, text: str, number: int):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"text,number", [("", "bad_string"), (13, 1), (None, 1), ("test", [1, 2, 3])] "text,number", [("", "bad_string"), (13, 1), ("test", [1, 2, 3])]
) )
def test_invalid_prop_type(component1, text: str, number: int): def test_invalid_prop_type(component1, text: str, number: int):
"""Test that an invalid prop type raises an error. """Test that an invalid prop type raises an error.
@ -420,6 +420,43 @@ def test_get_event_triggers(component1, component2):
) )
@pytest.fixture
def test_component() -> Type[Component]:
"""A test component.
Returns:
A test component.
"""
class TestComponent(Component):
pass
return TestComponent
# Write a test case to check if the create method filters out None props
def test_create_filters_none_props(test_component):
child1 = test_component()
child2 = test_component()
props = {
"prop1": "value1",
"prop2": None,
"prop3": "value3",
"prop4": None,
"style": {"color": "white", "text-align": "center"}, # Adding a style prop
}
component = test_component.create(child1, child2, **props)
# Assert that None props are not present in the component's props
assert "prop2" not in component.get_props()
assert "prop4" not in component.get_props()
# Assert that the style prop is present in the component's props
assert component.style["color"] == "white"
assert component.style["text-align"] == "center"
class C1State(BaseState): class C1State(BaseState):
"""State for testing C1 component.""" """State for testing C1 component."""