Path: blob/trunk/javascript/grid-ui/src/tests/components/ThemeToggle.test.tsx
1989 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617import React from 'react'18import { render, screen, fireEvent } from '@testing-library/react'19import { ThemeToggle } from '../../components/ThemeToggle/ThemeToggle'20import { CustomThemeProvider } from '../../contexts/ThemeContext'2122const mockMatchMedia = (matches: boolean) => ({23matches,24addEventListener: jest.fn(),25removeEventListener: jest.fn()26})2728beforeEach(() => {29Object.defineProperty(window, 'matchMedia', {30writable: true,31value: jest.fn().mockImplementation(() => mockMatchMedia(false))32})33})3435it('cycles through theme modes on click', () => {36render(37<CustomThemeProvider>38<ThemeToggle />39</CustomThemeProvider>40)4142const button = screen.getByRole('button')4344// Should start with system mode (AutoMode icon)45expect(button).toHaveAttribute('aria-label', 'Toggle theme')46expect(screen.getByTestId('AutoModeIcon')).toBeInTheDocument()4748// Click to light mode49fireEvent.click(button)50expect(screen.getByTestId('LightModeIcon')).toBeInTheDocument()51expect(screen.queryByTestId('AutoModeIcon')).not.toBeInTheDocument()5253// Click to dark mode54fireEvent.click(button)55expect(screen.getByTestId('DarkModeIcon')).toBeInTheDocument()56expect(screen.queryByTestId('LightModeIcon')).not.toBeInTheDocument()5758// Click back to system mode59fireEvent.click(button)60expect(screen.getByTestId('AutoModeIcon')).toBeInTheDocument()61expect(screen.queryByTestId('DarkModeIcon')).not.toBeInTheDocument()62})6364it('responds to system preference changes', () => {65const listeners: Array<(e: any) => void> = []66const mockMediaQuery = {67matches: false,68addEventListener: jest.fn((_, handler) => listeners.push(handler)),69removeEventListener: jest.fn()70}7172window.matchMedia = jest.fn(() => mockMediaQuery)7374render(75<CustomThemeProvider>76<ThemeToggle />77</CustomThemeProvider>78)7980// Simulate system preference change81listeners.forEach(listener => listener({ matches: true }))8283expect(mockMediaQuery.addEventListener).toHaveBeenCalledWith('change', expect.any(Function))84})8586