Skip to content

Commit c52972a

Browse files
robwaadamghill
authored andcommitted
test: Add test with context variable
1 parent 5c31533 commit c52972a

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

tests/templatetags/test_unicorn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
from django.template.base import Token, TokenType
1+
from django.template.base import Parser, Token, TokenType
22

33
from django_unicorn.templatetags.unicorn import unicorn
44

55

66
def test_unicorn():
77
token = Token(TokenType.TEXT, "unicorn 'todo'")
8-
unicorn_node = unicorn(None, token)
8+
unicorn_node = unicorn(Parser([]), token)
99

10-
assert unicorn_node.component_name == "todo"
10+
assert unicorn_node.component_name.resolve({}) == "todo"
1111
assert len(unicorn_node.kwargs) == 0
1212

1313

1414
def test_unicorn_kwargs():
1515
token = Token(TokenType.TEXT, "unicorn 'todo' blob='blob'")
16-
unicorn_node = unicorn(None, token)
16+
unicorn_node = unicorn(Parser([]), token)
1717

1818
assert unicorn_node.kwargs == {"blob": "blob"}
1919

2020

2121
def test_unicorn_args_and_kwargs():
2222
# args after the component name get ignored
2323
token = Token(TokenType.TEXT, "unicorn 'todo' '1' 2 hello='world' test=3 '4'")
24-
unicorn_node = unicorn(None, token)
24+
unicorn_node = unicorn(Parser([]), token)
2525

2626
assert unicorn_node.kwargs == {"hello": "world", "test": 3}

tests/templatetags/test_unicorn_render.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from example.coffee.models import Flavor
1212

1313

14+
class FakeComponent(UnicornView):
15+
template_name = "templates/test_component.html"
16+
17+
1418
class FakeComponentParent(UnicornView):
1519
template_name = "templates/test_component_parent.html"
1620

@@ -66,7 +70,7 @@ def test_unicorn_render_kwarg():
6670
TokenType.TEXT,
6771
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' test_kwarg='tested!'",
6872
)
69-
unicorn_node = unicorn(None, token)
73+
unicorn_node = unicorn(Parser([]), token)
7074
context = {}
7175
actual = unicorn_node.render(Context(context))
7276

@@ -78,7 +82,7 @@ def test_unicorn_render_context_variable():
7882
TokenType.TEXT,
7983
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' test_kwarg=test_var.nested",
8084
)
81-
unicorn_node = unicorn(None, token)
85+
unicorn_node = unicorn(Parser([]), token)
8286
context = {"test_var": {"nested": "variable!"}}
8387
actual = unicorn_node.render(Context(context))
8488

@@ -90,7 +94,7 @@ def test_unicorn_render_with_invalid_html():
9094
TokenType.TEXT,
9195
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargsWithHtmlEntity' test_kwarg=test_var.nested",
9296
)
93-
unicorn_node = unicorn(None, token)
97+
unicorn_node = unicorn(Parser([]), token)
9498
context = {"test_var": {"nested": "variable!"}}
9599
actual = unicorn_node.render(Context(context))
96100

@@ -103,7 +107,7 @@ def test_unicorn_render_parent(settings):
103107
TokenType.TEXT,
104108
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' parent=view",
105109
)
106-
unicorn_node = unicorn(None, token)
110+
unicorn_node = unicorn(Parser([]), token)
107111
view = FakeComponentParent(component_name="test", component_id="asdf")
108112
context = {"view": view}
109113
unicorn_node.render(Context(context))
@@ -121,7 +125,7 @@ def test_unicorn_render_parent_with_key(settings):
121125
TokenType.TEXT,
122126
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' parent=view key='blob'",
123127
)
124-
unicorn_node = unicorn(None, token)
128+
unicorn_node = unicorn(Parser([]), token)
125129
view = FakeComponentParent(component_name="test", component_id="asdf")
126130
context = {"view": view}
127131
unicorn_node.render(Context(context))
@@ -138,7 +142,7 @@ def test_unicorn_render_parent_with_id(settings):
138142
TokenType.TEXT,
139143
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' parent=view id='flob'",
140144
)
141-
unicorn_node = unicorn(None, token)
145+
unicorn_node = unicorn(Parser([]), token)
142146
view = FakeComponentParent(component_name="test", component_id="asdf")
143147
context = {"view": view}
144148
unicorn_node.render(Context(context))
@@ -155,7 +159,7 @@ def test_unicorn_render_parent_with_pk(settings):
155159
TokenType.TEXT,
156160
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' parent=view pk=99",
157161
)
158-
unicorn_node = unicorn(None, token)
162+
unicorn_node = unicorn(Parser([]), token)
159163
view = FakeComponentParent(component_name="test", component_id="asdf")
160164
context = {"view": view}
161165
unicorn_node.render(Context(context))
@@ -172,7 +176,7 @@ def test_unicorn_render_parent_with_model_id(settings):
172176
TokenType.TEXT,
173177
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' parent=view model=model",
174178
)
175-
unicorn_node = unicorn(None, token)
179+
unicorn_node = unicorn(Parser([]), token)
176180
view = FakeComponentParent(component_name="test", component_id="asdf")
177181

