Add frontend.remove_extra_js_url (#119831)

This commit is contained in:
Erik Montnemery 2024-06-17 15:15:00 +02:00 committed by GitHub
parent c0a3b507c0
commit 8af5748716
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 20 deletions

View File

@ -330,6 +330,15 @@ def add_extra_js_url(hass: HomeAssistant, url: str, es5: bool = False) -> None:
hass.data[key].add(url)
def remove_extra_js_url(hass: HomeAssistant, url: str, es5: bool = False) -> None:
"""Remove extra js or module url to load.
This function allows custom integrations to remove extra js or module.
"""
key = DATA_EXTRA_JS_URL_ES5 if es5 else DATA_EXTRA_MODULE_URL
hass.data[key].remove(url)
def add_manifest_json_key(key: str, val: Any) -> None:
"""Add a keyval to the manifest.json."""
MANIFEST_JSON.update_key(key, val)

View File

@ -21,6 +21,7 @@ from homeassistant.components.frontend import (
add_extra_js_url,
async_register_built_in_panel,
async_remove_panel,
remove_extra_js_url,
)
from homeassistant.components.websocket_api import TYPE_RESULT
from homeassistant.core import HomeAssistant
@ -409,43 +410,48 @@ async def test_missing_themes(hass: HomeAssistant, ws_client) -> None:
@pytest.mark.usefixtures("mock_onboarded")
async def test_extra_js(hass: HomeAssistant, mock_http_client_with_extra_js) -> None:
"""Test that extra javascript is loaded."""
resp = await mock_http_client_with_extra_js.get("")
assert resp.status == 200
assert "cache-control" not in resp.headers
text = await resp.text()
async def get_response():
resp = await mock_http_client_with_extra_js.get("")
assert resp.status == 200
assert "cache-control" not in resp.headers
return await resp.text()
text = await get_response()
assert '"/local/my_module.js"' in text
assert '"/local/my_es5.js"' in text
# Test dynamically adding extra javascript
# Test dynamically adding and removing extra javascript
add_extra_js_url(hass, "/local/my_module_2.js", False)
add_extra_js_url(hass, "/local/my_es5_2.js", True)
resp = await mock_http_client_with_extra_js.get("")
assert resp.status == 200
assert "cache-control" not in resp.headers
text = await resp.text()
text = await get_response()
assert '"/local/my_module_2.js"' in text
assert '"/local/my_es5_2.js"' in text
remove_extra_js_url(hass, "/local/my_module_2.js", False)
remove_extra_js_url(hass, "/local/my_es5_2.js", True)
text = await get_response()
assert '"/local/my_module_2.js"' not in text
assert '"/local/my_es5_2.js"' not in text
# Remove again should not raise
remove_extra_js_url(hass, "/local/my_module_2.js", False)
remove_extra_js_url(hass, "/local/my_es5_2.js", True)
text = await get_response()
assert '"/local/my_module_2.js"' not in text
assert '"/local/my_es5_2.js"' not in text
# safe mode
hass.config.safe_mode = True
resp = await mock_http_client_with_extra_js.get("")
assert resp.status == 200
assert "cache-control" not in resp.headers
text = await resp.text()
text = await get_response()
assert '"/local/my_module.js"' not in text
assert '"/local/my_es5.js"' not in text
# Test dynamically adding extra javascript
add_extra_js_url(hass, "/local/my_module_2.js", False)
add_extra_js_url(hass, "/local/my_es5_2.js", True)
resp = await mock_http_client_with_extra_js.get("")
assert resp.status == 200
assert "cache-control" not in resp.headers
text = await resp.text()
text = await get_response()
assert '"/local/my_module_2.js"' not in text
assert '"/local/my_es5_2.js"' not in text