Skip to content
Snippets Groups Projects
Commit b097eaa2 authored by Martin Burchell's avatar Martin Burchell
Browse files

Modified render_tab to have access to the request

Also refactored some of the tests to pick out the args of interest
sent to render_string
parent 81cc4e8f
No related branches found
No related tags found
2 merge requests!23Tabbed view header styling,!22Tabbed page
from django import template
from django.template.loader import render_to_string
from ..tab_pool import get_tab
def render_tab(tab_instance):
register = template.Library()
@register.simple_tag(takes_context=True)
def render_tab(context, tab_instance):
tab = get_tab(tab_instance.name)
template_name = tab.template_name
......@@ -13,6 +18,13 @@ def render_tab(tab_instance):
else:
settings = {} # TODO: json field doesn't default to this?
context = tab.get_context_data(**settings)
tab_details = tab.get_context_data(**settings)
if context: # TODO: Only used for testing?
request = context.get('request')
else:
request = None
return render_to_string(template_name, context)
return render_to_string(template_name,
context=tab_details,
request=request)
......@@ -27,9 +27,10 @@ def test_uses_template_name(mock_render):
page = TabbedPageFactory()
tab_instance = TabInstanceFactory(page=page, name='test-tab')
render_tab(tab_instance)
render_tab(None, tab_instance)
mock_render.assert_called_once_with('test-tab-template', {})
args, _ = mock_render.call_args
assert args[0] == 'test-tab-template'
@pytest.mark.django_db
......@@ -44,9 +45,27 @@ def test_uses_context(mock_render):
tab_instance = TabInstanceFactory(page=page, name='test-tab')
render_tab(tab_instance)
render_tab(None, tab_instance)
mock_render.assert_called_once_with(None, test_context)
_, kwargs = mock_render.call_args
assert kwargs['context'] == test_context
@pytest.mark.django_db
@patch(render_to_string_method)
def test_uses_request(mock_render):
tab = TestTab()
register_tab('test-tab', tab)
page = TabbedPageFactory()
tab_instance = TabInstanceFactory(page=page, name='test-tab')
request = 'a request'
context = {'request': request}
render_tab(context, tab_instance)
_, kwargs = mock_render.call_args
assert kwargs['request'] == request
@pytest.mark.django_db
......@@ -62,6 +81,7 @@ def test_settings_passed_to_widget_get_context_data(render_to_string_method):
tab_instance = TabInstanceFactory(page=page,
name='test-tab',
settings=settings)
render_tab(tab_instance)
render_tab(None, tab_instance)
mock_get_context.assert_called_once_with(columns=columns)
_, kwargs = mock_get_context.call_args
assert kwargs['columns'] == columns
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment