Repository: wagtail/wagtail
File: wagtail/admin/tests/test_templatetags.py (Python)

Code before:
        soup = self.get_soup(rendered)
        invalid_icons = soup.select("ol li:not(:last-child) svg use[href='#icon-site']")
        self.assertEqual(len(invalid_icons), 0)
        icon = soup.select_one("ol li:last-child svg use[href='#icon-site']")
        self.assertIsNotNone(icon)


class DarkLightModeTest(AdminTemplateTestUtils, WagtailTestUtils, TestCase):
    fixtures = ["test.json"]

    def setUp(self):
        self.request = get_dummy_request()
        self.user = self.login()
        self.request.user = self.user

        self.profile, created = UserProfile.objects.get_or_create(user=self.user)

    def test_dark_mode(self):
        self.profile.theme = "dark"
        self.profile.save()

        template = """
            {% load wagtailadmin_tags %}
            <body class="{% admin_theme_color_scheme %}">
                {% page_breadcrumbs page 'wagtailadmin_explore' %}
            </body>
        """
        page = Page.objects.get(id=1)
        rendered = Template(template).render(
            Context({"page": page, "request": self.request})
        )

        soup = self.get_soup(rendered)
        body = soup.select_one("body")
        self.assertIsNotNone(body)
        self.assertIn("dark", body["class"])

    def test_light_mode(self):
        self.profile.theme = "light"
        self.profile.save()

        template = """
            {% load wagtailadmin_tags %}
            <body class="{% admin_theme_color_scheme %}">
                {% page_breadcrumbs page 'wagtailadmin_explore' %}
            </body>
        """
        page = Page.objects.get(id=1)
        rendered = Template(template).render(
            Context({"page": page, "request": self.request})
        )

Review comment (bug):
We should have at least three tests. 

1. Check a case where there is no theme set on the profile, it should show as our default of l'ight dark'. This one is needed. 
2. Test to be updated, Profile has light, should show as 'light dark', you are only testing that one part of the string exists, which will pass even if the logic is wrong. 
3. Test to be updated, profile has dark, shows as 'dark light', you are only testing for one part of the string currently. 

Finally, we should add at least one test for the admin skeleton where we check the meta tag is correctly included. To check for the full string of 'light dark' and the meta. You can either use soup to find the meta or just the whole string. 



Code after:
        soup = self.get_soup(rendered)
        invalid_icons = soup.select("ol li:not(:last-child) svg use[href='#icon-site']")
        self.assertEqual(len(invalid_icons), 0)
        icon = soup.select_one("ol li:last-child svg use[href='#icon-site']")
        self.assertIsNotNone(icon)


class ThemeColorSchemeTest(AdminTemplateTestUtils, WagtailTestUtils, TestCase):
    def setUp(self):
        self.request = get_dummy_request()
        self.user = self.login()
        self.request.user = self.user
        self.profile = UserProfile.get_for_user(self.user)

    def test_default_mode(self):
        template = """
            {% load wagtailadmin_tags %}
            <meta name="color-scheme" content="{% admin_theme_color_scheme %}">
        """
        rendered = Template(template).render(Context({"request": self.request}))

        soup = self.get_soup(rendered)
        meta_tag = soup.find("meta", {"name": "color-scheme"})
        self.assertIsNotNone(meta_tag)
        self.assertEqual(meta_tag["content"], "dark light")

    def test_dark_mode(self):
        self.profile.theme = "dark"
        self.profile.save()

        template = """
            {% load wagtailadmin_tags %}
            <meta name="color-scheme" content="{% admin_theme_color_scheme %}">
        """
        rendered = Template(template).render(Context({"request": self.request}))

        soup = self.get_soup(rendered)
        meta_tag = soup.find("meta", {"name": "color-scheme"})
        self.assertIsNotNone(meta_tag)
        self.assertEqual(meta_tag["content"], "dark")

    def test_light_mode(self):
        self.profile.theme = "light"
        self.profile.save()

        template = """
            {% load wagtailadmin_tags %}
            <meta name="color-scheme" content="{% admin_theme_color_scheme %}">
        """
        rendered = Template(template).render(Context({"request": self.request}))