Added Hooks implementation

This commit is contained in:
2025-08-28 23:24:28 +02:00
parent eb8d6a99a2
commit 292a477298
5 changed files with 1152 additions and 74 deletions

View File

@@ -1,6 +1,7 @@
import pytest
from components.jsonviewer.components.JsonViewer import *
from components.jsonviewer.hooks import HookBuilder
from helpers import matches, span_icon, search_elements_by_name, extract_jsonviewer_node
JSON_VIEWER_INSTANCE_ID = "json_viewer"
@@ -296,30 +297,39 @@ def test_toggle_between_folding_modes(session):
def test_custom_hook_rendering(session, helper):
# Define a custom hook for testing
def custom_predicate(key, node, h):
return isinstance(node.value, str) and node.value == "custom_hook_test"
def custom_renderer(key, node, h):
return Span("CUSTOM_HOOK_RENDER", cls="custom-hook-class")
hooks = [(custom_predicate, custom_renderer)]
# Create JsonViewer with the custom hook
jsonv = JsonViewer(session, JSON_VIEWER_INSTANCE_ID, "custom_hook_test", hooks=hooks)
actual = jsonv.__ft__()
to_compare = search_elements_by_name(actual, "div", attrs={"id": f"{jv_id('root')}"})[0]
expected = Div(
Div(
None,
None,
Span("CUSTOM_HOOK_RENDER", cls="custom-hook-class"),
style=ML_20),
id=f"{jv_id('root')}")
assert matches(to_compare, expected)
# Define a custom condition to check if the value is "custom_hook_test"
def custom_condition(context):
return isinstance(context.node.value, str) and context.node.value == "custom_hook_test"
# Define a custom executor to render the desired output
def custom_renderer(context):
return Span("CUSTOM_HOOK_RENDER", cls="custom-hook-class")
# Build the hook using HookBuilder
hook = (HookBuilder()
.on_render()
.when_custom(custom_condition)
.execute(custom_renderer))
# Create a JsonViewer with the new hook
jsonv = JsonViewer(session, JSON_VIEWER_INSTANCE_ID, "custom_hook_test", hooks=[hook])
# Actual rendered output
actual = jsonv.__ft__()
to_compare = search_elements_by_name(actual, "div", attrs={"id": f"{jv_id('root')}"})[0]
# Expected rendered output
expected = Div(
Div(
None,
None,
Span("CUSTOM_HOOK_RENDER", cls="custom-hook-class"),
style=ML_20),
id=f"{jv_id('root')}"
)
# Assert that the actual output matches the expected output
assert matches(to_compare, expected)
def test_folding_mode_operations(session):
@@ -366,4 +376,4 @@ def test_helper_is_sha256(helper):
assert not helper.is_sha256("a" * 63) # Too short
assert not helper.is_sha256("a" * 65) # Too long
assert not helper.is_sha256("g" * 64) # Invalid character
assert not helper.is_sha256("test") # Not a hash
assert not helper.is_sha256("test") # Not a hash