Skip to content

Commit 3047ae1

Browse files
committed
Add tests for field attributes in javascript_exclude.
1 parent 3e1fa4b commit 3047ae1

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

django_unicorn/components/unicorn_view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def get_frontend_context_variables(self) -> str:
335335
attributes = self._attributes()
336336
frontend_context_variables.update(attributes)
337337

338-
exclude_field_attributes = ()
338+
exclude_field_attributes = []
339339

340340
# Remove any field in `javascript_exclude` from `frontend_context_variables`
341341
if hasattr(self, "Meta") and hasattr(self.Meta, "javascript_exclude"):
@@ -374,7 +374,7 @@ def get_frontend_context_variables(self) -> str:
374374

375375
encoded_frontend_context_variables = serializer.dumps(
376376
frontend_context_variables,
377-
exclude_field_attributes=exclude_field_attributes,
377+
exclude_field_attributes=tuple(exclude_field_attributes),
378378
)
379379

380380
return encoded_frontend_context_variables

tests/components/test_component.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,45 @@ class Meta:
115115
assert "name" in component.get_context_data()
116116

117117

118+
def test_meta_javascript_exclude_nested_with_tuple():
119+
class TestComponent(UnicornView):
120+
name = {"Universe": {"World": "Earth"}}
121+
122+
class Meta:
123+
javascript_exclude = ("name.Universe",)
124+
125+
expected = '{"name":{}}'
126+
component = TestComponent(component_id="asdf1234", component_name="hello-world")
127+
assert expected == component.get_frontend_context_variables()
128+
129+
130+
def test_meta_javascript_exclude_nested_multiple_with_spaces():
131+
class TestComponent(UnicornView):
132+
name = {"Universe": {"World": "Earth"}}
133+
another = {"Neutral Milk Hotel": {"album": {"On Avery Island": 1996}}}
134+
135+
class Meta:
136+
javascript_exclude = ("name.Universe", "another.Neutral Milk Hotel.album")
137+
138+
expected = '{"another":{"Neutral Milk Hotel":{}},"name":{}}'
139+
component = TestComponent(component_id="asdf1234", component_name="hello-world")
140+
assert expected == component.get_frontend_context_variables()
141+
142+
143+
def test_meta_javascript_exclude_nested_with_list():
144+
class TestComponent(UnicornView):
145+
name = {"Universe": {"World": "Earth"}}
146+
147+
class Meta:
148+
javascript_exclude = [
149+
"name.Universe",
150+
]
151+
152+
expected = '{"name":{}}'
153+
component = TestComponent(component_id="asdf1234", component_name="hello-world")
154+
assert expected == component.get_frontend_context_variables()
155+
156+
118157
def test_meta_exclude():
119158
class TestComponent(UnicornView):
120159
name = "World"

0 commit comments

Comments
 (0)