1- import remote from '@electron/remote' ;
21import axios from 'axios' ;
32import type { AxiosPromise , AxiosResponse } from 'axios' ;
3+ import { ipcRenderer } from 'electron' ;
44import nock from 'nock' ;
55import {
66 mockAuth ,
@@ -15,64 +15,46 @@ import type {
1515 Hostname ,
1616 Token ,
1717} from '../../types' ;
18+ import * as comms from '../../utils/comms' ;
1819import * as apiRequests from '../api/request' ;
1920import type { AuthMethod } from './types' ;
2021import * as auth from './utils' ;
2122import { getNewOAuthAppURL , getNewTokenURL } from './utils' ;
2223
23- const browserWindow = new remote . BrowserWindow ( ) ;
24-
2524describe ( 'renderer/utils/auth/utils.ts' , ( ) => {
2625 describe ( 'authGitHub' , ( ) => {
27- const loadURLMock = jest . spyOn ( browserWindow , 'loadURL' ) ;
26+ const openExternalLinkMock = jest
27+ . spyOn ( comms , 'openExternalLink' )
28+ . mockImplementation ( ) ;
2829
2930 afterEach ( ( ) => {
3031 jest . clearAllMocks ( ) ;
3132 } ) ;
3233
3334 it ( 'should call authGitHub - success' , async ( ) => {
34- // Casting to jest.Mock avoids Typescript errors, where the spy is expected to match all the original
35- // function's typing. I might fix all that if the return type of this was actually used, or if I was
36- // writing this test for a new feature. Since I'm just upgrading Jest, jest.Mock is a nice escape hatch
37- (
38- jest . spyOn ( browserWindow . webContents , 'on' ) as jest . Mock
39- ) . mockImplementation ( ( event , callback ) : void => {
40- if ( event === 'will-redirect' ) {
41- const event = new Event ( 'will-redirect' ) ;
42- callback ( event , 'https://github.com/?code=123-456' ) ;
35+ const mockIpcRendererOn = (
36+ jest . spyOn ( ipcRenderer , 'on' ) as jest . Mock
37+ ) . mockImplementation ( ( event , callback ) => {
38+ if ( event === 'gitify:auth-code' ) {
39+ callback ( null , 'auth' , '123-456' ) ;
4340 }
4441 } ) ;
4542
4643 const res = await auth . authGitHub ( ) ;
4744
48- expect ( res . authCode ) . toBe ( '123-456' ) ;
49-
50- expect (
51- browserWindow . webContents . session . clearStorageData ,
52- ) . toHaveBeenCalledTimes ( 1 ) ;
53-
54- expect ( loadURLMock ) . toHaveBeenCalledTimes ( 1 ) ;
55- expect ( loadURLMock ) . toHaveBeenCalledWith (
45+ expect ( openExternalLinkMock ) . toHaveBeenCalledTimes ( 1 ) ;
46+ expect ( openExternalLinkMock ) . toHaveBeenCalledWith (
5647 'https://github.com/login/oauth/authorize?client_id=FAKE_CLIENT_ID_123&scope=read%3Auser%2Cnotifications%2Crepo' ,
5748 ) ;
5849
59- expect ( browserWindow . destroy ) . toHaveBeenCalledTimes ( 1 ) ;
60- } ) ;
61-
62- it ( 'should call authGitHub - failure' , async ( ) => {
63- (
64- jest . spyOn ( browserWindow . webContents , 'on' ) as jest . Mock
65- ) . mockImplementation ( ( event , callback ) : void => {
66- if ( event === 'will-redirect' ) {
67- const event = new Event ( 'will-redirect' ) ;
68- callback ( event , 'https://www.github.com/?error=Oops' ) ;
69- }
70- } ) ;
71-
72- await expect ( async ( ) => await auth . authGitHub ( ) ) . rejects . toEqual (
73- "Oops! Something went wrong and we couldn't log you in using GitHub. Please try again." ,
50+ expect ( mockIpcRendererOn ) . toHaveBeenCalledTimes ( 1 ) ;
51+ expect ( mockIpcRendererOn ) . toHaveBeenCalledWith (
52+ 'gitify:auth-code' ,
53+ expect . any ( Function ) ,
7454 ) ;
75- expect ( loadURLMock ) . toHaveBeenCalledTimes ( 1 ) ;
55+
56+ expect ( res . authType ) . toBe ( 'GitHub App' ) ;
57+ expect ( res . authCode ) . toBe ( '123-456' ) ;
7658 } ) ;
7759 } ) ;
7860
0 commit comments