Skip to content

Commit b18584b

Browse files
authored
refactor(state): update state storage (#1150)
1 parent c5e7aa3 commit b18584b

16 files changed

+182
-181
lines changed

src/__mocks__/state-mocks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const mockUser: GitifyUser = {
1919
id: 123456789,
2020
};
2121

22-
export const mockAccounts: AuthState = {
22+
export const mockAuth: AuthState = {
2323
token: 'token-123-456',
2424
enterpriseAccounts: mockEnterpriseAccounts,
2525
user: mockUser,

src/components/NotificationRow.test.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fireEvent, render, screen } from '@testing-library/react';
22
import { shell } from 'electron';
3-
import { mockAccounts, mockSettings } from '../__mocks__/state-mocks';
3+
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
44
import { AppContext } from '../context/App';
55
import type { UserType } from '../typesGitHub';
66
import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
@@ -131,7 +131,7 @@ describe('components/NotificationRow.tsx', () => {
131131
value={{
132132
settings: { ...mockSettings, markAsDoneOnOpen: false },
133133
removeNotificationFromState,
134-
accounts: mockAccounts,
134+
auth: mockAuth,
135135
}}
136136
>
137137
<NotificationRow {...props} />
@@ -156,7 +156,7 @@ describe('components/NotificationRow.tsx', () => {
156156
value={{
157157
settings: { ...mockSettings, markAsDoneOnOpen: false },
158158
removeNotificationFromState,
159-
accounts: mockAccounts,
159+
auth: mockAuth,
160160
}}
161161
>
162162
<NotificationRow {...props} />
@@ -181,7 +181,7 @@ describe('components/NotificationRow.tsx', () => {
181181
value={{
182182
settings: { ...mockSettings, markAsDoneOnOpen: true },
183183
markNotificationDone,
184-
accounts: mockAccounts,
184+
auth: mockAuth,
185185
}}
186186
>
187187
<NotificationRow {...props} />
@@ -205,7 +205,7 @@ describe('components/NotificationRow.tsx', () => {
205205
<AppContext.Provider
206206
value={{
207207
settings: { ...mockSettings, markAsDoneOnOpen: false },
208-
accounts: mockAccounts,
208+
auth: mockAuth,
209209
}}
210210
>
211211
<AppContext.Provider value={{ markNotificationRead }}>
@@ -230,7 +230,7 @@ describe('components/NotificationRow.tsx', () => {
230230
<AppContext.Provider
231231
value={{
232232
settings: { ...mockSettings },
233-
accounts: mockAccounts,
233+
auth: mockAuth,
234234
}}
235235
>
236236
<AppContext.Provider value={{ markNotificationDone }}>
@@ -285,7 +285,7 @@ describe('components/NotificationRow.tsx', () => {
285285
<AppContext.Provider
286286
value={{
287287
settings: { ...mockSettings },
288-
accounts: mockAccounts,
288+
auth: mockAuth,
289289
}}
290290
>
291291
<NotificationRow {...props} />

src/components/NotificationRow.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ interface IProps {
3737
export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
3838
const {
3939
settings,
40-
accounts,
40+
auth: accounts,
4141
removeNotificationFromState,
4242
markNotificationRead,
4343
markNotificationDone,

src/context/App.test.tsx

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { act, fireEvent, render, waitFor } from '@testing-library/react';
22
import { useContext } from 'react';
33

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

1616
const customRender = (
1717
ui,
18-
accounts: AuthState = mockAccounts,
18+
accounts: AuthState = mockAuth,
1919
settings: SettingsState = mockSettings,
2020
) => {
2121
return render(
22-
<AppContext.Provider value={{ accounts, settings }}>
22+
<AppContext.Provider value={{ auth: accounts, settings }}>
2323
<AppProvider>{ui}</AppProvider>
2424
</AppContext.Provider>,
2525
);
@@ -320,9 +320,9 @@ describe('context/App.tsx', () => {
320320
fireEvent.click(getByText('Test Case'));
321321
});
322322

323-
expect(saveStateMock).toHaveBeenCalledWith(
324-
{ enterpriseAccounts: [], token: null, user: null },
325-
{
323+
expect(saveStateMock).toHaveBeenCalledWith({
324+
auth: { enterpriseAccounts: [], token: null, user: null },
325+
settings: {
326326
participating: true,
327327
playSound: true,
328328
showNotifications: true,
@@ -335,7 +335,7 @@ describe('context/App.tsx', () => {
335335
showAccountHostname: false,
336336
delayNotificationState: false,
337337
},
338-
);
338+
});
339339
});
340340

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

366366
expect(setAutoLaunchMock).toHaveBeenCalledWith(true);
367367

368-
expect(saveStateMock).toHaveBeenCalledWith(
369-
{ enterpriseAccounts: [], token: null, user: null },
370-
{
368+
expect(saveStateMock).toHaveBeenCalledWith({
369+
auth: { enterpriseAccounts: [], token: null, user: null },
370+
settings: {
371371
participating: false,
372372
playSound: true,
373373
showNotifications: true,
@@ -380,6 +380,6 @@ describe('context/App.tsx', () => {
380380
showAccountHostname: false,
381381
delayNotificationState: false,
382382
},
383-
);
383+
});
384384
});
385385
});

src/context/App.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const defaultSettings: SettingsState = {
4747
};
4848

4949
interface AppContextState {
50-
accounts: AuthState;
50+
auth: AuthState;
5151
isLoggedIn: boolean;
5252
login: () => void;
5353
loginEnterprise: (data: AuthOptions) => void;
@@ -136,7 +136,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
136136

137137
const newSettings = { ...settings, [name]: value };
138138
setSettings(newSettings);
139-
saveState(accounts, newSettings);
139+
saveState({ auth: accounts, settings: newSettings });
140140
},
141141
[accounts, settings],
142142
);
@@ -152,7 +152,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
152152
const user = await getUserData(token, hostname);
153153
const updatedAccounts = addAccount(accounts, token, hostname, user);
154154
setAccounts(updatedAccounts);
155-
saveState(updatedAccounts, settings);
155+
saveState({ auth: updatedAccounts, settings });
156156
}, [accounts, settings]);
157157

158158
const loginEnterprise = useCallback(
@@ -161,7 +161,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
161161
const { token, hostname } = await getToken(authCode, authOptions);
162162
const updatedAccounts = addAccount(accounts, token, hostname);
163163
setAccounts(updatedAccounts);
164-
saveState(updatedAccounts, settings);
164+
saveState({ auth: updatedAccounts, settings });
165165
},
166166
[accounts, settings],
167167
);
@@ -173,7 +173,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
173173
const user = await getUserData(token, hostname);
174174
const updatedAccounts = addAccount(accounts, token, hostname, user);
175175
setAccounts(updatedAccounts);
176-
saveState(updatedAccounts, settings);
176+
saveState({ auth: updatedAccounts, settings });
177177
},
178178
[accounts, settings],
179179
);
@@ -186,8 +186,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
186186
const restoreSettings = useCallback(() => {
187187
const existing = loadState();
188188

189-
if (existing.accounts) {
190-
setAccounts({ ...defaultAccounts, ...existing.accounts });
189+
if (existing.auth) {
190+
setAccounts({ ...defaultAccounts, ...existing.auth });
191191
}
192192

193193
if (existing.settings) {
@@ -234,7 +234,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
234234
return (
235235
<AppContext.Provider
236236
value={{
237-
accounts,
237+
auth: accounts,
238238
isLoggedIn,
239239
login,
240240
loginEnterprise,

src/hooks/useNotifications.test.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { act, renderHook, waitFor } from '@testing-library/react';
22
import axios, { AxiosError } from 'axios';
33
import nock from 'nock';
44

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

3636
act(() => {
37-
result.current.fetchNotifications(mockAccounts, {
37+
result.current.fetchNotifications(mockAuth, {
3838
...mockSettings,
3939
detailedNotifications: false,
4040
});
@@ -87,7 +87,7 @@ describe('hooks/useNotifications.ts', () => {
8787
const { result } = renderHook(() => useNotifications());
8888

8989
act(() => {
90-
result.current.fetchNotifications(mockAccounts, mockSettings);
90+
result.current.fetchNotifications(mockAuth, mockSettings);
9191
});
9292

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

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

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

@@ -168,7 +168,7 @@ describe('hooks/useNotifications.ts', () => {
168168
describe('github.com', () => {
169169
it('should fetch notifications with success - github.com only', async () => {
170170
const accounts: AuthState = {
171-
...mockAccounts,
171+
...mockAuth,
172172
enterpriseAccounts: [],
173173
user: mockUser,
174174
};
@@ -201,7 +201,7 @@ describe('hooks/useNotifications.ts', () => {
201201

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

@@ -233,7 +233,7 @@ describe('hooks/useNotifications.ts', () => {
233233
describe('with detailed notifications', () => {
234234
it('should fetch notifications with success', async () => {
235235
const accounts: AuthState = {
236-
...mockAccounts,
236+
...mockAuth,
237237
enterpriseAccounts: [],
238238
user: mockUser,
239239
};
@@ -429,7 +429,7 @@ describe('hooks/useNotifications.ts', () => {
429429
const { result } = renderHook(() => useNotifications());
430430

431431
act(() => {
432-
result.current.fetchNotifications(mockAccounts, {
432+
result.current.fetchNotifications(mockAuth, {
433433
...mockSettings,
434434
detailedNotifications: false,
435435
});
@@ -455,7 +455,7 @@ describe('hooks/useNotifications.ts', () => {
455455
const id = 'notification-123';
456456

457457
describe('github.com', () => {
458-
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
458+
const accounts = { ...mockAuth, enterpriseAccounts: [] };
459459
const hostname = 'github.com';
460460

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

508508
describe('enterprise', () => {
509-
const accounts = { ...mockAccounts, token: null };
509+
const accounts = { ...mockAuth, token: null };
510510
const hostname = 'github.gitify.io';
511511

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

563563
describe('github.com', () => {
564-
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
564+
const accounts = { ...mockAuth, enterpriseAccounts: [] };
565565
const hostname = 'github.com';
566566

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

614614
describe('enterprise', () => {
615-
const accounts = { ...mockAccounts, token: null };
615+
const accounts = { ...mockAuth, token: null };
616616
const hostname = 'github.gitify.io';
617617

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

669669
describe('github.com', () => {
670-
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
670+
const accounts = { ...mockAuth, enterpriseAccounts: [] };
671671
const hostname = 'github.com';
672672

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

732732
describe('enterprise', () => {
733-
const accounts = { ...mockAccounts, token: null };
733+
const accounts = { ...mockAuth, token: null };
734734
const hostname = 'github.gitify.io';
735735

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

799799
describe('github.com', () => {
800-
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
800+
const accounts = { ...mockAuth, enterpriseAccounts: [] };
801801
const hostname = 'github.com';
802802

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

850850
describe('enterprise', () => {
851-
const accounts = { ...mockAccounts, token: null };
851+
const accounts = { ...mockAuth, token: null };
852852
const hostname = 'github.gitify.io';
853853

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

906906
describe('github.com', () => {
907-
const accounts = { ...mockAccounts, enterpriseAccounts: [] };
907+
const accounts = { ...mockAuth, enterpriseAccounts: [] };
908908
const hostname = 'github.com';
909909

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

957957
describe('enterprise', () => {
958-
const accounts = { ...mockAccounts, token: null };
958+
const accounts = { ...mockAuth, token: null };
959959
const hostname = 'github.gitify.io';
960960

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

0 commit comments

Comments
 (0)