|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
| 7 | +from django_bird.components import components |
7 | 8 | from django_bird.staticfiles import Asset |
8 | 9 | from django_bird.staticfiles import AssetType |
| 10 | +from django_bird.staticfiles import assets |
9 | 11 | from django_bird.staticfiles import get_template_assets |
10 | 12 |
|
11 | 13 |
|
@@ -119,3 +121,144 @@ def test_custom_component_dir(self, create_template, create_bird_template): |
119 | 121 |
|
120 | 122 | assert len(assets) == 1 |
121 | 123 | assert Asset(css_file, AssetType.CSS) in assets |
| 124 | + |
| 125 | + |
| 126 | +class TestComponentAssetRegistry: |
| 127 | + def test_asset_registry( |
| 128 | + self, create_bird_template, create_bird_asset, create_template, templates_dir |
| 129 | + ): |
| 130 | + alert = create_bird_template("alert", '<div class="alert">{{ slot }}</div>') |
| 131 | + create_bird_asset(alert, ".alert { color: red; }", "css") |
| 132 | + create_bird_asset(alert, "console.log('alert');", "js") |
| 133 | + |
| 134 | + badge = create_bird_template("badge", "<span>{{ slot }}</span>") |
| 135 | + create_bird_asset(badge, ".badge { color: blue; }", "css") |
| 136 | + create_bird_asset(badge, "console.log('badge');", "js") |
| 137 | + |
| 138 | + button = create_bird_template("button", "<button>{{ slot }}</button>") |
| 139 | + create_bird_asset(button, ".button { color: blue; }", "css") |
| 140 | + create_bird_asset(button, "console.log('button');", "js") |
| 141 | + |
| 142 | + base_path = templates_dir / "base.html" |
| 143 | + base_path.write_text(""" |
| 144 | + <html> |
| 145 | + <head> |
| 146 | + <title>Test</title> |
| 147 | + {% bird:css %} |
| 148 | + </head> |
| 149 | + <body> |
| 150 | + {% bird alert %}Base Alert{% endbird %} |
| 151 | + {% block content %}{% endblock %} |
| 152 | + {% bird:js %} |
| 153 | + </body> |
| 154 | + </html> |
| 155 | + """) |
| 156 | + |
| 157 | + include_path = templates_dir / "include.html" |
| 158 | + include_path.write_text(""" |
| 159 | + {% bird badge %}Active{% endbird %} |
| 160 | + """) |
| 161 | + |
| 162 | + child_path = templates_dir / "child.html" |
| 163 | + child_path.write_text(""" |
| 164 | + {% extends 'base.html' %} |
| 165 | + {% block content %} |
| 166 | + {% bird button %}Click me{% endbird %} |
| 167 | + {% include 'include.html' %} |
| 168 | + {% endblock %} |
| 169 | + """) |
| 170 | + |
| 171 | + create_template(child_path) |
| 172 | + |
| 173 | + assert len(assets.components) == 3 |
| 174 | + |
| 175 | + def test_get_assets_by_type( |
| 176 | + self, create_bird_template, create_bird_asset, create_template |
| 177 | + ): |
| 178 | + template_file = create_bird_template("test", "<div>Test</div>") |
| 179 | + css_asset = create_bird_asset(template_file, ".test { color: red; }", "css") |
| 180 | + js_asset = create_bird_asset(template_file, "console.log('test');", "js") |
| 181 | + |
| 182 | + component = components.get_component("test") |
| 183 | + assets.register(component) |
| 184 | + |
| 185 | + css_assets = assets.get_assets(AssetType.CSS) |
| 186 | + js_assets = assets.get_assets(AssetType.JS) |
| 187 | + |
| 188 | + assert len(css_assets) == 1 |
| 189 | + assert len(js_assets) == 1 |
| 190 | + assert Asset(Path(css_asset), AssetType.CSS) in css_assets |
| 191 | + assert Asset(Path(js_asset), AssetType.JS) in js_assets |
| 192 | + |
| 193 | + def test_register_same_component_multiple_times( |
| 194 | + self, create_bird_template, create_bird_asset |
| 195 | + ): |
| 196 | + template_file = create_bird_template("test", "<div>Test</div>") |
| 197 | + create_bird_asset(template_file, ".test { color: red; }", "css") |
| 198 | + |
| 199 | + component = components.get_component("test") |
| 200 | + |
| 201 | + assets.register(component) |
| 202 | + assets.register(component) |
| 203 | + |
| 204 | + assert len(assets.components) == 1 |
| 205 | + assert len(assets.get_assets(AssetType.CSS)) == 1 |
| 206 | + |
| 207 | + def test_multiple_components_same_asset_names( |
| 208 | + self, create_bird_template, create_bird_asset |
| 209 | + ): |
| 210 | + template1 = create_bird_template("comp1", "<div>One</div>", sub_dir="first") |
| 211 | + template2 = create_bird_template("comp2", "<div>Two</div>", sub_dir="second") |
| 212 | + |
| 213 | + css1 = create_bird_asset(template1, ".one { color: red; }", "css") |
| 214 | + css2 = create_bird_asset(template2, ".two { color: blue; }", "css") |
| 215 | + |
| 216 | + comp1 = components.get_component("first/comp1") |
| 217 | + comp2 = components.get_component("second/comp2") |
| 218 | + |
| 219 | + assets.register(comp1) |
| 220 | + assets.register(comp2) |
| 221 | + |
| 222 | + css_assets = assets.get_assets(AssetType.CSS) |
| 223 | + assert len(css_assets) == 2 |
| 224 | + |
| 225 | + asset_paths = {str(asset.path) for asset in css_assets} |
| 226 | + assert str(css1) in asset_paths |
| 227 | + assert str(css2) in asset_paths |
| 228 | + |
| 229 | + def test_template_inheritance_assets( |
| 230 | + self, create_bird_template, create_bird_asset, create_template, templates_dir |
| 231 | + ): |
| 232 | + parent = create_bird_template("parent", "<div>Parent</div>") |
| 233 | + child = create_bird_template("child", "<div>Child</div>") |
| 234 | + |
| 235 | + parent_css = create_bird_asset(parent, ".parent { color: red; }", "css") |
| 236 | + child_css = create_bird_asset(child, ".child { color: blue; }", "css") |
| 237 | + |
| 238 | + base_path = templates_dir / "base.html" |
| 239 | + base_path.write_text(""" |
| 240 | + {% bird parent %}Parent Content{% endbird %} |
| 241 | + {% block content %}{% endblock %} |
| 242 | + """) |
| 243 | + |
| 244 | + child_path = templates_dir / "child.html" |
| 245 | + child_path.write_text(""" |
| 246 | + {% extends 'base.html' %} |
| 247 | + {% block content %} |
| 248 | + {% bird child %}Child Content{% endbird %} |
| 249 | + {% endblock %} |
| 250 | + """) |
| 251 | + |
| 252 | + template = create_template(child_path) |
| 253 | + template.render({}) |
| 254 | + |
| 255 | + css_assets = assets.get_assets(AssetType.CSS) |
| 256 | + asset_paths = {str(asset.path) for asset in css_assets} |
| 257 | + |
| 258 | + assert str(parent_css) in asset_paths, "Parent CSS not found in assets" |
| 259 | + assert str(child_css) in asset_paths, "Child CSS not found in assets" |
| 260 | + |
| 261 | + def test_empty_registry(self): |
| 262 | + assert len(assets.components) == 0 |
| 263 | + assert len(assets.get_assets(AssetType.CSS)) == 0 |
| 264 | + assert len(assets.get_assets(AssetType.JS)) == 0 |
0 commit comments