Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Jan 24, 2023
1 parent 65b5d65 commit 60f8d5a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
3 changes: 3 additions & 0 deletions requirements/test-env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ playwright

# I'm not quite sure why this needs to be installed for tests with Sanic to pass
sanic-testing

# Used to generate model changes from layout update messages
jsonpointer
2 changes: 1 addition & 1 deletion src/idom/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def __aexit__(
VdomChild = Union[ComponentType, "VdomDict", str]
"""A single child element of a :class:`VdomDict`"""

VdomChildren = Sequence[VdomChild]
VdomChildren = "Sequence[VdomChild]"
"""Describes a series of :class:`VdomChild` elements"""

VdomAttributesAndChildren = Union[
Expand Down
19 changes: 8 additions & 11 deletions tests/test_core/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
current_hook,
strictly_equal,
)
from idom.core.layout import Layout, LayoutUpdateMessage
from idom.core.layout import Layout
from idom.testing import DisplayFixture, HookCatcher, assert_idom_did_log, poll
from idom.testing.logs import assert_idom_did_not_log
from idom.utils import Ref
from tests.tooling.common import DEFAULT_TYPE_DELAY
from tests.tooling.common import DEFAULT_TYPE_DELAY, update_message


async def test_must_be_rendering_in_layout_to_use_hooks():
Expand All @@ -42,30 +42,27 @@ def SimpleStatefulComponent():

async with idom.Layout(sse) as layout:
update_1 = await layout.render()
assert update_1 == LayoutUpdateMessage(
assert update_1 == update_message(
path="",
old=None,
new={
model={
"tagName": "",
"children": [{"tagName": "div", "children": ["0"]}],
},
)

update_2 = await layout.render()
assert update_2 == LayoutUpdateMessage(
assert update_2 == update_message(
path="",
old=update_1.new,
new={
model={
"tagName": "",
"children": [{"tagName": "div", "children": ["1"]}],
},
)

update_3 = await layout.render()
assert update_3 == LayoutUpdateMessage(
assert update_3 == update_message(
path="",
old=update_2.new,
new={
model={
"tagName": "",
"children": [{"tagName": "div", "children": ["2"]}],
},
Expand Down
10 changes: 1 addition & 9 deletions tests/test_core/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
from idom.core.component import component
from idom.core.hooks import use_effect, use_state
from idom.core.layout import Layout
from idom.core.types import LayoutEventMessage, LayoutUpdateMessage
from idom.testing import (
HookCatcher,
StaticEventHandler,
assert_idom_did_log,
capture_idom_logs,
)
from idom.utils import Ref
from tests.tooling.common import event_message, update_message
from tests.tooling.hooks import use_force_render, use_toggle


Expand All @@ -35,14 +35,6 @@ def no_logged_errors():
raise record.exc_info[1]


def event_message(target: str, *data: Any) -> LayoutEventMessage:
return {"type": "layout-event", "target": target, "data": data}


def update_message(path: str, model: Any) -> LayoutUpdateMessage:
return {"type": "layout-update", "path": path, "model": model}


def test_layout_repr():
@idom.component
def MyComponent():
Expand Down
28 changes: 15 additions & 13 deletions tests/test_core/test_serve.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import asyncio
from typing import Any, Sequence

from jsonpointer import set_pointer

import idom
from idom.core.layout import Layout, LayoutEventMessage, LayoutUpdateMessage
from idom.core.serve import LayoutUpdateMessage, serve_layout
from idom.core.layout import Layout
from idom.core.serve import serve_layout
from idom.core.types import LayoutUpdateMessage
from idom.testing import StaticEventHandler
from tests.tooling.common import event_message


EVENT_NAME = "onEvent"
STATIC_EVENT_HANDLER = StaticEventHandler()


def test_vdom_json_patch_create_from_apply_to():
update = LayoutUpdateMessage("", {"a": 1, "b": [1]}, {"a": 2, "b": [1, 2]})
patch = LayoutUpdateMessage.create_from(update)
result = patch.apply_to({"a": 1, "b": [1]})
assert result == {"a": 2, "b": [1, 2]}


def make_send_recv_callbacks(events_to_inject):
changes = []

Expand Down Expand Up @@ -46,7 +43,7 @@ async def recv():


def make_events_and_expected_model():
events = [LayoutEventMessage(STATIC_EVENT_HANDLER.target, [])] * 4
events = [event_message(STATIC_EVENT_HANDLER.target)] * 4
expected_model = {
"tagName": "",
"children": [
Expand All @@ -72,7 +69,12 @@ def assert_changes_produce_expected_model(
) -> None:
model_from_changes = {}
for update in changes:
model_from_changes = update.apply_to(model_from_changes)
if update["path"]:
model_from_changes = set_pointer(
model_from_changes, update["path"], update["model"]
)
else:
model_from_changes.update(update["model"])
assert model_from_changes == expected_model


Expand Down Expand Up @@ -128,8 +130,8 @@ async def handle_event():
)
)

await recv_queue.put(LayoutEventMessage(blocked_handler.target, []))
await recv_queue.put(event_message(blocked_handler.target))
await will_block.wait()

await recv_queue.put(LayoutEventMessage(non_blocked_handler.target, []))
await recv_queue.put(event_message(non_blocked_handler.target))
await second_event_did_execute.wait()
13 changes: 13 additions & 0 deletions tests/tooling/common.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
from typing import Any

from idom.core.types import LayoutEventMessage, LayoutUpdateMessage


# see: https://github.com/microsoft/playwright-python/issues/1614
DEFAULT_TYPE_DELAY = 100 # miliseconds


def event_message(target: str, *data: Any) -> LayoutEventMessage:
return {"type": "layout-event", "target": target, "data": data}


def update_message(path: str, model: Any) -> LayoutUpdateMessage:
return {"type": "layout-update", "path": path, "model": model}

0 comments on commit 60f8d5a

Please sign in to comment.