From 983dfcf69a155cddeedc85e30345487ab02d2919 Mon Sep 17 00:00:00 2001 From: David Uhlmann Date: Thu, 20 Oct 2022 03:44:44 +1000 Subject: [PATCH] fix: theming tests (#159) * Fix window.matchMedia may not exist in test environment * Fix getting of potentially more than one "Duality" text --- src/lib/themeProvider.tsx | 11 ++++++----- src/pages/App/App.test.tsx | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib/themeProvider.tsx b/src/lib/themeProvider.tsx index 2625d6aa6..3a925dbd3 100644 --- a/src/lib/themeProvider.tsx +++ b/src/lib/themeProvider.tsx @@ -55,11 +55,12 @@ export function ThemeProvider({ children }: { children: React.ReactNode }) { // preference change (if the browser settings were changed) useEffect(() => { - const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); - mediaQuery.addEventListener('change', onPreferenceChange, false); - return () => - mediaQuery.removeEventListener('change', onPreferenceChange, false); - + const mediaQuery = window.matchMedia?.('(prefers-color-scheme: dark)'); + if (mediaQuery) { + mediaQuery.addEventListener('change', onPreferenceChange, false); + return () => + mediaQuery.removeEventListener('change', onPreferenceChange, false); + } function onPreferenceChange() { setDefaultTheme(getDefaultBrowserTheme()); } diff --git a/src/pages/App/App.test.tsx b/src/pages/App/App.test.tsx index 6a28aa26c..758d600df 100644 --- a/src/pages/App/App.test.tsx +++ b/src/pages/App/App.test.tsx @@ -3,6 +3,6 @@ import App from './App'; test('renders learn react link', () => { render(); - const page = screen.getByText(/Duality/i); - expect(page).toBeInTheDocument(); + const page = screen.getAllByText(/Duality/i); + expect(page[0]).toBeInTheDocument(); });