Skip to content

Commit 4de1d91

Browse files
authored
refactor: replace accounts with auth for AuthState (#1154)
1 parent ce8d6ba commit 4de1d91

File tree

9 files changed

+84
-93
lines changed

9 files changed

+84
-93
lines changed

src/context/App.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ jest.mock('../hooks/useNotifications');
1515

1616
const customRender = (
1717
ui,
18-
accounts: AuthState = mockAuth,
18+
auth: AuthState = mockAuth,
1919
settings: SettingsState = mockSettings,
2020
) => {
2121
return render(
22-
<AppContext.Provider value={{ auth: accounts, settings }}>
22+
<AppContext.Provider value={{ auth, settings }}>
2323
<AppProvider>{ui}</AppProvider>
2424
</AppContext.Provider>,
2525
);

src/context/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { getNotificationCount } from '../utils/notifications';
2626
import { clearState, loadState, saveState } from '../utils/storage';
2727
import { setTheme } from '../utils/theme';
2828

29-
const defaultAccounts: AuthState = {
29+
const defaultAuth: AuthState = {
3030
token: null,
3131
enterpriseAccounts: [],
3232
user: null,
@@ -79,7 +79,7 @@ interface AppContextState {
7979
export const AppContext = createContext<Partial<AppContextState>>({});
8080

8181
export const AppProvider = ({ children }: { children: ReactNode }) => {
82-
const [accounts, setAccounts] = useState<AuthState>(defaultAccounts);
82+
const [accounts, setAccounts] = useState<AuthState>(defaultAuth);
8383
const [settings, setSettings] = useState<SettingsState>(defaultSettings);
8484
const {
8585
fetchNotifications,
@@ -179,15 +179,15 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
179179
);
180180

181181
const logout = useCallback(() => {
182-
setAccounts(defaultAccounts);
182+
setAccounts(defaultAuth);
183183
clearState();
184184
}, []);
185185

186186
const restoreSettings = useCallback(() => {
187187
const existing = loadState();
188188

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

193193
if (existing.settings) {

src/hooks/useNotifications.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('hooks/useNotifications.ts', () => {
102102

103103
describe('enterprise', () => {
104104
it('should fetch notifications with success - enterprise only', async () => {
105-
const accounts: AuthState = {
105+
const auth: AuthState = {
106106
...mockAuth,
107107
token: null,
108108
};
@@ -119,7 +119,7 @@ describe('hooks/useNotifications.ts', () => {
119119
const { result } = renderHook(() => useNotifications());
120120

121121
act(() => {
122-
result.current.fetchNotifications(accounts, {
122+
result.current.fetchNotifications(auth, {
123123
...mockSettings,
124124
detailedNotifications: false,
125125
});
@@ -136,7 +136,7 @@ describe('hooks/useNotifications.ts', () => {
136136
});
137137

138138
it('should fetch notifications with failure - enterprise only', async () => {
139-
const accounts: AuthState = {
139+
const auth: AuthState = {
140140
...mockAuth,
141141
token: null,
142142
};
@@ -156,7 +156,7 @@ describe('hooks/useNotifications.ts', () => {
156156
const { result } = renderHook(() => useNotifications());
157157

158158
act(() => {
159-
result.current.fetchNotifications(accounts, mockSettings);
159+
result.current.fetchNotifications(auth, mockSettings);
160160
});
161161

162162
await waitFor(() => {
@@ -167,7 +167,7 @@ describe('hooks/useNotifications.ts', () => {
167167

168168
describe('github.com', () => {
169169
it('should fetch notifications with success - github.com only', async () => {
170-
const accounts: AuthState = {
170+
const auth: AuthState = {
171171
...mockAuth,
172172
enterpriseAccounts: [],
173173
user: mockUser,
@@ -185,7 +185,7 @@ describe('hooks/useNotifications.ts', () => {
185185
const { result } = renderHook(() => useNotifications());
186186

187187
act(() => {
188-
result.current.fetchNotifications(accounts, {
188+
result.current.fetchNotifications(auth, {
189189
...mockSettings,
190190
detailedNotifications: false,
191191
});
@@ -200,7 +200,7 @@ describe('hooks/useNotifications.ts', () => {
200200
});
201201

202202
it('should fetch notifications with failures - github.com only', async () => {
203-
const accounts: AuthState = {
203+
const auth: AuthState = {
204204
...mockAuth,
205205
enterpriseAccounts: [],
206206
};
@@ -220,7 +220,7 @@ describe('hooks/useNotifications.ts', () => {
220220
const { result } = renderHook(() => useNotifications());
221221

222222
act(() => {
223-
result.current.fetchNotifications(accounts, mockSettings);
223+
result.current.fetchNotifications(auth, mockSettings);
224224
});
225225

226226
await waitFor(() => {
@@ -232,7 +232,7 @@ describe('hooks/useNotifications.ts', () => {
232232

233233
describe('with detailed notifications', () => {
234234
it('should fetch notifications with success', async () => {
235-
const accounts: AuthState = {
235+
const auth: AuthState = {
236236
...mockAuth,
237237
enterpriseAccounts: [],
238238
user: mockUser,
@@ -393,7 +393,7 @@ describe('hooks/useNotifications.ts', () => {
393393
const { result } = renderHook(() => useNotifications());
394394

395395
act(() => {
396-
result.current.fetchNotifications(accounts, {
396+
result.current.fetchNotifications(auth, {
397397
...mockSettings,
398398
detailedNotifications: true,
399399
});

src/hooks/useNotifications.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,35 @@ interface NotificationsState {
3030
hostname: string,
3131
) => void;
3232
fetchNotifications: (
33-
accounts: AuthState,
33+
auth: AuthState,
3434
settings: SettingsState,
3535
) => Promise<void>;
3636
markNotificationRead: (
37-
accounts: AuthState,
37+
auth: AuthState,
3838
settings: SettingsState,
3939
id: string,
4040
hostname: string,
4141
) => Promise<void>;
4242
markNotificationDone: (
43-
accounts: AuthState,
43+
auth: AuthState,
4444
settings: SettingsState,
4545
id: string,
4646
hostname: string,
4747
) => Promise<void>;
4848
unsubscribeNotification: (
49-
accounts: AuthState,
49+
auth: AuthState,
5050
settings: SettingsState,
5151
id: string,
5252
hostname: string,
5353
) => Promise<void>;
5454
markRepoNotifications: (
55-
accounts: AuthState,
55+
auth: AuthState,
5656
settings: SettingsState,
5757
repoSlug: string,
5858
hostname: string,
5959
) => Promise<void>;
6060
markRepoNotificationsDone: (
61-
accounts: AuthState,
61+
auth: AuthState,
6262
settings: SettingsState,
6363
repoSlug: string,
6464
hostname: string,
@@ -76,21 +76,18 @@ export const useNotifications = (): NotificationsState => {
7676
);
7777

7878
const fetchNotifications = useCallback(
79-
async (accounts: AuthState, settings: SettingsState) => {
79+
async (auth: AuthState, settings: SettingsState) => {
8080
setStatus('loading');
8181

8282
try {
83-
const fetchedNotifications = await getAllNotifications(
84-
accounts,
85-
settings,
86-
);
83+
const fetchedNotifications = await getAllNotifications(auth, settings);
8784

8885
setNotifications(fetchedNotifications);
8986
triggerNativeNotifications(
9087
notifications,
9188
fetchedNotifications,
9289
settings,
93-
accounts,
90+
auth,
9491
);
9592
setStatus('success');
9693
} catch (err) {
@@ -103,14 +100,14 @@ export const useNotifications = (): NotificationsState => {
103100

104101
const markNotificationRead = useCallback(
105102
async (
106-
accounts: AuthState,
103+
auth: AuthState,
107104
settings: SettingsState,
108105
id: string,
109106
hostname: string,
110107
) => {
111108
setStatus('loading');
112109

113-
const token = getTokenForHost(hostname, accounts);
110+
const token = getTokenForHost(hostname, auth);
114111

115112
try {
116113
await markNotificationThreadAsRead(id, hostname, token);
@@ -134,14 +131,14 @@ export const useNotifications = (): NotificationsState => {
134131

135132
const markNotificationDone = useCallback(
136133
async (
137-
accounts: AuthState,
134+
auth: AuthState,
138135
settings: SettingsState,
139136
id: string,
140137
hostname: string,
141138
) => {
142139
setStatus('loading');
143140

144-
const token = getTokenForHost(hostname, accounts);
141+
const token = getTokenForHost(hostname, auth);
145142

146143
try {
147144
await markNotificationThreadAsDone(id, hostname, token);
@@ -165,18 +162,18 @@ export const useNotifications = (): NotificationsState => {
165162

166163
const unsubscribeNotification = useCallback(
167164
async (
168-
accounts: AuthState,
165+
auth: AuthState,
169166
settings: SettingsState,
170167
id: string,
171168
hostname: string,
172169
) => {
173170
setStatus('loading');
174171

175-
const token = getTokenForHost(hostname, accounts);
172+
const token = getTokenForHost(hostname, auth);
176173

177174
try {
178175
await ignoreNotificationThreadSubscription(id, hostname, token);
179-
await markNotificationRead(accounts, settings, id, hostname);
176+
await markNotificationRead(auth, settings, id, hostname);
180177
setStatus('success');
181178
} catch (err) {
182179
setStatus('success');
@@ -187,14 +184,14 @@ export const useNotifications = (): NotificationsState => {
187184

188185
const markRepoNotifications = useCallback(
189186
async (
190-
accounts: AuthState,
187+
auth: AuthState,
191188
settings: SettingsState,
192189
repoSlug: string,
193190
hostname: string,
194191
) => {
195192
setStatus('loading');
196193

197-
const token = getTokenForHost(hostname, accounts);
194+
const token = getTokenForHost(hostname, auth);
198195

199196
try {
200197
await markRepositoryNotificationsAsRead(repoSlug, hostname, token);
@@ -216,7 +213,7 @@ export const useNotifications = (): NotificationsState => {
216213

217214
const markRepoNotificationsDone = useCallback(
218215
async (
219-
accounts: AuthState,
216+
auth: AuthState,
220217
settings: SettingsState,
221218
repoSlug: string,
222219
hostname: string,
@@ -238,7 +235,7 @@ export const useNotifications = (): NotificationsState => {
238235
await Promise.all(
239236
notificationsToRemove.map((notification) =>
240237
markNotificationDone(
241-
accounts,
238+
auth,
242239
settings,
243240
notification.id,
244241
notifications[accountIndex].hostname,

src/routes/LoginWithOAuthApp.test.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jest.mock('react-router-dom', () => ({
1515
describe('routes/LoginWithOAuthApp.tsx', () => {
1616
const openExternalMock = jest.spyOn(shell, 'openExternal');
1717

18-
const mockAccounts: AuthState = {
18+
const mockAuth: AuthState = {
1919
enterpriseAccounts: [],
2020
user: null,
2121
};
@@ -29,7 +29,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
2929

3030
it('renders correctly', () => {
3131
const tree = render(
32-
<AppContext.Provider value={{ auth: mockAccounts }}>
32+
<AppContext.Provider value={{ auth: mockAuth }}>
3333
<MemoryRouter>
3434
<LoginWithOAuthApp />
3535
</MemoryRouter>
@@ -41,7 +41,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
4141

4242
it('let us go back', () => {
4343
render(
44-
<AppContext.Provider value={{ auth: mockAccounts }}>
44+
<AppContext.Provider value={{ auth: mockAuth }}>
4545
<MemoryRouter>
4646
<LoginWithOAuthApp />
4747
</MemoryRouter>
@@ -80,7 +80,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
8080
describe("'Create new OAuth App' button", () => {
8181
it('should be disabled if no hostname configured', async () => {
8282
render(
83-
<AppContext.Provider value={{ auth: mockAccounts }}>
83+
<AppContext.Provider value={{ auth: mockAuth }}>
8484
<MemoryRouter>
8585
<LoginWithOAuthApp />
8686
</MemoryRouter>
@@ -94,7 +94,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
9494

9595
it('should open in browser if hostname configured', async () => {
9696
render(
97-
<AppContext.Provider value={{ auth: mockAccounts }}>
97+
<AppContext.Provider value={{ auth: mockAuth }}>
9898
<MemoryRouter>
9999
<LoginWithOAuthApp />
100100
</MemoryRouter>
@@ -113,7 +113,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
113113

114114
it('should receive a logged-in enterprise account', () => {
115115
const { rerender } = render(
116-
<AppContext.Provider value={{ auth: mockAccounts }}>
116+
<AppContext.Provider value={{ auth: mockAuth }}>
117117
<MemoryRouter>
118118
<LoginWithOAuthApp />
119119
</MemoryRouter>
@@ -142,7 +142,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
142142

143143
it('should render the form with errors', () => {
144144
render(
145-
<AppContext.Provider value={{ auth: mockAccounts }}>
145+
<AppContext.Provider value={{ auth: mockAuth }}>
146146
<MemoryRouter>
147147
<LoginWithOAuthApp />
148148
</MemoryRouter>
@@ -168,7 +168,7 @@ describe('routes/LoginWithOAuthApp.tsx', () => {
168168

169169
it('should open help docs in the browser', async () => {
170170
render(
171-
<AppContext.Provider value={{ auth: mockAccounts }}>
171+
<AppContext.Provider value={{ auth: mockAuth }}>
172172
<MemoryRouter>
173173
<LoginWithOAuthApp />
174174
</MemoryRouter>

0 commit comments

Comments
 (0)