from typing import Any, Dict from django.conf import settings from django.http import HttpResponse from django.template import Context, Template from django.test import Client, SimpleTestCase from django.urls import path from django_components import Component, ComponentView, register, types from django_components.urls import urlpatterns as dc_urlpatterns from django_components.testing import djc_test from .testutils import setup_test_config setup_test_config({"autodiscover": False}) class CustomClient(Client): def __init__(self, urlpatterns=None, *args, **kwargs): import types if urlpatterns: urls_module = types.ModuleType("urls") urls_module.urlpatterns = urlpatterns + dc_urlpatterns # type: ignore settings.ROOT_URLCONF = urls_module else: settings.ROOT_URLCONF = __name__ settings.SECRET_KEY = "secret" # noqa super().__init__(*args, **kwargs) @djc_test class TestComponentAsView(SimpleTestCase): def test_render_component_from_template(self): @register("testcomponent") class MockComponentRequest(Component): template = """
""" def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]: return {"variable": variable} def render_template_view(request): template = Template( """ {% load component_tags %} {% component "testcomponent" variable="TEMPLATE" %}{% endcomponent %} """ ) return HttpResponse(template.render(Context({}))) client = CustomClient(urlpatterns=[path("test_template/", render_template_view)]) response = client.get("/test_template/") self.assertEqual(response.status_code, 200) self.assertInHTML( '', response.content.decode(), ) def test_get_request(self): class MockComponentRequest(Component): template = """ """ def get_context_data(self, variable): return {"inner_var": variable} class View(ComponentView): def get(self, request, *args, **kwargs) -> HttpResponse: return self.component.render_to_response(kwargs={"variable": "GET"}) client = CustomClient(urlpatterns=[path("test/", MockComponentRequest.as_view())]) response = client.get("/test/") self.assertEqual(response.status_code, 200) self.assertInHTML( '', response.content.decode(), ) def test_get_request_shortcut(self): class MockComponentRequest(Component): template = """ """ def get_context_data(self, variable): return {"inner_var": variable} def get(self, request, *args, **kwargs) -> HttpResponse: return self.render_to_response(kwargs={"variable": "GET"}) client = CustomClient(urlpatterns=[path("test/", MockComponentRequest.as_view())]) response = client.get("/test/") self.assertEqual(response.status_code, 200) self.assertInHTML( '', response.content.decode(), ) def test_post_request(self): class MockComponentRequest(Component): template: types.django_html = """ """ def get_context_data(self, variable): return {"inner_var": variable} class View(ComponentView): def post(self, request, *args, **kwargs) -> HttpResponse: variable = request.POST.get("variable") return self.component.render_to_response(kwargs={"variable": variable}) client = CustomClient(urlpatterns=[path("test/", MockComponentRequest.as_view())]) response = client.post("/test/", {"variable": "POST"}) self.assertEqual(response.status_code, 200) self.assertInHTML( '', response.content.decode(), ) def test_post_request_shortcut(self): class MockComponentRequest(Component): template: types.django_html = """ """ def get_context_data(self, variable): return {"inner_var": variable} def post(self, request, *args, **kwargs) -> HttpResponse: variable = request.POST.get("variable") return self.render_to_response(kwargs={"variable": variable}) client = CustomClient(urlpatterns=[path("test/", MockComponentRequest.as_view())]) response = client.post("/test/", {"variable": "POST"}) self.assertEqual(response.status_code, 200) self.assertInHTML( '', response.content.decode(), ) def test_instantiate_component(self): class MockComponentRequest(Component): template = """ """ def get_context_data(self, variable): return {"inner_var": variable} def get(self, request, *args, **kwargs) -> HttpResponse: return self.render_to_response(kwargs={"variable": self.name}) client = CustomClient(urlpatterns=[path("test/", MockComponentRequest("my_comp").as_view())]) response = client.get("/test/") self.assertEqual(response.status_code, 200) self.assertInHTML( '', response.content.decode(), ) def test_replace_slot_in_view(self): class MockComponentSlot(Component): template = """ {% load component_tags %}