Skip to content

Commit

Permalink
chore: send event when operating system change the colors
Browse files Browse the repository at this point in the history
related to #5914
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf committed Feb 12, 2024
1 parent 393aaf1 commit e8e4ebb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/renderer/src/lib/appearance/Appearance.spec.ts
Expand Up @@ -24,13 +24,14 @@ import Appearance from './Appearance.svelte';
import { AppearanceSettings } from '../../../../main/src/plugin/appearance-settings';

const getConfigurationValueMock = vi.fn();
const addEventListenerMock = vi.fn();

beforeEach(() => {
vi.resetAllMocks();
(window as any).getConfigurationValue = getConfigurationValueMock;
(window as any).matchMedia = vi.fn().mockReturnValue({
matches: false,
addEventListener: vi.fn(),
addEventListener: addEventListenerMock,
removeEventListener: vi.fn(),
});
});
Expand Down Expand Up @@ -113,3 +114,29 @@ test('Expect dark mode using dark configuration', async () => {
// expect to have color-scheme: dark
expect(getRootElement(container)).toHaveStyle('color-scheme: dark');
});

test('Expect event being changed when changing the default appearance on the operating system', async () => {
const spyDispatchEvent = vi.spyOn(window, 'dispatchEvent');

let userCallback: () => void = () => {};
addEventListenerMock.mockImplementation((event: string, callback: () => void) => {
if (event === 'change') {
userCallback = callback;
}
});

await awaitRender();

// check no dispatched event for now
expect(spyDispatchEvent).not.toHaveBeenCalled();

// call the callback on matchMedia
userCallback();

// check dispatched event
expect(spyDispatchEvent).toHaveBeenCalled();

// get details of the event
const event = spyDispatchEvent.mock.calls[0][0];
expect(event.type).toBe('appearance-changed');
});
2 changes: 2 additions & 0 deletions packages/renderer/src/lib/appearance/Appearance.svelte
Expand Up @@ -32,6 +32,8 @@ onMount(async () => {
// add a listener for the appearance change in case user change setting on the Operating System
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
updateAppearance();
// notify changes
window.dispatchEvent(new Event('appearance-changed'));
});
});
Expand Down

0 comments on commit e8e4ebb

Please sign in to comment.