Skip to content

Commit 33e245c

Browse files
add test for slot usage and fix active_slot assignment bug (#24)
1 parent c494de9 commit 33e245c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/django_bird/templatetags/django_bird.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def render_slots(self, context: Context) -> dict[str, str]:
8181
if isinstance(node, SlotNode):
8282
active_slot = node.name
8383
contents.setdefault(active_slot, [])
84+
else:
85+
active_slot = self.default_slot
8486

8587
rendered_content = node.render(context)
8688
contents[active_slot].append(rendered_content)

tests/test_templatetags.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,49 @@ def test_self_closing_tag(self, create_bird_template):
127127
rendered = template.render(context=Context({}))
128128
assert rendered == "<img src='image' />"
129129

130+
@pytest.mark.parametrize(
131+
"component,template,context,expected",
132+
[
133+
(
134+
"<button>{{ slot }}</button>",
135+
"{% bird button %}Click me{% endbird %}",
136+
{},
137+
"<button>Click me</button>",
138+
),
139+
(
140+
"<button>{% bird:slot %}{% endbird:slot %}</button>",
141+
"{% bird button %}Click me{% endbird %}",
142+
{},
143+
"<button>Click me</button>",
144+
),
145+
(
146+
"<button>{% bird:slot default %}{% endbird:slot %}</button>",
147+
"{% bird button %}Click me{% endbird %}",
148+
{},
149+
"<button>Click me</button>",
150+
),
151+
(
152+
"<button><span>{% bird:slot leading-icon %}{% endbird:slot %}</span>{{ slot }}</button>",
153+
"{% bird button %}{% bird:slot leading-icon %}Icon here{% endbird:slot %}Click me{% endbird %}",
154+
{},
155+
"<button><span>Icon here</span>Click me</button>",
156+
),
157+
],
158+
)
159+
def test_with_slots(
160+
self,
161+
component,
162+
template,
163+
context,
164+
expected,
165+
create_bird_template,
166+
normalize_whitespace,
167+
):
168+
create_bird_template("button", component)
169+
t = Template(template)
170+
rendered = t.render(context=Context(context))
171+
assert normalize_whitespace(rendered) == expected
172+
130173

131174
@pytest.mark.xfail(reason="Feature not implemented yet")
132175
class TestBirdTemplateTagFutureFeatures:

0 commit comments

Comments
 (0)