diff --git a/wagtail/contrib/table_block/tests.py b/wagtail/contrib/table_block/tests.py
index fab7674f37..5332a03ee4 100644
--- a/wagtail/contrib/table_block/tests.py
+++ b/wagtail/contrib/table_block/tests.py
@@ -3,7 +3,6 @@ import json
from django.test import SimpleTestCase, TestCase
from django.urls import reverse
from django.utils import translation
-from django.utils.html import escape
from wagtail.contrib.table_block.blocks import DEFAULT_TABLE_OPTIONS, TableBlock
from wagtail.core.models import Page
@@ -11,60 +10,7 @@ from wagtail.tests.testapp.models import TableBlockStreamPage
from wagtail.tests.utils import WagtailTestUtils
-def get_cell_classname(cells_meta, row_index, col_index):
- """
- Helper function used in building a test html
- table. Provides a cell's class attribute if
- one is specified in the meta.
- """
- if cells_meta:
- for meta in cells_meta:
- if meta.get('row') == row_index and meta.get('col') == col_index:
- return ' class="%s"' % meta.get('className')
- return ''
-
-
-def tiny_escape(val):
- """
- Helper function used in building a test html
- table.
- """
- return '' if val is None else escape(val)
-
-
-def get_test_html_from_value(value):
- """
- Generate a test html from a TableBlock value.
- Individual column values are escaped because
- that's what we expect from the TableBlock.
- """
- data = list(value['data']) # Make a copy
- meta = value.get('cell')
- table = '
'
- if value['first_row_is_table_header']:
- row_header = data.pop(0)
- table += ''
- for col_idx, th in enumerate(row_header):
- table += '%s | ' % (get_cell_classname(meta, 0, col_idx), tiny_escape(th))
- table += '
'
- table += ''
- row_idx_start = 1 if value['first_row_is_table_header'] else 0
- for row_idx, row in enumerate(data, row_idx_start):
- table += ''
- first = True
- for col_idx, col in enumerate(row):
- if value['first_col_is_header'] and first:
- table += '%s | ' % (get_cell_classname(meta, row_idx, col_idx), tiny_escape(col))
- else:
- table += '%s | ' % (get_cell_classname(meta, row_idx, col_idx), tiny_escape(col))
- first = False
- table += '
'
- table += '
'
- return table
-
-
-class TestTableBlockRenderingBase(TestCase):
-
+class TestTableBlock(TestCase):
def setUp(self):
self.default_table_options = {
'minSpareRows': 0,
@@ -81,15 +27,6 @@ class TestTableBlockRenderingBase(TestCase):
'autoColumnSize': False,
}
- self.default_value = {'first_row_is_table_header': False,
- 'first_col_is_header': False, 'data': [[None, None, None],
- [None, None, None], [None, None, None]]}
-
- self.default_expected = get_test_html_from_value(self.default_value)
-
-
-class TestTableBlock(TestTableBlockRenderingBase):
-
def test_table_block_render(self):
"""
Test a generic render.
@@ -99,12 +36,20 @@ class TestTableBlock(TestTableBlockRenderingBase):
[None, None, None]]}
block = TableBlock()
result = block.render(value)
- expected = get_test_html_from_value(value)
+ expected = """
+
+
+ Test 1 | Test 2 | Test 3 |
+ | | |
+ | | |
+
+
+ """
self.assertHTMLEqual(result, expected)
self.assertIn('Test 2', result)
- def test_table_block_aligment_render(self):
+ def test_table_block_alignment_render(self):
"""
Test a generic render with some cells aligned.
"""
@@ -115,7 +60,17 @@ class TestTableBlock(TestTableBlockRenderingBase):
[None, None, None]]}
block = TableBlock()
result = block.render(value)
- expected = get_test_html_from_value(value)
+ expected = """
+
+
+ Test 1 | Test 2 | Test 3 |
+
+
+ | | |
+ | | |
+
+
+ """
self.assertHTMLEqual(result, expected)
self.assertIn('Test 2', result)
@@ -125,8 +80,25 @@ class TestTableBlock(TestTableBlockRenderingBase):
An empty table should render okay.
"""
block = TableBlock()
- result = block.render(self.default_value)
- self.assertHTMLEqual(result, self.default_expected)
+ result = block.render({
+ 'first_row_is_table_header': False,
+ 'first_col_is_header': False,
+ 'data': [
+ [None, None, None],
+ [None, None, None],
+ [None, None, None]
+ ]
+ })
+ expected = """
+
+ """
+ self.assertHTMLEqual(result, expected)
def test_do_not_render_html(self):
"""
@@ -137,7 +109,15 @@ class TestTableBlock(TestTableBlockRenderingBase):
'data': [['Test
', None, None], [None, None, None],
[None, None, None]]}
- expected = get_test_html_from_value(value)
+ expected = """
+
+
+ <p><strong>Test</strong></p> | | |
+ | | |
+ | | |
+
+
+ """
block = TableBlock()
result = block.render(value)
@@ -150,7 +130,17 @@ class TestTableBlock(TestTableBlockRenderingBase):
value = {'first_row_is_table_header': True, 'first_col_is_header': False,
'data': [['Foo', 'Bar', 'Baz'], [None, None, None], [None, None, None]]}
- expected = get_test_html_from_value(value)
+ expected = """
+
+
+ Foo | Bar | Baz |
+
+
+ | | |
+ | | |
+
+
+ """
block = TableBlock()
result = block.render(value)
self.assertHTMLEqual(result, expected)
@@ -162,7 +152,15 @@ class TestTableBlock(TestTableBlockRenderingBase):
value = {'first_row_is_table_header': False, 'first_col_is_header': True,
'data': [['Foo', 'Bar', 'Baz'], ['one', 'two', 'three'], ['four', 'five', 'six']]}
- expected = get_test_html_from_value(value)
+ expected = """
+
+
+ Foo | Bar | Baz |
+ one | two | three |
+ four | five | six |
+
+
+ """
block = TableBlock()
result = block.render(value)
self.assertHTMLEqual(result, expected)
@@ -174,7 +172,17 @@ class TestTableBlock(TestTableBlockRenderingBase):
value = {'first_row_is_table_header': True, 'first_col_is_header': True,
'data': [['Foo', 'Bar', 'Baz'], ['one', 'two', 'three'], ['four', 'five', 'six']]}
- expected = get_test_html_from_value(value)
+ expected = """
+
+
+ Foo | Bar | Baz |
+
+
+ one | two | three |
+ four | five | six |
+
+
+ """
block = TableBlock()
result = block.render(value)
self.assertHTMLEqual(result, expected)