11const _ = require ( "lodash" ) ;
2- const fetch = require ( "node-fetch" ) ;
3-
42const eventObj = require ( "./modules/event.json" ) ;
53const { onExecutePostLogin } = require ( "../actions/activateNewUsersInCIS.js" ) ;
64
7- // Mock the node-fetch module
8- jest . mock ( "node-fetch" ) ;
9-
105// Take all log enteries and combine them into a single array
116const combineLog = ( consoleLogs ) => {
127 let combinedLog = [ ] ;
@@ -124,11 +119,13 @@ beforeEach(() => {
124119 } ) ;
125120 }
126121 } ;
122+ fetchSpy = jest . spyOn ( global , "fetch" ) . mockImplementation ( fetchRespCallback ) ;
127123} ) ;
128124
129125afterEach ( ( ) => {
130126 // Clean up after each test
131127 jest . clearAllMocks ( ) ;
128+ fetchSpy . mockRestore ( ) ;
132129 consoleLogSpy . mockRestore ( ) ;
133130 consoleWarnSpy . mockRestore ( ) ;
134131 consoleErrorSpy . mockRestore ( ) ;
@@ -150,9 +147,6 @@ test("Expect onExecutePostLogin to be defined", async () => {
150147} ) ;
151148
152149test ( "When connection does not match, expect empty logs and workflow to continue" , async ( ) => {
153- // Mock Fetch
154- fetch . mockImplementation ( fetchRespCallback ) ;
155-
156150 // Set connection name to a connection other than WHITELISTED_CONNECTIONS
157151 _event . connection . name = "non_whitelisted_provider" ;
158152
@@ -175,9 +169,6 @@ test("When connection does not match, expect empty logs and workflow to continue
175169} ) ;
176170
177171test ( "When existsInCIS is already set, expect empty logs and workflow to continue" , async ( ) => {
178- // Mock Fetch
179- fetch . mockImplementation ( fetchRespCallback ) ;
180-
181172 // Set existsInCIS in user metadata
182173 _event . user . app_metadata = { existsInCIS : true } ;
183174
@@ -244,9 +235,6 @@ test("When failing to get beartoken, expect error logged and workflow to continu
244235} ) ;
245236
246237test ( "When failing to get profile, expect error logged and workflow to continue" , async ( ) => {
247- // Mock Fetch
248- fetch . mockImplementation ( fetchRespCallback ) ;
249-
250238 // Set token and person fetch responses
251239 tokenResp = { access_token : "fakefakefakefake" } ;
252240 personResponseStatusOk = false ;
@@ -272,9 +260,6 @@ test("When failing to get profile, expect error logged and workflow to continue"
272260} ) ;
273261
274262test ( "When change API fails, expect error logged and workflow to continue" , async ( ) => {
275- // Mock Fetch
276- fetch . mockImplementation ( fetchRespCallback ) ;
277-
278263 // Set token, person and change responses
279264 tokenResp = { access_token : "fakefakefakefake" } ;
280265 personResp = { } ;
@@ -302,9 +287,6 @@ test("When change API fails, expect error logged and workflow to continue", asyn
302287test . each ( WHITELISTED_CONNECTIONS ) (
303288 "When new user with %s connection is created, expect it logged and workflow to continue" ,
304289 async ( connection ) => {
305- // Mock Fetch
306- fetch . mockImplementation ( fetchRespCallback ) ;
307-
308290 // Set token, person and change responses
309291 tokenResp = { access_token : "fakefakefakefake" } ;
310292 personResp = { } ;
@@ -339,9 +321,6 @@ test.each(WHITELISTED_CONNECTIONS)(
339321) ;
340322
341323test ( "When person API profile exists, expect set existsInCIS" , async ( ) => {
342- // Mock Fetch
343- fetch . mockImplementation ( fetchRespCallback ) ;
344-
345324 // Set token and person fetch responses
346325 tokenResp = { access_token : "fakefakefakefake" } ;
347326 personResp = { usernames : { values : { "HACK#GITHUB" : "jdoegithub" } } } ;
0 commit comments