from django.conf import settings from django.test import SimpleTestCase from wagtail.rich_text import features as richtext_feature_registry from website.common.utils import ( count_words, extract_text, get_table_of_contents, heading_id, ) class TableOfContentsTestCase(SimpleTestCase): def test_creates_table_of_contents(self) -> None: toc = get_table_of_contents( """
I'm no heading
") self.assertEqual(toc, []) def test_no_content(self) -> None: toc = get_table_of_contents("") self.assertEqual(toc, []) def test_non_sequential_headings(self) -> None: toc = get_table_of_contents( """2 content
2.2 content
Hello there!
"), "Hello there!") self.assertEqual( extract_text("Paragraph 1
\nParagraph 2
"), "Paragraph 1 Paragraph 2", ) self.assertEqual( extract_text("New stuff."), "New stuff.", ) def test_plain_text(self) -> None: self.assertEqual(extract_text("Hello there!"), "Hello there!") class CountWordsTestCase(SimpleTestCase): def test_counts_words(self) -> None: self.assertEqual(count_words("a b c"), 3) self.assertEqual(count_words("Correct Horse Battery Staple"), 4) self.assertEqual(count_words("Hello there! How are you?"), 5) class RichTextFeaturesTestCase(SimpleTestCase): def test_features_exist(self) -> None: for editor, editor_config in settings.WAGTAILADMIN_RICH_TEXT_EDITORS.items(): for feature in editor_config["OPTIONS"]["features"]: with self.subTest(editor=editor, feature=feature): self.assertIsNotNone( richtext_feature_registry.get_editor_plugin("draftail", feature) ) class HeadingIDTestCase(SimpleTestCase): def test_headings(self) -> None: self.assertEqual(heading_id("123"), "ref-123") self.assertEqual(heading_id("test"), "test") self.assertEqual(heading_id("Look, a title!"), "look-a-title")