|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (C) 2024 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + ***********************************************************************/ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | + |
| 21 | +import { render } from '@testing-library/svelte'; |
| 22 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 23 | + |
| 24 | +import Appearance from './Appearance.svelte'; |
| 25 | + |
| 26 | +const addEventListenerMock = vi.fn(); |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + vi.resetAllMocks(); |
| 30 | + window.matchMedia = vi.fn().mockReturnValue({ |
| 31 | + matches: false, |
| 32 | + addEventListener: addEventListenerMock, |
| 33 | + removeEventListener: vi.fn(), |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +function getRootElement(container: HTMLElement): HTMLElement { |
| 38 | + // get root html element |
| 39 | + let rootElement: HTMLElement | null = container; |
| 40 | + let loop = 0; |
| 41 | + while (rootElement?.parentElement && loop < 10) { |
| 42 | + rootElement = container.parentElement; |
| 43 | + loop++; |
| 44 | + } |
| 45 | + return rootElement as HTMLElement; |
| 46 | +} |
| 47 | + |
| 48 | +function getRootElementClassesValue(container: HTMLElement): string | undefined { |
| 49 | + return getRootElement(container).classList.value; |
| 50 | +} |
| 51 | + |
| 52 | +test('check initial light theme', async () => { |
| 53 | + const { baseElement } = render(Appearance, {}); |
| 54 | + // expect to have no (dark) class as OS is using light |
| 55 | + await vi.waitFor(() => expect(getRootElementClassesValue(baseElement)).toBe('')); |
| 56 | +}); |
| 57 | + |
| 58 | +test('check initial dark theme', async () => { |
| 59 | + window.matchMedia = vi.fn().mockReturnValue({ |
| 60 | + matches: true, |
| 61 | + addEventListener: addEventListenerMock, |
| 62 | + removeEventListener: vi.fn(), |
| 63 | + }); |
| 64 | + const { baseElement } = render(Appearance, {}); |
| 65 | + // expect to have no (dark) class as OS is using light |
| 66 | + await vi.waitFor(() => expect(getRootElementClassesValue(baseElement)).toBe('dark')); |
| 67 | +}); |
| 68 | + |
| 69 | +test('Expect event being changed when changing the default appearance on the operating system', async () => { |
| 70 | + // initial is dark |
| 71 | + window.matchMedia = vi.fn().mockReturnValue({ |
| 72 | + matches: true, |
| 73 | + addEventListener: addEventListenerMock, |
| 74 | + removeEventListener: vi.fn(), |
| 75 | + }); |
| 76 | + |
| 77 | + let userCallback: () => void = () => {}; |
| 78 | + addEventListenerMock.mockImplementation((event: string, callback: () => void) => { |
| 79 | + if (event === 'change') { |
| 80 | + userCallback = callback; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + const { baseElement } = render(Appearance, {}); |
| 85 | + |
| 86 | + // check it's dark |
| 87 | + expect(getRootElementClassesValue(baseElement)).toBe('dark'); |
| 88 | + |
| 89 | + // now change to light |
| 90 | + window.matchMedia = vi.fn().mockReturnValue({ |
| 91 | + matches: false, |
| 92 | + addEventListener: addEventListenerMock, |
| 93 | + removeEventListener: vi.fn(), |
| 94 | + }); |
| 95 | + |
| 96 | + // call the callback on matchMedia |
| 97 | + userCallback(); |
| 98 | + |
| 99 | + // check if it's now light |
| 100 | + await vi.waitFor(() => expect(getRootElementClassesValue(baseElement)).toBe('')); |
| 101 | + |
| 102 | + // now change to dark |
| 103 | + window.matchMedia = vi.fn().mockReturnValue({ |
| 104 | + matches: true, |
| 105 | + addEventListener: addEventListenerMock, |
| 106 | + removeEventListener: vi.fn(), |
| 107 | + }); |
| 108 | + |
| 109 | + // call again the callback on matchMedia |
| 110 | + userCallback(); |
| 111 | + |
| 112 | + // check if it's now dark |
| 113 | + await vi.waitFor(() => expect(getRootElementClassesValue(baseElement)).toBe('dark')); |
| 114 | +}); |
0 commit comments