From 54f24457d9be732b523f55607dc867e2e08f5263 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Fri, 28 Jun 2024 08:15:25 -0700 Subject: [PATCH 1/4] feat: reset to default settings --- src/context/App.tsx | 7 ++++++ src/routes/Settings.test.tsx | 23 +++++++++++++++++++ src/routes/Settings.tsx | 14 ++++++++++- .../__snapshots__/Settings.test.tsx.snap | 21 +++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/context/App.tsx b/src/context/App.tsx index 16904b58e..2f1340953 100644 --- a/src/context/App.tsx +++ b/src/context/App.tsx @@ -94,6 +94,7 @@ interface AppContextState { name: keyof SettingsState, value: boolean | Theme | string | null, ) => void; + resetSettings: () => void; } export const AppContext = createContext>({}); @@ -158,6 +159,11 @@ export const AppProvider = ({ children }: { children: ReactNode }) => { [auth, settings], ); + const resetSettings = useCallback(() => { + setSettings(defaultSettings); + saveState({ auth, settings: defaultSettings }); + }, [auth]); + const isLoggedIn = useMemo(() => { return auth.accounts.length > 0; }, [auth]); @@ -290,6 +296,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => { settings, updateSetting, + resetSettings, }} > {children} diff --git a/src/routes/Settings.test.tsx b/src/routes/Settings.test.tsx index 333cc184e..93a23827f 100644 --- a/src/routes/Settings.test.tsx +++ b/src/routes/Settings.test.tsx @@ -15,6 +15,8 @@ jest.mock('react-router-dom', () => ({ describe('routes/Settings.tsx', () => { let originalPlatform: NodeJS.Platform; const updateSetting = jest.fn(); + const resetSettings = jest.fn(); + const fetchNotifications = jest.fn(); beforeAll(() => { @@ -624,6 +626,27 @@ describe('routes/Settings.tsx', () => { ); }); + it('should reset default settings', async () => { + await act(async () => { + render( + + + + + , + ); + }); + + fireEvent.click(screen.getByTitle('Reset default settings')); + expect(resetSettings).toHaveBeenCalled(); + }); + it('should open account management', async () => { await act(async () => { render( diff --git a/src/routes/Settings.tsx b/src/routes/Settings.tsx index fada57163..b6d3ce558 100644 --- a/src/routes/Settings.tsx +++ b/src/routes/Settings.tsx @@ -5,6 +5,7 @@ import { MilestoneIcon, PersonIcon, TagIcon, + UndoIcon, XCircleIcon, } from '@primer/octicons-react'; import { ipcRenderer } from 'electron'; @@ -32,7 +33,7 @@ import { isLinux, isMacOS } from '../utils/platform'; import { setTheme } from '../utils/theme'; export const SettingsRoute: FC = () => { - const { settings, updateSetting } = useContext(AppContext); + const { settings, updateSetting, resetSettings } = useContext(AppContext); const navigate = useNavigate(); const [appVersion, setAppVersion] = useState(null); @@ -302,6 +303,17 @@ export const SettingsRoute: FC = () => { Gitify {appVersion}
+ +
+ + ); + }; + + const { getByText } = customRender(); + + act(() => { + fireEvent.click(getByText('Test Case')); + }); + + expect(saveStateMock).toHaveBeenCalledWith({ + auth: { + accounts: [], + enterpriseAccounts: [], + token: null, + user: null, + } as AuthState, + settings: defaultSettings, + }); + }); }); }); From db14e66906c58c8f6d72b7528cc5de2fe57e5aa6 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Wed, 3 Jul 2024 18:15:29 -0800 Subject: [PATCH 3/4] add confirm dialog --- .../settings/SettingsFooter.test.tsx | 27 ++++++++++++++++++- src/components/settings/SettingsFooter.tsx | 3 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/components/settings/SettingsFooter.test.tsx b/src/components/settings/SettingsFooter.test.tsx index 1defda81a..b4738754e 100644 --- a/src/components/settings/SettingsFooter.test.tsx +++ b/src/components/settings/SettingsFooter.test.tsx @@ -106,7 +106,9 @@ describe('routes/components/SettingsFooter.tsx', () => { ); }); - it('should reset default settings', async () => { + it('should reset default settings when `OK`', async () => { + window.confirm = jest.fn(() => true); // always click 'OK' + await act(async () => { render( { expect(resetSettings).toHaveBeenCalled(); }); + it('should skip reset default settings when `cancelled`', async () => { + window.confirm = jest.fn(() => false); // always click 'cancel' + + await act(async () => { + render( + + + + + , + ); + }); + + fireEvent.click(screen.getByTitle('Reset default settings')); + expect(resetSettings).not.toHaveBeenCalled(); + }); + it('should open account management', async () => { await act(async () => { render( diff --git a/src/components/settings/SettingsFooter.tsx b/src/components/settings/SettingsFooter.tsx index 1b6652da7..1386c21a3 100644 --- a/src/components/settings/SettingsFooter.tsx +++ b/src/components/settings/SettingsFooter.tsx @@ -39,7 +39,8 @@ export const SettingsFooter: FC = () => { className={BUTTON_CLASS_NAME} title="Reset default settings" onClick={() => { - resetSettings(); + confirm('Are you sure you want to reset all settings?') && + resetSettings(); }} > From c88c564a609184038bb72f007c24dd61c6dc571e Mon Sep 17 00:00:00 2001 From: Afonso Jorge Ramos Date: Fri, 5 Jul 2024 18:55:46 +0100 Subject: [PATCH 4/4] chore: test reset settings text at the end of settings --- .../settings/AppearanceSettings.tsx | 2 +- .../settings/NotificationSettings.tsx | 2 +- .../settings/SettingsFooter.test.tsx | 48 ------------------- src/components/settings/SettingsFooter.tsx | 17 +------ src/components/settings/SystemSettings.tsx | 2 +- src/routes/Settings.test.tsx | 47 ++++++++++++++++++ src/routes/Settings.tsx | 16 ++++++- .../__snapshots__/Settings.test.tsx.snap | 41 ++++------------ 8 files changed, 76 insertions(+), 99 deletions(-) diff --git a/src/components/settings/AppearanceSettings.tsx b/src/components/settings/AppearanceSettings.tsx index 4efd91340..8c6825e43 100644 --- a/src/components/settings/AppearanceSettings.tsx +++ b/src/components/settings/AppearanceSettings.tsx @@ -25,7 +25,7 @@ export const AppearanceSettings: FC = () => { }, [settings.theme]); return ( -
+
Appearance diff --git a/src/components/settings/NotificationSettings.tsx b/src/components/settings/NotificationSettings.tsx index c0880c223..2913c4fee 100644 --- a/src/components/settings/NotificationSettings.tsx +++ b/src/components/settings/NotificationSettings.tsx @@ -9,7 +9,7 @@ export const NotificationSettings: FC = () => { const { settings, updateSetting } = useContext(AppContext); return ( -
+
Notifications diff --git a/src/components/settings/SettingsFooter.test.tsx b/src/components/settings/SettingsFooter.test.tsx index b4738754e..546c75997 100644 --- a/src/components/settings/SettingsFooter.test.tsx +++ b/src/components/settings/SettingsFooter.test.tsx @@ -12,8 +12,6 @@ jest.mock('react-router-dom', () => ({ })); describe('routes/components/SettingsFooter.tsx', () => { - const resetSettings = jest.fn(); - afterEach(() => { jest.clearAllMocks(); }); @@ -106,52 +104,6 @@ describe('routes/components/SettingsFooter.tsx', () => { ); }); - it('should reset default settings when `OK`', async () => { - window.confirm = jest.fn(() => true); // always click 'OK' - - await act(async () => { - render( - - - - - , - ); - }); - - fireEvent.click(screen.getByTitle('Reset default settings')); - expect(resetSettings).toHaveBeenCalled(); - }); - - it('should skip reset default settings when `cancelled`', async () => { - window.confirm = jest.fn(() => false); // always click 'cancel' - - await act(async () => { - render( - - - - - , - ); - }); - - fireEvent.click(screen.getByTitle('Reset default settings')); - expect(resetSettings).not.toHaveBeenCalled(); - }); - it('should open account management', async () => { await act(async () => { render( diff --git a/src/components/settings/SettingsFooter.tsx b/src/components/settings/SettingsFooter.tsx index 1386c21a3..54d4c4708 100644 --- a/src/components/settings/SettingsFooter.tsx +++ b/src/components/settings/SettingsFooter.tsx @@ -1,14 +1,12 @@ -import { PersonIcon, UndoIcon, XCircleIcon } from '@primer/octicons-react'; -import { type FC, useContext, useEffect, useState } from 'react'; +import { PersonIcon, XCircleIcon } from '@primer/octicons-react'; +import { type FC, useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; -import { AppContext } from '../../context/App'; import { BUTTON_CLASS_NAME } from '../../styles/gitify'; import { Size } from '../../types'; import { getAppVersion, quitApp } from '../../utils/comms'; import { openGitifyReleaseNotes } from '../../utils/links'; export const SettingsFooter: FC = () => { - const { resetSettings } = useContext(AppContext); const [appVersion, setAppVersion] = useState(null); const navigate = useNavigate(); @@ -34,17 +32,6 @@ export const SettingsFooter: FC = () => { Gitify {appVersion}
-
diff --git a/src/routes/__snapshots__/Settings.test.tsx.snap b/src/routes/__snapshots__/Settings.test.tsx.snap index 86ba7d5c0..f52fe8198 100644 --- a/src/routes/__snapshots__/Settings.test.tsx.snap +++ b/src/routes/__snapshots__/Settings.test.tsx.snap @@ -36,11 +36,9 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = `
-
+
-
+
-
+
+
-