178182
# Fake a model that only has an id
@@ -199,7 +203,7 @@ def test_unicorn_render_parent_with_model_pk(settings):
199203
TokenType.TEXT,
200204
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' parent=view model=model",
201205
)
202-
unicorn_node = unicorn(None, token)
206+
unicorn_node = unicorn(Parser([]), token)
203207
view = FakeComponentParent(component_name="test", component_id="asdf")
204208

205209
flavor = Flavor(pk=187)
@@ -218,7 +222,7 @@ def test_unicorn_render_id_use_pk():
218222
TokenType.TEXT,
219223
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentModel' model_id=model.id",
220224
)
221-
unicorn_node = unicorn(None, token)
225+
unicorn_node = unicorn(Parser([]), token)
222226
context = {"model": {"pk": 123}}
223227
actual = unicorn_node.render(Context(context))
224228

@@ -231,7 +235,7 @@ def test_unicorn_render_component_one_script_tag(settings):
231235
TokenType.TEXT,
232236
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs'",
233237
)
234-
unicorn_node = unicorn(None, token)
238+
unicorn_node = unicorn(Parser([]), token)
235239
context = {}
236240
html = unicorn_node.render(Context(context))
237241

@@ -245,7 +249,7 @@ def test_unicorn_render_child_component_no_script_tag(settings):
245249
TokenType.TEXT,
246250
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentKwargs' parent=view",
247251
)
248-
unicorn_node = unicorn(None, token)
252+
unicorn_node = unicorn(Parser([]), token)
249253
view = FakeComponentParent(component_name="test", component_id="asdf")
250254
context = {"view": view}
251255
html = unicorn_node.render(Context(context))
@@ -259,7 +263,7 @@ def test_unicorn_render_parent_component_one_script_tag(settings):
259263
TokenType.TEXT,
260264
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentParent'",
261265
)
262-
unicorn_node = unicorn(None, token)
266+
unicorn_node = unicorn(Parser([]), token)
263267
context = {}
264268
html = unicorn_node.render(Context(context))
265269

@@ -273,7 +277,7 @@ def test_unicorn_render_calls(settings):
273277
TokenType.TEXT,
274278
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentCalls'",
275279
)
276-
unicorn_node = unicorn(None, token)
280+
unicorn_node = unicorn(Parser([]), token)
277281
context = {}
278282
html = unicorn_node.render(Context(context))
279283

@@ -288,7 +292,7 @@ def test_unicorn_render_calls_with_arg(settings):
288292
TokenType.TEXT,
289293
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentCalls2'",
290294
)
291-
unicorn_node = unicorn(None, token)
295+
unicorn_node = unicorn(Parser([]), token)
292296
context = {}
293297
html = unicorn_node.render(Context(context))
294298

@@ -303,7 +307,7 @@ def test_unicorn_render_calls_no_mount_call(settings):
303307
TokenType.TEXT,
304308
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentParent'",
305309
)
306-
unicorn_node = unicorn(None, token)
310+
unicorn_node = unicorn(Parser([]), token)
307311
context = {}
308312
html = unicorn_node.render(Context(context))
309313

@@ -318,7 +322,7 @@ def test_unicorn_render_hash(settings):
318322
TokenType.TEXT,
319323
"unicorn 'tests.templatetags.test_unicorn_render.FakeComponentParent'",
320324
)
321-
unicorn_node = unicorn(None, token)
325+
unicorn_node = unicorn(Parser([]), token)
322326
context = {}
323327
html = unicorn_node.render(Context(context))
324328

@@ -331,3 +335,16 @@ def test_unicorn_render_hash(settings):
331335
rendered_content = html[:script_idx]
332336
expected_hash = generate_checksum(rendered_content)
333337
assert f'"hash":"{expected_hash}"' in html
338+
339+
340+
def test_unicorn_render_with_component_name_from_context():
341+
token = Token(
342+
TokenType.TEXT,
343+
"unicorn component_name",
344+
)
345+
unicorn_node = unicorn(Parser([]), token)
346+
context = {"component_name": "tests.templatetags.test_unicorn_render.FakeComponent"}
347+
unicorn_node.render(Context(context))
348+
349+
# Success if no exception was raised so far
350+
assert True

0 commit comments

Comments
 (0)