diff --git a/requirements/test-env.txt b/requirements/test-env.txt index aa007c295..ac59ea45b 100644 --- a/requirements/test-env.txt +++ b/requirements/test-env.txt @@ -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 diff --git a/src/idom/core/types.py b/src/idom/core/types.py index 697471d37..0fd78ec22 100644 --- a/src/idom/core/types.py +++ b/src/idom/core/types.py @@ -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[ diff --git a/tests/test_core/test_hooks.py b/tests/test_core/test_hooks.py index 73d5bf111..293e773a2 100644 --- a/tests/test_core/test_hooks.py +++ b/tests/test_core/test_hooks.py @@ -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(): @@ -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"]}], }, diff --git a/tests/test_core/test_layout.py b/tests/test_core/test_layout.py index 3814cd3b5..71013de9b 100644 --- a/tests/test_core/test_layout.py +++ b/tests/test_core/test_layout.py @@ -15,7 +15,6 @@ 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, @@ -23,6 +22,7 @@ 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 @@ -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(): diff --git a/tests/test_core/test_serve.py b/tests/test_core/test_serve.py index 9d715bfa5..20eb9fa86 100644 --- a/tests/test_core/test_serve.py +++ b/tests/test_core/test_serve.py @@ -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 = [] @@ -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": [ @@ -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 @@ -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() diff --git a/tests/tooling/common.py b/tests/tooling/common.py index c995eacde..e0706b561 100644 --- a/tests/tooling/common.py +++ b/tests/tooling/common.py @@ -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}