Skip to content

Commit 43c3df0

Browse files
committed
Refactor to use globalThis for matchMedia across theme helpers and tests
1 parent 0fc78c0 commit 43c3df0

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

frontend/src/Context/Theme/helpers/getTokensByMode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const getTokensByMode = (mode: 'light' | 'dark') => {
1717
let key = originalKey;
1818

1919
if (key.includes('-')) {
20-
key = key.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
20+
key = key.replaceAll(/-([a-z])/g, (_, char) => char.toUpperCase());
2121
}
2222

2323
acc[key as ColorKey] = colors[originalKey as keyof typeof colors].value;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function isDarkMode(): boolean {
2-
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
2+
return globalThis.matchMedia && globalThis.matchMedia('(prefers-color-scheme: dark)').matches;
33
}
44

55
export default isDarkMode;

frontend/src/Context/Theme/hooks/useSynchronizeThemeAndMedia.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const useSynchronizeThemeAndMedia = ({
1111
const modeRef = useStableRef(() => (isDarkMode() ? 'dark' : 'light'), [{}]);
1212

1313
useEffect(() => {
14-
const isMatchMediaSupported = !!window.matchMedia;
14+
const isMatchMediaSupported = !!globalThis.matchMedia;
1515

1616
if (!isMatchMediaSupported) {
1717
return;
@@ -30,7 +30,7 @@ const useSynchronizeThemeAndMedia = ({
3030

3131
const subscribeToMediaChanges = () => {
3232
const abort = new AbortController();
33-
const media = window.matchMedia('(prefers-color-scheme: dark)');
33+
const media = globalThis.matchMedia('(prefers-color-scheme: dark)');
3434

3535
media.addEventListener('change', toggleTheme);
3636

frontend/src/components/GoodBye/GoodbyeMessage/GoodbyeMessage.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ const matchMediaCommon = {
2626
};
2727

2828
describe('GoodbyeMessage', () => {
29-
const originalMatchMedia = window.matchMedia;
29+
const originalMatchMedia = globalThis.matchMedia;
3030

3131
beforeEach(() => {
3232
(useNavigate as Mock).mockReturnValue(mockNavigate);
3333
});
3434

3535
afterAll(() => {
36-
window.matchMedia = originalMatchMedia;
36+
globalThis.matchMedia = originalMatchMedia;
3737
});
3838

3939
it('renders the header', () => {
@@ -70,7 +70,7 @@ describe('GoodbyeMessage', () => {
7070
});
7171

7272
it('renders correctly on screen less than SMALL_VIEWPORT', () => {
73-
window.matchMedia = vi.fn().mockImplementation((query) => ({
73+
globalThis.matchMedia = vi.fn().mockImplementation((query) => ({
7474
matches: new RegExp(`\\(max-width:\\s*${SMALL_VIEWPORT}px\\)`).test(query),
7575
media: query,
7676
...matchMediaCommon,
@@ -88,7 +88,7 @@ describe('GoodbyeMessage', () => {
8888
});
8989

9090
it('renders correctly on screen greater than SMALL_VIEWPORT', () => {
91-
window.matchMedia = vi.fn().mockImplementation((query) => ({
91+
globalThis.matchMedia = vi.fn().mockImplementation((query) => ({
9292
matches: new RegExp(`\\(min-width:\\s*${SMALL_VIEWPORT + 1}px\\)`).test(query),
9393
media: query,
9494
...matchMediaCommon,

frontend/src/hooks/tests/useIsSmallViewport.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ const matchMediaCommon = {
1212
};
1313

1414
describe('useIsSmallViewport', () => {
15-
const originalMatchMedia = window.matchMedia;
15+
const originalMatchMedia = globalThis.matchMedia;
1616

1717
beforeEach(() => {
18-
window.matchMedia = vi.fn().mockImplementation((query) => ({
18+
globalThis.matchMedia = vi.fn().mockImplementation((query) => ({
1919
matches: new RegExp(`\\(max-width:\\s*${SMALL_VIEWPORT + 1}px\\)`).test(query),
2020
media: query,
2121
...matchMediaCommon,
2222
}));
2323
});
2424

2525
afterAll(() => {
26-
window.matchMedia = originalMatchMedia;
26+
globalThis.matchMedia = originalMatchMedia;
2727
});
2828

2929
it('should return false when window width is greater than 768px', () => {
@@ -33,7 +33,7 @@ describe('useIsSmallViewport', () => {
3333
});
3434

3535
it('should return true when window width is less than or equal to 768px', () => {
36-
window.matchMedia = vi.fn().mockImplementation((query) => ({
36+
globalThis.matchMedia = vi.fn().mockImplementation((query) => ({
3737
matches: new RegExp(`\\(max-width:\\s*${SMALL_VIEWPORT}px\\)`).test(query),
3838
media: query,
3939
...matchMediaCommon,

frontend/src/hooks/tests/useIsTabletViewport.spec.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ const matchMediaCommon = {
1212
};
1313

1414
describe('useIsTabletViewport', () => {
15-
const originalMatchMedia = window.matchMedia;
15+
const originalMatchMedia = globalThis.matchMedia;
1616

1717
beforeEach(() => {
18-
window.matchMedia = vi.fn().mockImplementation((query) => ({
18+
globalThis.matchMedia = vi.fn().mockImplementation((query) => ({
1919
matches: new RegExp(`\\(max-width:\\s*${TABLET_VIEWPORT + 1}px\\)`).test(query),
2020
media: query,
2121
...matchMediaCommon,
2222
}));
2323
});
2424

2525
afterAll(() => {
26-
window.matchMedia = originalMatchMedia;
26+
globalThis.matchMedia = originalMatchMedia;
2727
});
2828

29-
it('should return false when window width is greater than 899px', () => {
29+
it('should return false when globalThis width is greater than 899px', () => {
3030
const { result } = renderHook(() => useIsTabletViewport());
3131

3232
expect(result.current).toBe(false);
3333
});
3434

35-
it('should return true when window width is less than or equal to 899px', () => {
36-
window.matchMedia = vi.fn().mockImplementation((query) => ({
35+
it('should return true when globalThis width is less than or equal to 899px', () => {
36+
globalThis.matchMedia = vi.fn().mockImplementation((query) => ({
3737
matches: new RegExp(`\\(max-width:\\s*${TABLET_VIEWPORT}px\\)`).test(query),
3838
media: query,
3939
...matchMediaCommon,

0 commit comments

Comments
 (0)