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 from django.urls import path from django_components import Component, ComponentView, register, types from .django_test_setup import setup_test_config from .testutils import BaseTestCase, parametrize_context_behavior 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 # type: ignore settings.ROOT_URLCONF = urls_module else: settings.ROOT_URLCONF = __name__ settings.SECRET_KEY = "secret" # noqa super().__init__(*args, **kwargs) class TestComponentAsView(BaseTestCase): 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.assertIn( b'', response.content, ) 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.assertIn( b'', response.content, ) 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.assertIn( b'', response.content, ) 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.assertIn( b'', response.content, ) 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.assertIn( b'', response.content, ) @parametrize_context_behavior(["django", "isolated"]) def test_replace_slot_in_view(self): class MockComponentSlot(Component): template = """ {% load component_tags %}