Skip to content

Commit 1744fdc

Browse files
committed
Add support for args in component.call().
1 parent d6438fb commit 1744fdc

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

django_unicorn/components/unicorn_view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ def reset(self):
216216
)
217217
pass
218218

219-
def call(self, function_name):
219+
def call(self, function_name, *args):
220220
"""
221221
Add a JavaScript method name and arguments to be called after the component is rendered.
222222
"""
223-
self.calls.append({"fn": function_name})
223+
self.calls.append({"fn": function_name, "args": args})
224224

225225
def mount(self):
226226
"""

django_unicorn/static/js/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class Component {
125125
calls = calls || [];
126126

127127
calls.forEach((call) => {
128-
this.window[call.fn]();
128+
this.window[call.fn](...call.args);
129129
});
130130
}
131131

example/unicorn/components/js.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def mount(self):
77
pass
88

99
def call_javascript(self):
10-
self.call("callAlert")
10+
self.call("callAlert", "world")
1111

1212
def choose_state():
1313
pass

example/unicorn/templates/unicorn/js.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div>
22
<script>
3-
function callAlert() {
4-
alert("hello");
3+
function callAlert(name) {
4+
alert("hello, " + name);
55
}
66
</script>
77

tests/templatetags/test_unicorn_render.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def mount(self):
3636
self.call("testCall")
3737

3838

39+
class FakeComponentCalls2(UnicornView):
40+
template_name = "templates/test_component_parent.html"
41+
42+
def mount(self):
43+
self.call("testCall2", "hello")
44+
45+
3946
def test_unicorn_render_kwarg():
4047
token = Token(
4148
TokenType.TEXT,
@@ -240,7 +247,22 @@ def test_unicorn_render_calls(settings):
240247

241248
assert "<script" in html
242249
assert len(re.findall("<script", html)) == 1
243-
assert '"calls":[{"fn":"testCall"}]});' in html
250+
assert '"calls":[{"fn":"testCall","args":[]}]});' in html
251+
252+
253+
def test_unicorn_render_calls_with_arg(settings):
254+
settings.DEBUG = True
255+
token = Token(
256+
TokenType.TEXT,
257+
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentCalls2'",
258+
)
259+
unicorn_node = unicorn(None, token)
260+
context = {}
261+
html = unicorn_node.render(context)
262+
263+
assert "<script" in html
264+
assert len(re.findall("<script", html)) == 1
265+
assert '"calls":[{"fn":"testCall2","args":["hello"]}]});' in html
244266

245267

246268
def test_unicorn_render_calls_no_mount_call(settings):

tests/views/message/test_calls.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def test_call(self):
1111
def test_call2(self):
1212
self.call("testCall2")
1313

14+
def test_call3(self):
15+
self.call("testCall3", "hello")
16+
1417

1518
FAKE_CALLS_COMPONENT_URL = "/message/tests.views.message.test_calls.FakeCallsComponent"
1619

@@ -25,7 +28,7 @@ def test_message_calls(client):
2528
)
2629

2730
body = response.json()
28-
assert body.get("calls") == [{"fn": "testCall"}]
31+
assert body.get("calls") == [{"args": [], "fn": "testCall"}]
2932

3033

3134
def test_message_multiple_calls(client):
@@ -38,4 +41,20 @@ def test_message_multiple_calls(client):
3841
)
3942

4043
body = response.json()
41-
assert body.get("calls") == [{"fn": "testCall"}, {"fn": "testCall2"}]
44+
assert body.get("calls") == [
45+
{"args": [], "fn": "testCall"},
46+
{"args": [], "fn": "testCall2"},
47+
]
48+
49+
50+
def test_message_calls_with_arg(client):
51+
action_queue = [
52+
{"payload": {"name": "test_call3"}, "type": "callMethod", "target": None,}
53+
]
54+
55+
response = post_and_get_response(
56+
client, url=FAKE_CALLS_COMPONENT_URL, action_queue=action_queue
57+
)
58+
59+
body = response.json()
60+
assert body.get("calls") == [{"args": ["hello"], "fn": "testCall3"}]

0 commit comments

Comments
 (0)