|
| 1 | +import pytest |
| 2 | + |
1 | 3 | import idom |
| 4 | +from idom.core.layout import RenderError, LayoutUpdate |
2 | 5 |
|
3 | 6 | from .utils import RenderHistory |
4 | 7 |
|
5 | 8 |
|
| 9 | +def test_layout_expects_abstract_element(): |
| 10 | + with pytest.raises(TypeError, match="Expected an AbstractElement"): |
| 11 | + idom.Layout(None) |
| 12 | + with pytest.raises(TypeError, match="Expected an AbstractElement"): |
| 13 | + idom.Layout(idom.html.div()) |
| 14 | + |
| 15 | + |
| 16 | +async def test_layout_has_event_loop(event_loop): |
| 17 | + @idom.element |
| 18 | + async def my_element(self): |
| 19 | + ... |
| 20 | + |
| 21 | + layout = idom.Layout(my_element()) |
| 22 | + assert layout.loop is event_loop |
| 23 | + |
| 24 | + |
6 | 25 | async def test_simple_layout(): |
7 | 26 | @idom.element |
8 | 27 | async def simple_element(self, tag): |
@@ -65,3 +84,96 @@ async def child_element(self): |
65 | 84 | history.child_2.id: {"tagName": "div"}, |
66 | 85 | } |
67 | 86 | assert old == [history.child_1.id] |
| 87 | + |
| 88 | + |
| 89 | +async def test_layout_render_error_has_partial_update(): |
| 90 | + history = RenderHistory() |
| 91 | + |
| 92 | + @history.track("main") |
| 93 | + @idom.element |
| 94 | + async def main(self): |
| 95 | + return idom.html.div([ok_child(), bad_child()]) |
| 96 | + |
| 97 | + @history.track("ok_child") |
| 98 | + @idom.element |
| 99 | + async def ok_child(self): |
| 100 | + return idom.html.div(["hello"]) |
| 101 | + |
| 102 | + @idom.element |
| 103 | + async def bad_child(self): |
| 104 | + raise ValueError("Something went wrong :(") |
| 105 | + |
| 106 | + layout = idom.Layout(main()) |
| 107 | + |
| 108 | + try: |
| 109 | + await layout.render() |
| 110 | + except RenderError as e: |
| 111 | + error = e |
| 112 | + |
| 113 | + assert error.partial_render == LayoutUpdate( |
| 114 | + src=history.main_1.id, |
| 115 | + new={ |
| 116 | + history.ok_child_1.id: { |
| 117 | + "tagName": "div", |
| 118 | + "children": [{"type": "str", "data": "hello"}], |
| 119 | + } |
| 120 | + }, |
| 121 | + old=[], |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +async def test_render_raw_vdom_dict_with_single_element_object_as_children(): |
| 126 | + history = RenderHistory() |
| 127 | + |
| 128 | + @history.track("main") |
| 129 | + @idom.element |
| 130 | + async def main(self): |
| 131 | + return {"tagName": "div", "children": child()} |
| 132 | + |
| 133 | + @history.track("child") |
| 134 | + @idom.element |
| 135 | + async def child(self): |
| 136 | + return {"tagName": "div", "children": {"tagName": "h1"}} |
| 137 | + |
| 138 | + render = await idom.Layout(main()).render() |
| 139 | + |
| 140 | + assert render == LayoutUpdate( |
| 141 | + src=history.main_1.id, |
| 142 | + new={ |
| 143 | + history.child_1.id: { |
| 144 | + "tagName": "div", |
| 145 | + "children": [{"type": "obj", "data": {"tagName": "h1"}}], |
| 146 | + }, |
| 147 | + history.main_1.id: { |
| 148 | + "tagName": "div", |
| 149 | + "children": [{"type": "ref", "data": history.child_1.id}], |
| 150 | + }, |
| 151 | + }, |
| 152 | + old=[], |
| 153 | + ) |
| 154 | + |
| 155 | + |
| 156 | +async def test_element_parents_must_exist_unless_is_root(): |
| 157 | + history = RenderHistory() |
| 158 | + |
| 159 | + @history.track("main") |
| 160 | + @idom.element |
| 161 | + async def main(self): |
| 162 | + return child() |
| 163 | + |
| 164 | + @history.track("child") |
| 165 | + @idom.element |
| 166 | + async def child(self): |
| 167 | + return idom.html.div() |
| 168 | + |
| 169 | + layout = idom.Layout(main()) |
| 170 | + await layout.render() |
| 171 | + |
| 172 | + assert layout._element_parent(history.main_1) is None |
| 173 | + |
| 174 | + @idom.element |
| 175 | + async def element_not_in_layout(self): |
| 176 | + ... |
| 177 | + |
| 178 | + with pytest.raises(KeyError): |
| 179 | + layout._element_parent(element_not_in_layout()) |
0 commit comments