|
1 | | -"""Core tests module.""" |
| 1 | +"""Tests for ReactFlow model creation.""" |
2 | 2 |
|
3 | | -import panel_reactflow # noqa |
| 3 | +from panel.pane import Markdown |
| 4 | +from panel.viewable import Viewer |
4 | 5 |
|
| 6 | +from panel_reactflow import ReactFlow |
5 | 7 |
|
6 | | -def test_example(): |
7 | | - """Test example.""" |
8 | | - assert panel_reactflow |
| 8 | + |
| 9 | +def test_reactflow_add_node_with_arbitrary_object(document, comm) -> None: |
| 10 | + """Test that arbitrary objects (e.g., HoloViews) work as node views via pn.panel(). |
| 11 | +
|
| 12 | + This addresses issue #13 where objects without __panel__() method |
| 13 | + (like HoloViews Curve objects) would raise AttributeError. |
| 14 | + """ |
| 15 | + |
| 16 | + class MockPlot: |
| 17 | + """Mock object simulating HoloViews/hvplot objects (no __panel__ method).""" |
| 18 | + |
| 19 | + def __repr__(self): |
| 20 | + return "MockPlot(data)" |
| 21 | + |
| 22 | + flow = ReactFlow() |
| 23 | + mock_plot = MockPlot() |
| 24 | + flow.add_node({"id": "n1", "position": {"x": 0, "y": 0}, "label": "Plot Node", "data": {}, "view": mock_plot}) |
| 25 | + |
| 26 | + # This should not raise AttributeError about '_models' |
| 27 | + # The object should be converted via pn.panel() |
| 28 | + _ = flow.get_root(document, comm=comm) |
| 29 | + assert len(flow.nodes) == 1 |
| 30 | + assert flow.nodes[0]["id"] == "n1" |
| 31 | + |
| 32 | + |
| 33 | +def test_view_idx_updates_on_remove_node(document, comm) -> None: |
| 34 | + flow = ReactFlow() |
| 35 | + flow.add_node({"id": "n1", "position": {"x": 0, "y": 0}, "data": {}, "view": Markdown("A")}) |
| 36 | + flow.add_node({"id": "n2", "position": {"x": 1, "y": 1}, "data": {}, "view": Markdown("B")}) |
| 37 | + flow.add_node({"id": "n3", "position": {"x": 2, "y": 2}, "data": {}}) |
| 38 | + |
| 39 | + model = flow.get_root(document, comm=comm) |
| 40 | + |
| 41 | + flow.remove_node("n1") |
| 42 | + |
| 43 | + remaining = {node["id"]: node for node in model.data.nodes} |
| 44 | + assert remaining["n2"]["data"]["view_idx"] == 0 |
| 45 | + assert remaining["n3"]["data"].get("view_idx") is None |
| 46 | + |
| 47 | + |
| 48 | +def test_reactflow_add_node_with_viewer(document, comm) -> None: |
| 49 | + """Test that Viewer objects with __panel__() method work as node views.""" |
| 50 | + |
| 51 | + class MyViewer(Viewer): |
| 52 | + def __panel__(self): |
| 53 | + return Markdown("Hello from Viewer!") |
| 54 | + |
| 55 | + flow = ReactFlow() |
| 56 | + my_viewer = MyViewer() |
| 57 | + flow.add_node({"id": "n1", "position": {"x": 0, "y": 0}, "label": "Viewer Node", "data": {}, "view": my_viewer}) |
| 58 | + |
| 59 | + # This should not raise AttributeError about '_models' |
| 60 | + _ = flow.get_root(document, comm=comm) |
| 61 | + assert len(flow.nodes) == 1 |
| 62 | + assert flow.nodes[0]["id"] == "n1" |
| 63 | + |
| 64 | + |
| 65 | +def test_reactflow_add_node_dynamically_creates_views(document, comm): |
| 66 | + flow = ReactFlow() |
| 67 | + model = flow.get_root(document, comm=comm) |
| 68 | + assert model.children == [ |
| 69 | + "_views", |
| 70 | + "_node_editor_views", |
| 71 | + "_edge_editor_views", |
| 72 | + "top_panel", |
| 73 | + "bottom_panel", |
| 74 | + "left_panel", |
| 75 | + "right_panel", |
| 76 | + ] |
| 77 | + |
| 78 | + flow.add_node({"id": "n1", "position": {"x": 0, "y": 0}, "label": "Viewer Node", "data": {}, "view": Markdown("foo")}) |
| 79 | + |
| 80 | + assert len(model.data._views) == 1 |
| 81 | + assert len(model.data._node_editor_views) == 1 |
0 commit comments