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 56e37b7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions reacton/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3186,3 +3186,24 @@ 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 state: None

@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 == 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 56e37b7

Please sign in to comment.