33 mockGitHubCloudAccount ,
44 mockGitHubEnterpriseServerAccount ,
55} from '../../__mocks__/account-mocks' ;
6- import { mockToken } from '../../__mocks__/state-mocks' ;
76
87import * as comms from '../comms' ;
98import { clearOctokitClientCache , createOctokitClient } from './octokit' ;
@@ -24,43 +23,99 @@ describe('renderer/utils/api/octokit.ts', () => {
2423 } ) ;
2524
2625 describe ( 'createOctokitClient' , ( ) => {
27- it ( 'should create octokit client for GitHub Cloud' , async ( ) => {
26+ it ( 'should create octokit rest client for GitHub Cloud' , async ( ) => {
2827 const getGitHubAPIBaseUrlSpy = jest . spyOn ( utils , 'getGitHubAPIBaseUrl' ) ;
2928 getGitHubAPIBaseUrlSpy . mockReturnValue (
3029 new URL ( 'https://api.github.com/' ) ,
3130 ) ;
3231
33- const octokit = await createOctokitClient ( mockGitHubCloudAccount ) ;
32+ const octokit = await createOctokitClient ( mockGitHubCloudAccount , 'rest' ) ;
3433
35- expect ( getGitHubAPIBaseUrlSpy ) . toHaveBeenCalledWith ( 'github.com' ) ;
34+ expect ( getGitHubAPIBaseUrlSpy ) . toHaveBeenCalledWith ( 'github.com' , 'rest' ) ;
3635 expect ( octokit ) . toBeDefined ( ) ;
37- expect ( mockDecryptValue ) . toHaveBeenCalledWith ( mockToken ) ;
36+ expect ( mockDecryptValue ) . toHaveBeenCalledWith (
37+ mockGitHubCloudAccount . token ,
38+ ) ;
39+ } ) ;
40+
41+ it ( 'should create octokit graphql client for GitHub Cloud' , async ( ) => {
42+ const getGitHubAPIBaseUrlSpy = jest . spyOn ( utils , 'getGitHubAPIBaseUrl' ) ;
43+ getGitHubAPIBaseUrlSpy . mockReturnValue (
44+ new URL ( 'https://api.github.com/' ) ,
45+ ) ;
46+
47+ const octokit = await createOctokitClient (
48+ mockGitHubCloudAccount ,
49+ 'graphql' ,
50+ ) ;
51+
52+ expect ( getGitHubAPIBaseUrlSpy ) . toHaveBeenCalledWith (
53+ 'github.com' ,
54+ 'graphql' ,
55+ ) ;
56+ expect ( octokit ) . toBeDefined ( ) ;
57+ expect ( mockDecryptValue ) . toHaveBeenCalledWith (
58+ mockGitHubCloudAccount . token ,
59+ ) ;
3860 } ) ;
3961
40- it ( 'should create octokit client for GitHub Enterprise Server' , async ( ) => {
62+ it ( 'should create octokit rest client for GitHub Enterprise Server' , async ( ) => {
4163 const getGitHubAPIBaseUrlSpy = jest . spyOn ( utils , 'getGitHubAPIBaseUrl' ) ;
4264 getGitHubAPIBaseUrlSpy . mockReturnValue (
4365 new URL ( 'https://github.gitify.io/api/v3/' ) ,
4466 ) ;
4567
4668 const octokit = await createOctokitClient (
4769 mockGitHubEnterpriseServerAccount ,
70+ 'rest' ,
71+ ) ;
72+
73+ expect ( getGitHubAPIBaseUrlSpy ) . toHaveBeenCalledWith (
74+ 'github.gitify.io' ,
75+ 'rest' ,
76+ ) ;
77+ expect ( octokit ) . toBeDefined ( ) ;
78+ expect ( mockDecryptValue ) . toHaveBeenCalledWith (
79+ mockGitHubEnterpriseServerAccount . token ,
80+ ) ;
81+ } ) ;
82+
83+ it ( 'should create octokit graphql client for GitHub Enterprise Server' , async ( ) => {
84+ const getGitHubAPIBaseUrlSpy = jest . spyOn ( utils , 'getGitHubAPIBaseUrl' ) ;
85+ getGitHubAPIBaseUrlSpy . mockReturnValue (
86+ new URL ( 'https://github.gitify.io/api/graphql/' ) ,
87+ ) ;
88+
89+ const octokit = await createOctokitClient (
90+ mockGitHubEnterpriseServerAccount ,
91+ 'graphql' ,
4892 ) ;
4993
50- expect ( getGitHubAPIBaseUrlSpy ) . toHaveBeenCalledWith ( 'github.gitify.io' ) ;
94+ expect ( getGitHubAPIBaseUrlSpy ) . toHaveBeenCalledWith (
95+ 'github.gitify.io' ,
96+ 'graphql' ,
97+ ) ;
5198 expect ( octokit ) . toBeDefined ( ) ;
52- expect ( mockDecryptValue ) . toHaveBeenCalledWith ( mockToken ) ;
99+ expect ( mockDecryptValue ) . toHaveBeenCalledWith (
100+ mockGitHubEnterpriseServerAccount . token ,
101+ ) ;
53102 } ) ;
54103
55- it ( 'should cache and reuse octokit clients for the same account' , async ( ) => {
104+ it ( 'should cache and reuse octokit clients for the same account and api type ' , async ( ) => {
56105 const getGitHubAPIBaseUrlSpy = jest . spyOn ( utils , 'getGitHubAPIBaseUrl' ) ;
57106 getGitHubAPIBaseUrlSpy . mockReturnValue (
58107 new URL ( 'https://api.github.com/' ) ,
59108 ) ;
60109
61- const octokit1 = await createOctokitClient ( mockGitHubCloudAccount ) ;
110+ const octokit1 = await createOctokitClient (
111+ mockGitHubCloudAccount ,
112+ 'rest' ,
113+ ) ;
62114
63- const octokit2 = await createOctokitClient ( mockGitHubCloudAccount ) ;
115+ const octokit2 = await createOctokitClient (
116+ mockGitHubCloudAccount ,
117+ 'rest' ,
118+ ) ;
64119
65120 // Should return the same instance
66121 expect ( octokit1 ) . toBe ( octokit2 ) ;
@@ -72,15 +127,41 @@ describe('renderer/utils/api/octokit.ts', () => {
72127 expect ( getGitHubAPIBaseUrlSpy ) . toHaveBeenCalledTimes ( 1 ) ;
73128 } ) ;
74129
75- it ( 'should create different clients for different accounts' , async ( ) => {
130+ it ( 'should create different clients for different accounts with same api type' , async ( ) => {
131+ const getGitHubAPIBaseUrlSpy = jest . spyOn ( utils , 'getGitHubAPIBaseUrl' ) ;
132+ getGitHubAPIBaseUrlSpy . mockReturnValue (
133+ new URL ( 'https://api.github.com/' ) ,
134+ ) ;
135+
136+ const octokit1 = await createOctokitClient ( mockGitHubAppAccount , 'rest' ) ;
137+
138+ const octokit2 = await createOctokitClient (
139+ mockGitHubCloudAccount ,
140+ 'rest' ,
141+ ) ;
142+
143+ // Should be different instances for different tokens
144+ expect ( octokit1 ) . not . toBe ( octokit2 ) ;
145+
146+ // Should decrypt both tokens
147+ expect ( mockDecryptValue ) . toHaveBeenCalledTimes ( 2 ) ;
148+ } ) ;
149+
150+ it ( 'should create different clients for same accounts with different api type' , async ( ) => {
76151 const getGitHubAPIBaseUrlSpy = jest . spyOn ( utils , 'getGitHubAPIBaseUrl' ) ;
77152 getGitHubAPIBaseUrlSpy . mockReturnValue (
78153 new URL ( 'https://api.github.com/' ) ,
79154 ) ;
80155
81- const octokit1 = await createOctokitClient ( mockGitHubAppAccount ) ;
156+ const octokit1 = await createOctokitClient (
157+ mockGitHubCloudAccount ,
158+ 'rest' ,
159+ ) ;
82160
83- const octokit2 = await createOctokitClient ( mockGitHubCloudAccount ) ;
161+ const octokit2 = await createOctokitClient (
162+ mockGitHubCloudAccount ,
163+ 'graphql' ,
164+ ) ;
84165
85166 // Should be different instances for different tokens
86167 expect ( octokit1 ) . not . toBe ( octokit2 ) ;
0 commit comments