Skip to content

Commit 6938a6e

Browse files
committed
fix more tests
1 parent 65b5d65 commit 6938a6e

File tree

6 files changed

+41
-36
lines changed

6 files changed

+41
-36
lines changed

requirements/test-env.txt

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ playwright
1010

1111
# I'm not quite sure why this needs to be installed for tests with Sanic to pass
1212
sanic-testing
13+
14+
# Used to generate model changes from layout update messages
15+
jsonpointer

src/idom/core/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def __aexit__(
9898
VdomChild = Union[ComponentType, "VdomDict", str]
9999
"""A single child element of a :class:`VdomDict`"""
100100

101-
VdomChildren = Sequence[VdomChild]
101+
VdomChildren = "Sequence[VdomChild]"
102102
"""Describes a series of :class:`VdomChild` elements"""
103103

104104
VdomAttributesAndChildren = Union[

tests/test_core/test_hooks.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
current_hook,
1212
strictly_equal,
1313
)
14-
from idom.core.layout import Layout, LayoutUpdateMessage
14+
from idom.core.layout import Layout
1515
from idom.testing import DisplayFixture, HookCatcher, assert_idom_did_log, poll
1616
from idom.testing.logs import assert_idom_did_not_log
1717
from idom.utils import Ref
18-
from tests.tooling.common import DEFAULT_TYPE_DELAY
18+
from tests.tooling.common import DEFAULT_TYPE_DELAY, update_message
1919

2020

2121
async def test_must_be_rendering_in_layout_to_use_hooks():
@@ -42,30 +42,27 @@ def SimpleStatefulComponent():
4242

4343
async with idom.Layout(sse) as layout:
4444
update_1 = await layout.render()
45-
assert update_1 == LayoutUpdateMessage(
45+
assert update_1 == update_message(
4646
path="",
47-
old=None,
48-
new={
47+
model={
4948
"tagName": "",
5049
"children": [{"tagName": "div", "children": ["0"]}],
5150
},
5251
)
5352

5453
update_2 = await layout.render()
55-
assert update_2 == LayoutUpdateMessage(
54+
assert update_2 == update_message(
5655
path="",
57-
old=update_1.new,
58-
new={
56+
model={
5957
"tagName": "",
6058
"children": [{"tagName": "div", "children": ["1"]}],
6159
},
6260
)
6361

6462
update_3 = await layout.render()
65-
assert update_3 == LayoutUpdateMessage(
63+
assert update_3 == update_message(
6664
path="",
67-
old=update_2.new,
68-
new={
65+
model={
6966
"tagName": "",
7067
"children": [{"tagName": "div", "children": ["2"]}],
7168
},

tests/test_core/test_layout.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import gc
33
import random
44
import re
5-
from collections.abc import Sequence
6-
from typing import Any
75
from weakref import finalize
86
from weakref import ref as weakref
97

@@ -15,14 +13,14 @@
1513
from idom.core.component import component
1614
from idom.core.hooks import use_effect, use_state
1715
from idom.core.layout import Layout
18-
from idom.core.types import LayoutEventMessage, LayoutUpdateMessage
1916
from idom.testing import (
2017
HookCatcher,
2118
StaticEventHandler,
2219
assert_idom_did_log,
2320
capture_idom_logs,
2421
)
2522
from idom.utils import Ref
23+
from tests.tooling.common import event_message, update_message
2624
from tests.tooling.hooks import use_force_render, use_toggle
2725

2826

@@ -35,14 +33,6 @@ def no_logged_errors():
3533
raise record.exc_info[1]
3634

3735

38-
def event_message(target: str, *data: Any) -> LayoutEventMessage:
39-
return {"type": "layout-event", "target": target, "data": data}
40-
41-
42-
def update_message(path: str, model: Any) -> LayoutUpdateMessage:
43-
return {"type": "layout-update", "path": path, "model": model}
44-
45-
4636
def test_layout_repr():
4737
@idom.component
4838
def MyComponent():

tests/test_core/test_serve.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import asyncio
22
from typing import Any, Sequence
33

4+
from jsonpointer import set_pointer
5+
46
import idom
5-
from idom.core.layout import Layout, LayoutEventMessage, LayoutUpdateMessage
6-
from idom.core.serve import LayoutUpdateMessage, serve_layout
7+
from idom.core.layout import Layout
8+
from idom.core.serve import serve_layout
9+
from idom.core.types import LayoutUpdateMessage
710
from idom.testing import StaticEventHandler
11+
from tests.tooling.common import event_message
812

913

1014
EVENT_NAME = "onEvent"
1115
STATIC_EVENT_HANDLER = StaticEventHandler()
1216

1317

14-
def test_vdom_json_patch_create_from_apply_to():
15-
update = LayoutUpdateMessage("", {"a": 1, "b": [1]}, {"a": 2, "b": [1, 2]})
16-
patch = LayoutUpdateMessage.create_from(update)
17-
result = patch.apply_to({"a": 1, "b": [1]})
18-
assert result == {"a": 2, "b": [1, 2]}
19-
20-
2118
def make_send_recv_callbacks(events_to_inject):
2219
changes = []
2320

@@ -46,7 +43,7 @@ async def recv():
4643

4744

4845
def make_events_and_expected_model():
49-
events = [LayoutEventMessage(STATIC_EVENT_HANDLER.target, [])] * 4
46+
events = [event_message(STATIC_EVENT_HANDLER.target)] * 4
5047
expected_model = {
5148
"tagName": "",
5249
"children": [
@@ -72,7 +69,12 @@ def assert_changes_produce_expected_model(
7269
) -> None:
7370
model_from_changes = {}
7471
for update in changes:
75-
model_from_changes = update.apply_to(model_from_changes)
72+
if update["path"]:
73+
model_from_changes = set_pointer(
74+
model_from_changes, update["path"], update["model"]
75+
)
76+
else:
77+
model_from_changes.update(update["model"])
7678
assert model_from_changes == expected_model
7779

7880

@@ -128,8 +130,8 @@ async def handle_event():
128130
)
129131
)
130132

131-
await recv_queue.put(LayoutEventMessage(blocked_handler.target, []))
133+
await recv_queue.put(event_message(blocked_handler.target))
132134
await will_block.wait()
133135

134-
await recv_queue.put(LayoutEventMessage(non_blocked_handler.target, []))
136+
await recv_queue.put(event_message(non_blocked_handler.target))
135137
await second_event_did_execute.wait()

tests/tooling/common.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1+
from typing import Any
2+
3+
from idom.core.types import LayoutEventMessage, LayoutUpdateMessage
4+
5+
16
# see: https://github.com/microsoft/playwright-python/issues/1614
27
DEFAULT_TYPE_DELAY = 100 # miliseconds
8+
9+
10+
def event_message(target: str, *data: Any) -> LayoutEventMessage:
11+
return {"type": "layout-event", "target": target, "data": data}
12+
13+
14+
def update_message(path: str, model: Any) -> LayoutUpdateMessage:
15+
return {"type": "layout-update", "path": path, "model": model}

0 commit comments

Comments
 (0)