mirror of
https://github.com/wagtail/wagtail.git
synced 2024-11-30 01:46:24 +01:00
Add tests for rendered StreamField output
This commit is contained in:
parent
f6fb743a18
commit
439e4b9a3d
@ -1,8 +1,13 @@
|
||||
import json
|
||||
import unittest
|
||||
|
||||
import django
|
||||
from django.apps import apps
|
||||
from django.test import TestCase
|
||||
from django.db import models
|
||||
from django.template import Template, Context
|
||||
from django.utils.safestring import SafeText
|
||||
from django.utils.six import text_type
|
||||
|
||||
from wagtail.tests.testapp.models import StreamModel
|
||||
from wagtail.wagtailcore import blocks
|
||||
@ -128,3 +133,56 @@ class TestStreamValueAccess(TestCase):
|
||||
self.assertEqual(len(fetched_body), 1)
|
||||
self.assertIsInstance(fetched_body[0].value, RichText)
|
||||
self.assertEqual(fetched_body[0].value.source, "<h2>hello world</h2>")
|
||||
|
||||
|
||||
class TestStreamFieldRenderingBase(TestCase):
|
||||
def setUp(self):
|
||||
self.image = Image.objects.create(
|
||||
title='Test image',
|
||||
file=get_test_image_file())
|
||||
|
||||
self.instance = StreamModel.objects.create(body=json.dumps([
|
||||
{'type': 'rich_text', 'value': '<p>Rich text</p>'},
|
||||
{'type': 'image', 'value': self.image.pk},
|
||||
{'type': 'text', 'value': 'Hello, World!'}]))
|
||||
|
||||
img_tag = self.image.get_rendition('original').img_tag()
|
||||
self.expected = ''.join([
|
||||
'<div class="block-rich_text"><div class="rich-text"><p>Rich text</p></div></div>',
|
||||
'<div class="block-image">{}</div>'.format(img_tag),
|
||||
'<div class="block-text">Hello, World!</div>',
|
||||
])
|
||||
|
||||
|
||||
class TestStreamFieldRendering(TestStreamFieldRenderingBase):
|
||||
def test_to_string(self):
|
||||
rendered = text_type(self.instance.body)
|
||||
self.assertHTMLEqual(rendered, self.expected)
|
||||
self.assertIsInstance(rendered, SafeText)
|
||||
|
||||
|
||||
class TestStreamFieldDjangoRendering(TestStreamFieldRenderingBase):
|
||||
def render(self, string, context):
|
||||
return Template(string).render(Context(context))
|
||||
|
||||
def test_render(self):
|
||||
rendered = self.render('{{ instance.body }}', {
|
||||
'instance': self.instance})
|
||||
self.assertHTMLEqual(rendered, self.expected)
|
||||
|
||||
|
||||
@unittest.skipIf(django.VERSION < (1, 8), 'Multiple engines only supported in Django>=1.8')
|
||||
class TestStreamFieldJinjaRendering(TestStreamFieldRenderingBase):
|
||||
def setUp(self):
|
||||
# This does not exist on Django<1.8
|
||||
super(TestStreamFieldJinjaRendering, self).setUp()
|
||||
from django.template import engines
|
||||
self.engine = engines['jinja2']
|
||||
|
||||
def render(self, string, context):
|
||||
return self.engine.from_string(string).render(context)
|
||||
|
||||
def test_render(self):
|
||||
rendered = self.render('{{ instance.body }}', {
|
||||
'instance': self.instance})
|
||||
self.assertHTMLEqual(rendered, self.expected)
|
||||
|
Loading…
Reference in New Issue
Block a user