Skip to content

Commit

Permalink
Add blocks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Loic Teixeira committed Apr 6, 2017
1 parent 906f50d commit 153c7f4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 16 deletions.
88 changes: 88 additions & 0 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from __future__ import absolute_import, unicode_literals

import unittest

from django.test import override_settings

from wagtaildraftail import blocks, draft_text, forms, widgets


class DraftailTextBlockTestCase(unittest.TestCase):

def test_get_default_with_string_default(self):
class StringDefaultDraftailTextBlock(blocks.DraftailTextBlock):
class Meta:
default = '{}'

block = StringDefaultDraftailTextBlock()
default = block.get_default()
expected_default = draft_text.DraftText('{}')

self.assertIsInstance(default, draft_text.DraftText)
self.assertEqual(default, expected_default)

def test_get_default_with_node_default(self):
class NodeDefaultDraftailTextBlock(blocks.DraftailTextBlock):
class Meta:
default = draft_text.DraftText('{}')

block = NodeDefaultDraftailTextBlock()
default = block.get_default()
expected_default = draft_text.DraftText('{}')

self.assertIsInstance(default, draft_text.DraftText)
self.assertEqual(default, expected_default)

def test_field_class(self):
block = blocks.DraftailTextBlock()

self.assertIsInstance(block.field, forms.SerializedJSONField)

@override_settings(WAGTAILADMIN_RICH_TEXT_EDITORS={
'test_editor': {
'WIDGET': 'wagtaildraftail.widgets.DraftailTextArea',
}
})
def test_field_is_initialized_with_widget(self):
block = blocks.DraftailTextBlock(editor='test_editor')

self.assertIsInstance(block.field.widget, widgets.DraftailTextArea)

def test_field_is_initialized_with_options(self):
options = {'required': False, 'help_text': 'weee'}
block = blocks.DraftailTextBlock(**options)

self.assertEqual(block.field.required, options['required'])
self.assertEqual(block.field.help_text, options['help_text'])

def test_to_python(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
python_value = blocks.DraftailTextBlock().to_python(value)
expected_python_value = draft_text.DraftText(value)

self.assertIsInstance(python_value, draft_text.DraftText)
self.assertEqual(python_value, expected_python_value)

def test_value_from_form(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
form_value = blocks.DraftailTextBlock().value_from_form(value)
expected_form_value = draft_text.DraftText(value)

self.assertIsInstance(form_value, draft_text.DraftText)
self.assertEqual(form_value, expected_form_value)

def test_get_searchable_content_with_string_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
searchable_content = blocks.DraftailTextBlock().get_searchable_content(value)
expected_searchable_content = [
'<p><strong>Cupcake</strong> ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...</p>']

self.assertEqual(searchable_content, expected_searchable_content)

def test_get_searchable_content_with_node_value(self):
value = draft_text.DraftText('{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}') # noqa: E501
searchable_content = blocks.DraftailTextBlock().get_searchable_content(value)
expected_searchable_content = [
'<p><strong>Cupcake</strong> ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...</p>']

self.assertEqual(searchable_content, expected_searchable_content)
13 changes: 0 additions & 13 deletions tests/test_wagtail_blocks.py

This file was deleted.

6 changes: 3 additions & 3 deletions wagtaildraftail/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def get_default(self):
else:
return DraftText(self.meta.default)

def to_python(self, value):
return DraftText(value)

@cached_property
def field(self):
from wagtail.wagtailadmin.rich_text import get_rich_text_editor_widget
return SerializedJSONField(widget=get_rich_text_editor_widget(self.editor), **self.field_options)

def to_python(self, value):
return DraftText(value)

def value_from_form(self, value):
return DraftText(value)

Expand Down

0 comments on commit 153c7f4

Please sign in to comment.