Skip to content

Commit

Permalink
feat: test for error when key mutation is attempted
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Apr 25, 2024
1 parent cdd8ae8 commit be8f548
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions reacton/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3186,3 +3186,25 @@ def Test():
assert vbox.children[0].description == "1"
assert vbox.children[1].description == "2"
rc.close()


def test_key_mutate_and_reuse(Container):
button = ButtonComponentFunction(description="Hi")
set_state = lambda value: None # noqa

@react.component
def Test():
nonlocal set_state
state, set_state = react.use_state(0)
if state == 0:
return Container(children=[button])
else:
return Container(children=[button.key("i_should_not_be_mutated")]).key("bla")

box, rc = react.render(Test(), handle_error=False)
assert rc.find(widgets.Button).widget.description == "Hi"
assert button._key is None
with pytest.raises(RuntimeError, match="Element keys should not be mutated after rendering"):
set_state(1)
rc.render(w.HTML(value="recover").key("HTML"))
rc.close()

0 comments on commit be8f548

Please sign in to comment.