@@ -3,21 +3,14 @@ import {
33} from "../index" ;
44import * as serverModule from "../index" ;
55import fetchMock from "jest-fetch-mock" ;
6- import {
7- ClientCredentials , AccessToken ,
8- } from "simple-oauth2" ;
6+ import { ClientCredentials } from "simple-oauth2" ;
97
108describe ( "ServerClient" , ( ) => {
119 let client : ServerClient ;
1210
1311 beforeEach ( ( ) => {
14- fetchMock . resetMocks ( ) ; // Reset fetch mocks before each test
15- client = new ServerClient ( {
16- publicKey : "test-public-key" ,
17- secretKey : "test-secret-key" ,
18- oauthClientId : "test-client-id" ,
19- oauthClientSecret : "test-client-secret" ,
20- } ) ;
12+ jest . clearAllMocks ( ) ;
13+ fetchMock . resetMocks ( ) ;
2114 } ) ;
2215
2316 describe ( "createClient" , ( ) => {
@@ -48,9 +41,14 @@ describe("ServerClient", () => {
4841 headers : {
4942 "Content-Type" : "application/json" ,
5043 } ,
51- } , // Add Content-Type header
44+ } ,
5245 ) ;
5346
47+ client = new ServerClient ( {
48+ publicKey : "test-public-key" ,
49+ secretKey : "test-secret-key" ,
50+ } ) ;
51+
5452 const result = await client [ "_makeRequest" ] ( "/test-path" , {
5553 method : "GET" ,
5654 } ) ;
@@ -73,9 +71,14 @@ describe("ServerClient", () => {
7371 headers : {
7472 "Content-Type" : "application/json" ,
7573 } ,
76- } , // Add Content-Type header
74+ } ,
7775 ) ;
7876
77+ client = new ServerClient ( {
78+ publicKey : "test-public-key" ,
79+ secretKey : "test-secret-key" ,
80+ } ) ;
81+
7982 const result = await client [ "_makeRequest" ] ( "/test-path" , {
8083 method : "POST" ,
8184 body : {
@@ -108,6 +111,11 @@ describe("ServerClient", () => {
108111 } ,
109112 } ) ;
110113
114+ client = new ServerClient ( {
115+ publicKey : "test-public-key" ,
116+ secretKey : "test-secret-key" ,
117+ } ) ;
118+
111119 await expect ( client [ "_makeRequest" ] ( "/bad-path" ) ) . rejects . toThrow ( "HTTP error! status: 404" ) ;
112120 expect ( fetchMock ) . toHaveBeenCalledWith (
113121 "https://api.pipedream.com/v1/bad-path" ,
@@ -118,19 +126,29 @@ describe("ServerClient", () => {
118126
119127 describe ( "_makeApiRequest" , ( ) => {
120128 it ( "should include OAuth Authorization header and make an API request" , async ( ) => {
121- const oauthToken = {
129+ // Create a mock oauthClient
130+ const getTokenMock = jest . fn ( ) . mockResolvedValue ( {
122131 token : {
123- access_token : "oauth-token" ,
132+ access_token : "mocked- oauth-token" ,
124133 } ,
125134 expired : jest . fn ( ) . mockReturnValue ( false ) ,
126- } as unknown as AccessToken ;
127-
128- client . oauthToken = oauthToken ;
135+ } ) ;
129136
130- client . oauthClient = {
131- getToken : jest . fn ( ) . mockResolvedValue ( oauthToken ) ,
137+ const oauthClientMock = {
138+ getToken : getTokenMock ,
132139 } as unknown as ClientCredentials ;
133140
141+ // Inject the mock oauthClient into the ServerClient instance
142+ client = new ServerClient (
143+ {
144+ publicKey : "test-public-key" ,
145+ secretKey : "test-secret-key" ,
146+ oauthClientId : "test-client-id" ,
147+ oauthClientSecret : "test-client-secret" ,
148+ } ,
149+ oauthClientMock , // Inject mock oauthClient
150+ ) ;
151+
134152 fetchMock . mockResponseOnce (
135153 JSON . stringify ( {
136154 success : true ,
@@ -139,7 +157,7 @@ describe("ServerClient", () => {
139157 headers : {
140158 "Content-Type" : "application/json" ,
141159 } ,
142- } , // Add Content-Type header
160+ } ,
143161 ) ;
144162
145163 const result = await client [ "_makeApiRequest" ] ( "/test-path" ) ;
@@ -151,7 +169,7 @@ describe("ServerClient", () => {
151169 "https://api.pipedream.com/v1/test-path" ,
152170 expect . objectContaining ( {
153171 headers : expect . objectContaining ( {
154- "Authorization" : "Bearer oauth-token" ,
172+ "Authorization" : "Bearer mocked- oauth-token" ,
155173 "Content-Type" : "application/json" ,
156174 } ) ,
157175 } ) ,
@@ -170,9 +188,14 @@ describe("ServerClient", () => {
170188 headers : {
171189 "Content-Type" : "application/json" ,
172190 } ,
173- } , // Add Content-Type header
191+ } ,
174192 ) ;
175193
194+ client = new ServerClient ( {
195+ publicKey : "test-public-key" ,
196+ secretKey : "test-secret-key" ,
197+ } ) ;
198+
176199 const result = await client . connectTokenCreate ( {
177200 app_slug : "test-app" ,
178201 external_user_id : "user-id" ,
@@ -212,9 +235,14 @@ describe("ServerClient", () => {
212235 headers : {
213236 "Content-Type" : "application/json" ,
214237 } ,
215- } , // Add Content-Type header
238+ } ,
216239 ) ;
217240
241+ client = new ServerClient ( {
242+ publicKey : "test-public-key" ,
243+ secretKey : "test-secret-key" ,
244+ } ) ;
245+
218246 const result = await client . getAccounts ( {
219247 include_credentials : 1 ,
220248 } ) ;
@@ -234,24 +262,28 @@ describe("ServerClient", () => {
234262
235263 describe ( "invokeWorkflow" , ( ) => {
236264 it ( "should invoke a workflow with provided URL and body" , async ( ) => {
237- const oauthToken = "oauth-token" ;
238-
239- // Mock the OAuth token and client
240- const oauthTokenObj = {
265+ // Create a mock oauthClient
266+ const getTokenMock = jest . fn ( ) . mockResolvedValue ( {
241267 token : {
242- access_token : oauthToken ,
268+ access_token : "mocked-oauth-token" ,
243269 } ,
244270 expired : jest . fn ( ) . mockReturnValue ( false ) ,
245- } as unknown as AccessToken ;
246-
247- client . oauthToken = oauthTokenObj ;
271+ } ) ;
248272
249- client . oauthClient = {
250- getToken : jest . fn ( ) . mockResolvedValue ( oauthTokenObj ) ,
273+ const oauthClientMock = {
274+ getToken : getTokenMock ,
251275 } as unknown as ClientCredentials ;
252276
253- // Optionally, spy on the _oauthAuthorizationHeader method
254- jest . spyOn ( client , "_oauthAuthorizationHeader" ) . mockResolvedValue ( `Bearer ${ oauthToken } ` ) ;
277+ // Inject the mock oauthClient into the ServerClient instance
278+ client = new ServerClient (
279+ {
280+ publicKey : "test-public-key" ,
281+ secretKey : "test-secret-key" ,
282+ oauthClientId : "test-client-id" ,
283+ oauthClientSecret : "test-client-secret" ,
284+ } ,
285+ oauthClientMock , // Inject mock oauthClient
286+ ) ;
255287
256288 fetchMock . mockResponseOnce (
257289 JSON . stringify ( {
@@ -261,7 +293,7 @@ describe("ServerClient", () => {
261293 headers : {
262294 "Content-Type" : "application/json" ,
263295 } ,
264- } , // Add Content-Type header
296+ } ,
265297 ) ;
266298
267299 const result = await client . invokeWorkflow ( "https://example.com/workflow" , {
@@ -281,7 +313,7 @@ describe("ServerClient", () => {
281313 foo : "bar" ,
282314 } ) ,
283315 headers : expect . objectContaining ( {
284- " Authorization" : ` Bearer ${ oauthToken } ` ,
316+ Authorization : " Bearer mocked-oauth-token" ,
285317 } ) ,
286318 } ) ,
287319 ) ;
0 commit comments