Repository: wagtail/wagtail
File: wagtail/contrib/settings/tests/shared/test_preview.py (Python)

Code before:

        # Check the JSON response
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            response.content.decode(),
            {"is_valid": True, "is_available": True},
        )

        # Send an invalid update request
        response = self.client.post(self.preview_on_edit_url, {})
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            response.content.decode(),
            {"is_valid": False, "is_available": True},
        )

        # Check the user can still see the preview with the last valid data
        self.assertIn(self.edit_session_key, self.client.session)

        response = self.client.get(self.preview_on_edit_url)

        # Check the HTML response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "tests/previewable_setting.html")
        soup = self.get_soup(response.content)
        # This value is accessed using the default `object` context variable
        current = soup.select_one("#current-object")
        self.assertIsNotNone(current)
        self.assertEqual(current.text.strip(), f"An edited {self.verbose_name}")
        # This value is accessed using the context processor, and it's the
        # setting being previewed
        injected = soup.select_one(f"#{self.model_name}")
        self.assertIsNotNone(injected)
        self.assertEqual(injected.text.strip(), f"An edited {self.verbose_name}")
        # The other setting is accessed using the context processor and it
        # should not be affected and still have the initial value
        other_setting = soup.select_one(f"#{self.other_model._meta.model_name}")
        self.assertIsNotNone(other_setting)
        self.assertEqual(
            other_setting.text.strip(),
            f"An initial {self.other_model._meta.verbose_name}",
        )

    def test_preview_on_edit_clear_preview_data(self):
        # Set a fake preview session data for the setting
        self.client.session[self.edit_session_key] = "test data"

        response = self.client.delete(self.preview_on_edit_url)
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            response.content.decode(),

Review comment (performance):
Now that you added elements with ids, I wonder how Beautifulsoup fares in terms of performance compared to `assertContains`/`assertNotContains`/

Code after:
        response = self.client.post(self.preview_on_edit_url, self.post_data)

        # Check the JSON response
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            response.content.decode(),
            {"is_valid": True, "is_available": True},
        )

        # Send an invalid update request
        response = self.client.post(self.preview_on_edit_url, {})
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            response.content.decode(),
            {"is_valid": False, "is_available": True},
        )

        # Check the user can still see the preview with the last valid data
        self.assertIn(self.edit_session_key, self.client.session)

        response = self.client.get(self.preview_on_edit_url)

        # Check the HTML response
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "tests/previewable_setting.html")
        soup = self.get_soup(response.content)
        # This value is accessed using the default `object` context variable
        current = soup.select_one("#current-object")
        self.assertIsNotNone(current)
        self.assertEqual(current.text.strip(), f"An edited {self.verbose_name}")
        # This value is accessed using the context processor, and it's the
        # setting being previewed
        injected = soup.select_one(f"#{self.model_name}")
        self.assertIsNotNone(injected)
        self.assertEqual(injected.text.strip(), f"An edited {self.verbose_name}")
        # The other setting is accessed using the context processor and it
        # should not be affected and still have the initial value
        other_setting = soup.select_one(f"#{self.other_model._meta.model_name}")
        self.assertIsNotNone(other_setting)
        self.assertEqual(
            other_setting.text.strip(),
            f"An initial {self.other_model._meta.verbose_name}",
        )

    def test_preview_on_edit_clear_preview_data(self):
        # Set a fake preview session data for the setting
        self.client.session[self.edit_session_key] = "test data"

        response = self.client.delete(self.preview_on_edit_url)
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(