Skip to content

Commit 99f40af

Browse files
committed
fix tests
rename device flow login functions Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent c55b1a9 commit 99f40af

3 files changed

Lines changed: 40 additions & 32 deletions

File tree

src/renderer/context/App.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ import {
7979
export interface AppContextState {
8080
auth: AuthState;
8181
isLoggedIn: boolean;
82-
startGitHubDeviceFlow: () => Promise<DeviceFlowSession>;
83-
pollGitHubDeviceFlow: (session: DeviceFlowSession) => Promise<Token | null>;
84-
completeGitHubDeviceLogin: (
82+
loginWithDeviceFlowStart: () => Promise<DeviceFlowSession>;
83+
loginWithDeviceFlowPoll: (
84+
session: DeviceFlowSession,
85+
) => Promise<Token | null>;
86+
loginWithDeviceFlowComplete: (
8587
token: Token,
8688
hostname?: Hostname,
8789
) => Promise<void>;
@@ -406,25 +408,31 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
406408
}, [auth]);
407409

408410
/**
409-
* Start a GitHub device flow session.
411+
* Login to GitHub Gitify OAuth App.
412+
*
413+
* Initiate device flow session.
410414
*/
411-
const startGitHubDeviceFlowWithDefaults = useCallback(
415+
const loginWithDeviceFlowStart = useCallback(
412416
async () => await startGitHubDeviceFlow(),
413417
[],
414418
);
415419

416420
/**
417-
* Poll GitHub device flow session for completion.
421+
* Login to GitHub Gitify OAuth App.
422+
*
423+
* Poll for device flow session.
418424
*/
419-
const pollGitHubDeviceFlowWithSession = useCallback(
425+
const loginWithDeviceFlowPoll = useCallback(
420426
async (session: DeviceFlowSession) => await pollGitHubDeviceFlow(session),
421427
[],
422428
);
423429

424430
/**
425-
* Persist GitHub app login after device flow completes.
431+
* Login to GitHub Gitify OAuth App.
432+
*
433+
* Finalize device flow session.
426434
*/
427-
const completeGitHubDeviceLogin = useCallback(
435+
const loginWithDeviceFlowComplete = useCallback(
428436
async (
429437
token: Token,
430438
hostname: Hostname = Constants.OAUTH_DEVICE_FLOW.hostname,
@@ -520,9 +528,9 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
520528
() => ({
521529
auth,
522530
isLoggedIn,
523-
startGitHubDeviceFlow: startGitHubDeviceFlowWithDefaults,
524-
pollGitHubDeviceFlow: pollGitHubDeviceFlowWithSession,
525-
completeGitHubDeviceLogin,
531+
loginWithDeviceFlowStart,
532+
loginWithDeviceFlowPoll,
533+
loginWithDeviceFlowComplete,
526534
loginWithOAuthApp,
527535
loginWithPersonalAccessToken,
528536
logoutFromAccount,
@@ -552,9 +560,9 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
552560
[
553561
auth,
554562
isLoggedIn,
555-
startGitHubDeviceFlowWithDefaults,
556-
pollGitHubDeviceFlowWithSession,
557-
completeGitHubDeviceLogin,
563+
loginWithDeviceFlowStart,
564+
loginWithDeviceFlowPoll,
565+
loginWithDeviceFlowComplete,
558566
loginWithOAuthApp,
559567
loginWithPersonalAccessToken,
560568
logoutFromAccount,

src/renderer/routes/LoginWithDeviceFlow.test.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('renderer/routes/LoginWithDeviceFlow.tsx', () => {
2525
});
2626

2727
it('should render and initialize device flow', async () => {
28-
const startGitHubDeviceFlowMock = jest.fn().mockResolvedValueOnce({
28+
const loginWithDeviceFlowStartMock = jest.fn().mockResolvedValueOnce({
2929
hostname: 'github.com',
3030
clientId: 'test-id',
3131
deviceCode: 'device-code',
@@ -36,10 +36,10 @@ describe('renderer/routes/LoginWithDeviceFlow.tsx', () => {
3636
});
3737

3838
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
39-
startGitHubDeviceFlow: startGitHubDeviceFlowMock,
39+
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
4040
});
4141

42-
expect(startGitHubDeviceFlowMock).toHaveBeenCalled();
42+
expect(loginWithDeviceFlowStartMock).toHaveBeenCalled();
4343

4444
await screen.findByText(/USER-1234/);
4545
expect(screen.getByText(/github.com\/login\/device/)).toBeInTheDocument();
@@ -52,7 +52,7 @@ describe('renderer/routes/LoginWithDeviceFlow.tsx', () => {
5252
});
5353

5454
it('should copy user code to clipboard when clicking copy button', async () => {
55-
const startGitHubDeviceFlowMock = jest.fn().mockResolvedValueOnce({
55+
const loginWithDeviceFlowStartMock = jest.fn().mockResolvedValueOnce({
5656
hostname: 'github.com',
5757
clientId: 'test-id',
5858
deviceCode: 'device-code',
@@ -63,7 +63,7 @@ describe('renderer/routes/LoginWithDeviceFlow.tsx', () => {
6363
});
6464

6565
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
66-
startGitHubDeviceFlow: startGitHubDeviceFlowMock,
66+
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
6767
});
6868

6969
await screen.findByText(/USER-1234/);
@@ -77,19 +77,19 @@ describe('renderer/routes/LoginWithDeviceFlow.tsx', () => {
7777
});
7878

7979
it('should handle device flow errors during initialization', async () => {
80-
const startGitHubDeviceFlowMock = jest
80+
const loginWithDeviceFlowStartMock = jest
8181
.fn()
8282
.mockRejectedValueOnce(new Error('Network error'));
8383

8484
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
85-
startGitHubDeviceFlow: startGitHubDeviceFlowMock,
85+
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
8686
});
8787

8888
await screen.findByText(/Failed to start authentication/);
8989
});
9090

9191
it('should navigate back on cancel', async () => {
92-
const startGitHubDeviceFlowMock = jest.fn().mockResolvedValueOnce({
92+
const loginWithDeviceFlowStartMock = jest.fn().mockResolvedValueOnce({
9393
hostname: 'github.com',
9494
clientId: 'test-id',
9595
deviceCode: 'device-code',
@@ -100,7 +100,7 @@ describe('renderer/routes/LoginWithDeviceFlow.tsx', () => {
100100
});
101101

102102
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
103-
startGitHubDeviceFlow: startGitHubDeviceFlowMock,
103+
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
104104
});
105105

106106
await screen.findByText(/USER-1234/);

src/renderer/routes/LoginWithDeviceFlow.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export const LoginWithDeviceFlowRoute: FC = () => {
2828
const navigate = useNavigate();
2929

3030
const {
31-
startGitHubDeviceFlow,
32-
pollGitHubDeviceFlow,
33-
completeGitHubDeviceLogin,
31+
loginWithDeviceFlowStart,
32+
loginWithDeviceFlowPoll,
33+
loginWithDeviceFlowComplete,
3434
} = useAppContext();
3535

3636
const [session, setSession] = useState<DeviceFlowSession | null>(null);
@@ -41,7 +41,7 @@ export const LoginWithDeviceFlowRoute: FC = () => {
4141
useEffect(() => {
4242
const initializeDeviceFlow = async () => {
4343
try {
44-
const newSession = await startGitHubDeviceFlow();
44+
const newSession = await loginWithDeviceFlowStart();
4545
setSession(newSession);
4646

4747
// Auto-copy the user code to clipboard
@@ -60,7 +60,7 @@ export const LoginWithDeviceFlowRoute: FC = () => {
6060
};
6161

6262
initializeDeviceFlow();
63-
}, [startGitHubDeviceFlow]);
63+
}, [loginWithDeviceFlowStart]);
6464

6565
// Poll for device flow completion
6666
useEffect(() => {
@@ -77,10 +77,10 @@ export const LoginWithDeviceFlowRoute: FC = () => {
7777

7878
try {
7979
while (isActive && Date.now() < session.expiresAt) {
80-
const token = await pollGitHubDeviceFlow(session);
80+
const token = await loginWithDeviceFlowPoll(session);
8181

8282
if (token && isActive) {
83-
await completeGitHubDeviceLogin(token, session.hostname);
83+
await loginWithDeviceFlowComplete(token, session.hostname);
8484
navigate(-1);
8585
return;
8686
}
@@ -117,7 +117,7 @@ export const LoginWithDeviceFlowRoute: FC = () => {
117117
clearTimeout(timeoutId);
118118
}
119119
};
120-
}, [session, pollGitHubDeviceFlow, completeGitHubDeviceLogin]);
120+
}, [session, loginWithDeviceFlowPoll, loginWithDeviceFlowComplete]);
121121

122122
const handleCopyUserCode = useCallback(async () => {
123123
if (session?.userCode) {

0 commit comments

Comments
 (0)