Skip to content

Commit 4b79f48

Browse files
committed
Clear extra_context so that the component can be cached correctly.
1 parent 4c13834 commit 4b79f48

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

django_unicorn/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def get_cacheable_component(
6565

6666
component.request = None
6767

68+
if component.extra_context:
69+
component.extra_context = None
70+
6871
if component.parent:
6972
component.parent = get_cacheable_component(component.parent)
7073

tests/test_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from unittest.mock import MagicMock
2+
3+
from django_unicorn.components import UnicornView
14
from django_unicorn.utils import (
25
generate_checksum,
6+
get_cacheable_component,
37
get_method_arguments,
48
get_type_hints,
59
sanitize_html,
@@ -69,3 +73,27 @@ def test_sanitize_html():
6973
data = '{"id":"abcd123","name":"text-inputs","key":"asdf","data":{"name":"World","testing_thing":"Whatever </script> <script>alert(\'uh oh\')</script>"},"calls":[],"hash":"hjkl"}'
7074
actual = sanitize_html(data)
7175
assert actual == expected
76+
77+
78+
class FakeComponent(UnicornView):
79+
pass
80+
81+
82+
def test_get_cacheable_component_request_is_none():
83+
component = FakeComponent(component_id="asdf123498", component_name="hello-world")
84+
component.request = MagicMock()
85+
assert component.request
86+
87+
actual = get_cacheable_component(component)
88+
89+
assert actual.request is None
90+
91+
92+
def test_get_cacheable_component_extra_context_is_none():
93+
component = FakeComponent(component_id="asdf123499", component_name="hello-world")
94+
component.extra_context = MagicMock()
95+
assert component.extra_context
96+
97+
actual = get_cacheable_component(component)
98+
99+
assert actual.extra_context is None

0 commit comments

Comments
 (0)