Skip to content

Commit

Permalink
refactor(state): update state storage (#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy authored May 28, 2024
1 parent c5e7aa3 commit b18584b
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 181 deletions.
2 changes: 1 addition & 1 deletion src/__mocks__/state-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const mockUser: GitifyUser = {
id: 123456789,
};

export const mockAccounts: AuthState = {
export const mockAuth: AuthState = {
token: 'token-123-456',
enterpriseAccounts: mockEnterpriseAccounts,
user: mockUser,
Expand Down
14 changes: 7 additions & 7 deletions src/components/NotificationRow.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { shell } from 'electron';
import { mockAccounts, mockSettings } from '../__mocks__/state-mocks';
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
import { AppContext } from '../context/App';
import type { UserType } from '../typesGitHub';
import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('components/NotificationRow.tsx', () => {
value={{
settings: { ...mockSettings, markAsDoneOnOpen: false },
removeNotificationFromState,
accounts: mockAccounts,
auth: mockAuth,
}}
>
<NotificationRow {...props} />
Expand All @@ -156,7 +156,7 @@ describe('components/NotificationRow.tsx', () => {
value={{
settings: { ...mockSettings, markAsDoneOnOpen: false },
removeNotificationFromState,
accounts: mockAccounts,
auth: mockAuth,
}}
>
<NotificationRow {...props} />
Expand All @@ -181,7 +181,7 @@ describe('components/NotificationRow.tsx', () => {
value={{
settings: { ...mockSettings, markAsDoneOnOpen: true },
markNotificationDone,
accounts: mockAccounts,
auth: mockAuth,
}}
>
<NotificationRow {...props} />
Expand All @@ -205,7 +205,7 @@ describe('components/NotificationRow.tsx', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings, markAsDoneOnOpen: false },
accounts: mockAccounts,
auth: mockAuth,
}}
>
<AppContext.Provider value={{ markNotificationRead }}>
Expand All @@ -230,7 +230,7 @@ describe('components/NotificationRow.tsx', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings },
accounts: mockAccounts,
auth: mockAuth,
}}
>
<AppContext.Provider value={{ markNotificationDone }}>
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('components/NotificationRow.tsx', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings },
accounts: mockAccounts,
auth: mockAuth,
}}
>
<NotificationRow {...props} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface IProps {
export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
const {
settings,
accounts,
auth: accounts,
removeNotificationFromState,
markNotificationRead,
markNotificationDone,
Expand Down
22 changes: 11 additions & 11 deletions src/context/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { act, fireEvent, render, waitFor } from '@testing-library/react';
import { useContext } from 'react';

import { mockAccounts, mockSettings } from '../__mocks__/state-mocks';
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
import { useNotifications } from '../hooks/useNotifications';
import type { AuthState, SettingsState } from '../types';
import * as apiRequests from '../utils/api/request';
Expand All @@ -15,11 +15,11 @@ jest.mock('../hooks/useNotifications');

const customRender = (
ui,
accounts: AuthState = mockAccounts,
accounts: AuthState = mockAuth,
settings: SettingsState = mockSettings,
) => {
return render(
<AppContext.Provider value={{ accounts, settings }}>
<AppContext.Provider value={{ auth: accounts, settings }}>
<AppProvider>{ui}</AppProvider>
</AppContext.Provider>,
);
Expand Down Expand Up @@ -320,9 +320,9 @@ describe('context/App.tsx', () => {
fireEvent.click(getByText('Test Case'));
});

expect(saveStateMock).toHaveBeenCalledWith(
{ enterpriseAccounts: [], token: null, user: null },
{
expect(saveStateMock).toHaveBeenCalledWith({
auth: { enterpriseAccounts: [], token: null, user: null },
settings: {
participating: true,
playSound: true,
showNotifications: true,
Expand All @@ -335,7 +335,7 @@ describe('context/App.tsx', () => {
showAccountHostname: false,
delayNotificationState: false,
},
);
});
});

it('should call updateSetting and set auto launch(openAtStartup)', async () => {
Expand Down Expand Up @@ -365,9 +365,9 @@ describe('context/App.tsx', () => {

expect(setAutoLaunchMock).toHaveBeenCalledWith(true);

expect(saveStateMock).toHaveBeenCalledWith(
{ enterpriseAccounts: [], token: null, user: null },
{
expect(saveStateMock).toHaveBeenCalledWith({
auth: { enterpriseAccounts: [], token: null, user: null },
settings: {
participating: false,
playSound: true,
showNotifications: true,
Expand All @@ -380,6 +380,6 @@ describe('context/App.tsx', () => {
showAccountHostname: false,
delayNotificationState: false,
},
);
});
});
});
16 changes: 8 additions & 8 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const defaultSettings: SettingsState = {
};

interface AppContextState {
accounts: AuthState;
auth: AuthState;
isLoggedIn: boolean;
login: () => void;
loginEnterprise: (data: AuthOptions) => void;
Expand Down Expand Up @@ -136,7 +136,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {

const newSettings = { ...settings, [name]: value };
setSettings(newSettings);
saveState(accounts, newSettings);
saveState({ auth: accounts, settings: newSettings });
},
[accounts, settings],
);
Expand All @@ -152,7 +152,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
const user = await getUserData(token, hostname);
const updatedAccounts = addAccount(accounts, token, hostname, user);
setAccounts(updatedAccounts);
saveState(updatedAccounts, settings);
saveState({ auth: updatedAccounts, settings });
}, [accounts, settings]);

const loginEnterprise = useCallback(
Expand All @@ -161,7 +161,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
const { token, hostname } = await getToken(authCode, authOptions);
const updatedAccounts = addAccount(accounts, token, hostname);
setAccounts(updatedAccounts);
saveState(updatedAccounts, settings);
saveState({ auth: updatedAccounts, settings });
},
[accounts, settings],
);
Expand All @@ -173,7 +173,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
const user = await getUserData(token, hostname);
const updatedAccounts = addAccount(accounts, token, hostname, user);
setAccounts(updatedAccounts);
saveState(updatedAccounts, settings);
saveState({ auth: updatedAccounts, settings });
},
[accounts, settings],
);
Expand All @@ -186,8 +186,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
const restoreSettings = useCallback(() => {
const existing = loadState();

if (existing.accounts) {
setAccounts({ ...defaultAccounts, ...existing.accounts });
if (existing.auth) {
setAccounts({ ...defaultAccounts, ...existing.auth });
}

if (existing.settings) {
Expand Down Expand Up @@ -234,7 +234,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
return (
<AppContext.Provider
value={{
accounts,
auth: accounts,
isLoggedIn,
login,
loginEnterprise,
Expand Down
38 changes: 19 additions & 19 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { act, renderHook, waitFor } from '@testing-library/react';
import axios, { AxiosError } from 'axios';
import nock from 'nock';

import { mockAccounts, mockSettings, mockUser } from '../__mocks__/state-mocks';
import { mockAuth, mockSettings, mockUser } from '../__mocks__/state-mocks';
import type { AuthState } from '../types';
import { mockNotificationUser } from '../utils/api/__mocks__/response-mocks';
import { Errors } from '../utils/constants';
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(mockAccounts, {
result.current.fetchNotifications(mockAuth, {
...mockSettings,
detailedNotifications: false,
});
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(mockAccounts, mockSettings);
result.current.fetchNotifications(mockAuth, mockSettings);
});

expect(result.current.status).toBe('loading');
Expand All @@ -103,7 +103,7 @@ describe('hooks/useNotifications.ts', () => {
describe('enterprise', () => {
it('should fetch notifications with success - enterprise only', async () => {
const accounts: AuthState = {
...mockAccounts,
...mockAuth,
token: null,
};

Expand Down Expand Up @@ -137,7 +137,7 @@ describe('hooks/useNotifications.ts', () => {

it('should fetch notifications with failure - enterprise only', async () => {
const accounts: AuthState = {
...mockAccounts,
...mockAuth,
token: null,
};

Expand Down Expand Up @@ -168,7 +168,7 @@ describe('hooks/useNotifications.ts', () => {
describe('github.com', () => {
it('should fetch notifications with success - github.com only', async () => {
const accounts: AuthState = {
...mockAccounts,
...mockAuth,
enterpriseAccounts: [],
user: mockUser,
};
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('hooks/useNotifications.ts', () => {

it('should fetch notifications with failures - github.com only', async () => {
const accounts: AuthState = {
...mockAccounts,
...mockAuth,
enterpriseAccounts: [],
};

Expand Down Expand Up @@ -233,7 +233,7 @@ describe('hooks/useNotifications.ts', () => {
describe('with detailed notifications', () => {
it('should fetch notifications with success', async () => {
const accounts: AuthState = {
...mockAccounts,
...mockAuth,
enterpriseAccounts: [],
user: mockUser,
};
Expand Down Expand Up @@ -429,7 +429,7 @@ describe('hooks/useNotifications.ts', () => {
const { result } = renderHook(() => useNotifications());

act(() => {
result.current.fetchNotifications(mockAccounts, {
result.current.fetchNotifications(mockAuth, {
...mockSettings,
detailedNotifications: false,
});
Expand All @@ -455,7 +455,7 @@ describe('hooks/useNotifications.ts', () => {
const id = 'notification-123';

describe('github.com', () => {
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
const accounts = { ...mockAuth, enterpriseAccounts: [] };
const hostname = 'github.com';

it('should mark a notification as read with success - github.com', async () => {
Expand Down Expand Up @@ -506,7 +506,7 @@ describe('hooks/useNotifications.ts', () => {
});

describe('enterprise', () => {
const accounts = { ...mockAccounts, token: null };
const accounts = { ...mockAuth, token: null };
const hostname = 'github.gitify.io';

it('should mark a notification as read with success - enterprise', async () => {
Expand Down Expand Up @@ -561,7 +561,7 @@ describe('hooks/useNotifications.ts', () => {
const id = 'notification-123';

describe('github.com', () => {
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
const accounts = { ...mockAuth, enterpriseAccounts: [] };
const hostname = 'github.com';

it('should mark a notification as done with success - github.com', async () => {
Expand Down Expand Up @@ -612,7 +612,7 @@ describe('hooks/useNotifications.ts', () => {
});

describe('enterprise', () => {
const accounts = { ...mockAccounts, token: null };
const accounts = { ...mockAuth, token: null };
const hostname = 'github.gitify.io';

it('should mark a notification as done with success - enterprise', async () => {
Expand Down Expand Up @@ -667,7 +667,7 @@ describe('hooks/useNotifications.ts', () => {
const id = 'notification-123';

describe('github.com', () => {
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
const accounts = { ...mockAuth, enterpriseAccounts: [] };
const hostname = 'github.com';

it('should unsubscribe from a notification with success - github.com', async () => {
Expand Down Expand Up @@ -730,7 +730,7 @@ describe('hooks/useNotifications.ts', () => {
});

describe('enterprise', () => {
const accounts = { ...mockAccounts, token: null };
const accounts = { ...mockAuth, token: null };
const hostname = 'github.gitify.io';

it('should unsubscribe from a notification with success - enterprise', async () => {
Expand Down Expand Up @@ -797,7 +797,7 @@ describe('hooks/useNotifications.ts', () => {
const repoSlug = 'gitify-app/notifications-test';

describe('github.com', () => {
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
const accounts = { ...mockAuth, enterpriseAccounts: [] };
const hostname = 'github.com';

it("should mark a repository's notifications as read with success - github.com", async () => {
Expand Down Expand Up @@ -848,7 +848,7 @@ describe('hooks/useNotifications.ts', () => {
});

describe('enterprise', () => {
const accounts = { ...mockAccounts, token: null };
const accounts = { ...mockAuth, token: null };
const hostname = 'github.gitify.io';

it("should mark a repository's notifications as read with success - enterprise", async () => {
Expand Down Expand Up @@ -904,7 +904,7 @@ describe('hooks/useNotifications.ts', () => {
const id = 'notification-123';

describe('github.com', () => {
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
const accounts = { ...mockAuth, enterpriseAccounts: [] };
const hostname = 'github.com';

it("should mark a repository's notifications as done with success - github.com", async () => {
Expand Down Expand Up @@ -955,7 +955,7 @@ describe('hooks/useNotifications.ts', () => {
});

describe('enterprise', () => {
const accounts = { ...mockAccounts, token: null };
const accounts = { ...mockAuth, token: null };
const hostname = 'github.gitify.io';

it("should mark a repository's notifications as done with success - enterprise", async () => {
Expand Down
Loading

0 comments on commit b18584b

Please sign in to comment.