diff --git a/lms/djangoapps/discussion/rest_api/serializers.py b/lms/djangoapps/discussion/rest_api/serializers.py index f90a137d769e..6e17f201d0f9 100644 --- a/lms/djangoapps/discussion/rest_api/serializers.py +++ b/lms/djangoapps/discussion/rest_api/serializers.py @@ -402,10 +402,9 @@ def get_unread_comment_count(self, obj): def get_preview_body(self, obj): """ - Returns a cleaned and truncated version of the thread's body to display in a - preview capacity. + Returns a cleaned version of the thread's body to display in a preview capacity. """ - return strip_tags(self.get_rendered_body(obj)).replace('\n', ' ') + return strip_tags(self.get_rendered_body(obj)).replace('\n', ' ').replace(' ', ' ') def get_close_reason(self, obj): """ diff --git a/lms/djangoapps/discussion/rest_api/tests/test_serializers.py b/lms/djangoapps/discussion/rest_api/tests/test_serializers.py index 6e7f02b55316..bd9cb0f1eace 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_serializers.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_serializers.py @@ -242,6 +242,21 @@ def test_response_count_missing(self): serialized = self.serialize(thread_data) assert 'response_count' not in serialized + def test_get_preview_body(self): + """ + Test for the 'get_preview_body' method. + + This test verifies that the 'get_preview_body' method returns a cleaned + version of the thread's body that is suitable for display as a preview. + The test specifically focuses on handling the presence of multiple + spaces within the body. + """ + thread_data = self.make_cs_content( + {"body": "
This is a test thread body with some text.
"} + ) + serialized = self.serialize(thread_data) + assert serialized['preview_body'] == "This is a test thread body with some text." + @ddt.ddt class CommentSerializerTest(SerializerTestMixin, SharedModuleStoreTestCase):