def test_none(renderer): def none(): return None renderer.render(Element(none)) assert renderer.steps == [] assert renderer.item is None
def component(use_state): visible, set_visible = use_state(True) if not visible: return None return Element( Button, pressed=lambda: set_visible(False), )
def test_state_layout(renderer): def component(use_state): visible, set_visible = use_state(True) if not visible: return None return Element( Button, pressed=lambda: set_visible(False), ) renderer.render(Element( VLayout, widgets=[Element(component)], )) assert renderer.item == VLayout(widgets=[Button()]) assert renderer.root.props['widgets'][0].state[0][0] == True renderer.item.widgets[0].pressed() assert renderer.root.props['widgets'][0].state[0][0] == False assert renderer.item == VLayout(widgets=[], )
def test_update(renderer): renderer.render(Element( simple, text="hej", )) renderer.render(Element( simple, text="hej2", )) assert renderer.steps == [ ("add", Label), ("update", { "text": "Very nice hej" }), ("update", { "text": "Very nice hej2" }), ] assert renderer.item == Label(text="Very nice hej2")
def test_create(renderer): renderer.render(Element( Label, text='hello', )) assert renderer.steps == [ ("add", Label), ("update", { "text": "hello" }), ] assert renderer.item == Label(text="hello")
def test_change_class(renderer): renderer.render(Element( Label, text='hello', )) assert renderer.item == Label(text="hello") renderer.render(Element( Text, text='hello2', )) assert renderer.steps == [ ("add", Label), ("update", { "text": "hello" }), ("remove", Label), ("add", Text), ("update", { "text": "hello2" }), ] assert renderer.item == Text(text="hello2")
def test_change_prop(renderer): renderer.render(Element( Label, text='hello', )) assert renderer.item == Label(text="hello") label = renderer.item renderer.render(Element( Label, text='hello2', )) assert renderer.steps == [ ("add", Label), ("update", { "text": "hello" }), ("update", { "text": "hello2" }), ] assert renderer.item == Label(text="hello2") assert renderer.item is label
def test_remove(renderer): renderer.render(Element( Label, text='hello', )) renderer.render(None) assert renderer.steps == [ ("add", Label), ("update", { "text": "hello" }), ("remove", Label), ] assert renderer.item is None
def test_effect(renderer): vars = [] vars2 = [] def add_var(var): vars.append(var) def cleanup(): vars2.append(var) return cleanup iter_vars = [] iter_vars2 = [] def add_var_iterator(var): iter_vars.append(var) yield iter_vars2.append(var) list_of_things = [] def component(use_effect, use_state, var): use_effect(add_var, int(var)) use_effect(add_var_iterator, int(var)) return None renderer.render(Element(component, var=1.0)) renderer.render(Element(component, var=1.2)) renderer.render(Element(component, var=2.0)) renderer.render(Element(component, var=1.0)) assert vars == [1, 2, 1] assert vars2 == [1, 2] assert iter_vars == [1, 2, 1] assert iter_vars2 == [1, 2]
def test_state_root(renderer): def component(use_state): visible, set_visible = use_state(True) if not visible: return None return Element( Button, pressed=lambda: set_visible(False), ) renderer.render(Element(component)) assert isinstance(renderer.item, Button) assert renderer.item == Button() renderer.root.state[0][0] = True renderer.item.pressed() renderer.root.state[0][0] = False assert renderer.item is None
def test_widgets_list(renderer): renderer.render( Element(VLayout, widgets=[[Element(Label, key="a")], [ Element(Label, key="a"), Element(Label, key="b"), ]])) assert renderer.steps == [ ("add", VLayout), ("add", Label), ("add", Label), ("add", Label), ] assert renderer.item == VLayout(widgets=[ Label(), Label(), Label(), ]) label_a = renderer.item.widgets[1] label_b = renderer.item.widgets[2] renderer.render( Element(VLayout, widgets=[[], [ Element(Label, key="b"), Element(Label, key="a", text="majs"), ]])) assert renderer.steps[4:] == [ ("remove", Label), ("update", { "text": "majs" }), ] assert renderer.item == VLayout(widgets=[ Label(), Label(text="majs"), ]) assert renderer.item.widgets[0] is label_b assert renderer.item.widgets[1] is label_a
def main(): app = QApplication(sys.argv) renderer = QtRenderer() renderer.render(Element(master)) app.exec_()
def test_layout(renderer): renderer.render( Element(Widget, layout=Element(VLayout, widgets=[ Element(Label, text="hello"), Element(Label, text="hello2"), ]))) assert renderer.steps == [ ("add", Widget), ("add", VLayout), ("add", Label), ("update", { "text": "hello" }), ("add", Label), ("update", { "text": "hello2" }), ] assert renderer.item == Widget(layout=VLayout(widgets=[ Label(text="hello"), Label(text="hello2"), ])) widgets = renderer.item.layout.widgets[:] renderer.render( Element(Widget, layout=Element(VLayout, widgets=[ Element(Label, text="hello2"), Element(Label, text="hello3"), ]))) assert renderer.steps[6:] == [ ("update", { "text": "hello2" }), ("update", { "text": "hello3" }), ] assert renderer.item == Widget(layout=VLayout(widgets=[ Label(text="hello2"), Label(text="hello3"), ])) assert renderer.item.layout.widgets == widgets renderer.render( Element(Widget, layout=Element(VLayout, widgets=[ None, Element(Label, text="hello3"), ]))) assert renderer.steps[8:] == [ ("remove", Label), ] assert renderer.item == Widget(layout=VLayout(widgets=[ Label(text="hello3"), ])) assert renderer.item.layout.widgets == [widgets[1]]
def simple(text=None): return Element( Label, text="Very nice " + text, )