Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danialkeimasi committed May 13, 2024
1 parent 9f15da1 commit 090d076
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
pip install -e ".[dev]"
- name: Run tests
run: |
py.test --cov
pytest --cov
3 changes: 2 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django_nextjs.apps.DjangoNextJSConfig",
]

MIDDLEWARE = [
Expand All @@ -27,7 +28,7 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"DIRS": [os.path.join(BASE_DIR, "templates")],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand Down
10 changes: 10 additions & 0 deletions tests/templates/custom_document.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "django_nextjs/document_base.html" %}
{% block head %}
before_head
{{ block.super }}
after_head
{% endblock %}

{% block body %}
{{ block.super }}
{% endblock %}
22 changes: 19 additions & 3 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from django.utils.datastructures import MultiValueDict

from django_nextjs.app_settings import NEXTJS_SERVER_URL
from django_nextjs.render import _get_render_context, render_nextjs_page
from django_nextjs.render import _get_render_context, render_nextjs_page_to_string
from django_nextjs.views import nextjs_page


def test_get_render_context_empty_html():
Expand Down Expand Up @@ -48,7 +49,7 @@ def test_get_render_context_html_with_sections_and_content():


@pytest.mark.asyncio
async def test_render_nextjs_page(rf: RequestFactory):
async def test_nextjs_page(rf: RequestFactory):
path = "random/path"
params = MultiValueDict({"name": ["Adrian", "Simon"], "position": ["Developer"]})
request = rf.get(f"/{path}", data=params)
Expand All @@ -61,7 +62,7 @@ async def test_render_nextjs_page(rf: RequestFactory):
mock_get.return_value.__aenter__.return_value.headers = {"Location": "target_value", "unimportant": ""}
mock_session.return_value.__aenter__ = AsyncMock(return_value=MagicMock(get=mock_get))

http_response = await render_nextjs_page(request, allow_redirects=True, headers={"extra": "headers"})
http_response = await nextjs_page(allow_redirects=True, headers={"extra": "headers"})(request)

assert http_response.content == nextjs_response.encode()
assert http_response.status_code == 200
Expand All @@ -80,3 +81,18 @@ async def test_render_nextjs_page(rf: RequestFactory):
assert kwargs["headers"]["user-agent"] == ""
assert kwargs["headers"]["x-real-ip"] == "127.0.0.1"
assert kwargs["headers"]["extra"] == "headers"


@pytest.mark.asyncio
async def test_render_nextjs_page_to_string(rf: RequestFactory):
request = rf.get(f"/random/path")
nextjs_response = """<html><head><link/></head><body id="__django_nextjs_body"><div id="__django_nextjs_body_begin"/><div id="__django_nextjs_body_end"/></body></html>"""

with patch("aiohttp.ClientSession") as mock_session:
with patch("aiohttp.ClientSession.get") as mock_get:
mock_get.return_value.__aenter__.return_value.text = AsyncMock(return_value=nextjs_response)
mock_session.return_value.__aenter__ = AsyncMock(return_value=MagicMock(get=mock_get))

response_text = await render_nextjs_page_to_string(request, template_name="custom_document.html")
assert "before_head" in response_text
assert "after_head" in response_text

0 comments on commit 090d076

Please sign in to comment.