11from __future__ import annotations
22
3+ from pathlib import Path
4+
35import pytest
46from django .template .backends .django import Template
57from django .template .exceptions import TemplateDoesNotExist
@@ -18,7 +20,7 @@ def test_from_name_basic(self, create_bird_template):
1820 comp = Component .from_name ("button" )
1921
2022 assert comp .name == "button"
21- assert comp .assets == set ()
23+ assert comp .assets == frozenset ()
2224 assert isinstance (comp .template , Template )
2325 assert comp .render ({}) == "<button>Click me</button>"
2426
@@ -38,30 +40,50 @@ def test_from_name_with_assets(self, create_template, create_bird_template):
3840 assert Asset (js_file , AssetType .JS ) in comp .assets
3941
4042 @pytest .mark .parametrize (
41- "asset_suffix,asset_content,expected_asset_type " ,
43+ "suffix,content,expected_type " ,
4244 [
4345 (".css" , "button { color: red; }" , AssetType .CSS ),
4446 (".js" , "console.log('loaded');" , AssetType .JS ),
4547 ],
4648 )
4749 def test_from_name_with_partial_assets (
48- self ,
49- asset_suffix ,
50- asset_content ,
51- expected_asset_type ,
52- create_template ,
53- create_bird_template ,
50+ self , suffix , content , expected_type , create_template , create_bird_template
5451 ):
5552 template_file = create_bird_template ("button" , "<button>Click me</button>" )
5653 create_template (template_file )
5754
58- file = template_file .with_suffix (asset_suffix )
59- file .write_text (asset_content )
55+ file = template_file .with_suffix (suffix )
56+ file .write_text (content )
6057
6158 comp = Component .from_name ("button" )
6259
6360 assert len (comp .assets ) == 1
64- assert Asset (file , expected_asset_type ) in comp .assets
61+ assert Asset (file , expected_type ) in comp .assets
62+
63+ def test_from_name_no_assets (self , create_template , create_bird_template ):
64+ template_file = create_bird_template ("button" , "<button>Click me</button>" )
65+ create_template (template_file )
66+
67+ comp = Component .from_name ("button" )
68+
69+ assert len (comp .assets ) == 0
70+
71+ def test_from_name_custom_component_dir (
72+ self , create_template , create_bird_template
73+ ):
74+ template_file = create_bird_template (
75+ name = "button" , content = "<button>" , sub_dir = "components"
76+ )
77+
78+ css_file = template_file .with_suffix (".css" )
79+ css_file .write_text ("button { color: red; }" )
80+
81+ create_template (template_file )
82+
83+ comp = Component .from_name ("components/button" )
84+
85+ assert len (comp .assets ) == 1
86+ assert Asset (css_file , AssetType .CSS ) in comp .assets
6587
6688
6789class TestComponentRegistry :
@@ -104,13 +126,97 @@ def test_component_not_found(self, registry):
104126 def test_cache_with_debug (self , registry , create_bird_template ):
105127 create_bird_template (name = "button" , content = "<button>Click me</button>" )
106128
107- assert len (registry ._cache ) == 0
129+ assert len (registry ._components ) == 0
108130
109131 with override_settings (DEBUG = True ):
110132 registry .get_component ("button" )
111133
112- assert len (registry ._cache ) == 0
134+ assert len (registry ._components ) == 0
113135
114136 registry .get_component ("button" )
115137
116- assert len (registry ._cache ) == 1
138+ assert len (registry ._components ) == 1
139+
140+ def test_get_assets_by_type (
141+ self , registry , create_bird_template , create_bird_asset
142+ ):
143+ template_file = create_bird_template ("test" , "<div>Test</div>" )
144+ css_asset = create_bird_asset (template_file , ".test { color: red; }" , "css" )
145+ js_asset = create_bird_asset (template_file , "console.log('test');" , "js" )
146+
147+ registry .get_component (
148+ "test"
149+ ) # This now registers the component and its assets
150+
151+ css_assets = registry .get_assets (AssetType .CSS )
152+ js_assets = registry .get_assets (AssetType .JS )
153+
154+ assert len (css_assets ) == 1
155+ assert len (js_assets ) == 1
156+ assert Asset (Path (css_asset ), AssetType .CSS ) in css_assets
157+ assert Asset (Path (js_asset ), AssetType .JS ) in js_assets
158+
159+ def test_multiple_components_same_asset_names (
160+ self , registry , create_bird_template , create_bird_asset
161+ ):
162+ template1 = create_bird_template ("comp1" , "<div>One</div>" , sub_dir = "first" )
163+ template2 = create_bird_template ("comp2" , "<div>Two</div>" , sub_dir = "second" )
164+
165+ css1 = create_bird_asset (template1 , ".one { color: red; }" , "css" )
166+ css2 = create_bird_asset (template2 , ".two { color: blue; }" , "css" )
167+
168+ registry .get_component ("first/comp1" )
169+ registry .get_component ("second/comp2" )
170+
171+ css_assets = registry .get_assets (AssetType .CSS )
172+ assert len (css_assets ) == 2
173+
174+ asset_paths = {str (asset .path ) for asset in css_assets }
175+ assert str (css1 ) in asset_paths
176+ assert str (css2 ) in asset_paths
177+
178+ def test_template_inheritance_assets (
179+ self ,
180+ registry ,
181+ create_bird_template ,
182+ create_bird_asset ,
183+ create_template ,
184+ templates_dir ,
185+ ):
186+ parent = create_bird_template ("parent" , "<div>Parent</div>" )
187+ child = create_bird_template ("child" , "<div>Child</div>" )
188+
189+ parent_css = create_bird_asset (parent , ".parent { color: red; }" , "css" )
190+ child_css = create_bird_asset (child , ".child { color: blue; }" , "css" )
191+
192+ base_path = templates_dir / "base.html"
193+ base_path .write_text ("""
194+ {% bird parent %}Parent Content{% endbird %}
195+ {% block content %}{% endblock %}
196+ """ )
197+
198+ child_path = templates_dir / "child.html"
199+ child_path .write_text ("""
200+ {% extends 'base.html' %}
201+ {% block content %}
202+ {% bird child %}Child Content{% endbird %}
203+ {% endblock %}
204+ """ )
205+
206+ # Pre-load the components so they're in the registry
207+ registry .get_component ("parent" )
208+ registry .get_component ("child" )
209+
210+ template = create_template (child_path )
211+ template .render ({})
212+
213+ css_assets = registry .get_assets (AssetType .CSS )
214+ asset_paths = {str (asset .path ) for asset in css_assets }
215+
216+ assert str (parent_css ) in asset_paths , "Parent CSS not found in assets"
217+ assert str (child_css ) in asset_paths , "Child CSS not found in assets"
218+
219+ def test_empty_registry (self , registry ):
220+ assert len (registry ._components ) == 0
221+ assert len (registry .get_assets (AssetType .CSS )) == 0
222+ assert len (registry .get_assets (AssetType .JS )) == 0
0 commit comments