-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated tests and created custom hook (#266)
* WIP : Fixing test for email verification using msw * Test : Updated test with msw * Feat : Added custom redirect hook * Feat : Utilised custom redirect hook * Chore : Updated yarn lock * Chore : Updated test file name * Chore : Removed unused dependency * Chore : Moved test file for a hook
- Loading branch information
Showing
9 changed files
with
164 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {useEffect} from 'react'; | ||
|
||
const useCountdownTimer = ( | ||
isSuccess, | ||
navigate, | ||
countdown, | ||
setCountdown, | ||
route = '/welcome', | ||
) => { | ||
useEffect(() => { | ||
let timer = null; | ||
if (isSuccess) { | ||
timer = setInterval(() => { | ||
if (countdown > 1) { | ||
setCountdown((prevCount) => prevCount - 1); | ||
} else { | ||
clearInterval(timer); | ||
navigate(route); | ||
} | ||
}, 1000); | ||
} | ||
return () => { | ||
clearInterval(timer); | ||
}; | ||
}, [isSuccess, countdown, navigate]); | ||
|
||
return countdown; | ||
}; | ||
|
||
export default useCountdownTimer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {renderHook, act} from '@testing-library/react-hooks'; | ||
import useCountdownTimer from './useCountdownTimer'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('useCountdownTimer', () => { | ||
afterEach(() => { | ||
jest.clearAllTimers(); | ||
jest.useRealTimers(); | ||
}); | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
it('should navigate to "/welcome" when countdown reaches 0', () => { | ||
const navigateMock = jest.fn(); | ||
let countdown = 3; | ||
const setCountdownMock = jest.fn().mockImplementation((value) => { | ||
countdown = value; | ||
}); | ||
const {result} = renderHook(() => | ||
useCountdownTimer(true, navigateMock, countdown, setCountdownMock), | ||
); | ||
act(() => { | ||
jest.advanceTimersByTime(3000); | ||
}); | ||
setTimeout(() => { | ||
expect(result.current).toBe(0); | ||
expect(navigateMock).toHaveBeenCalledWith('/welcome'); | ||
}, 3000); | ||
}); | ||
|
||
it('should not navigate when isSuccess is false', () => { | ||
const navigateMock = jest.fn(); | ||
let countdown = 3; | ||
const setCountdownMock = jest.fn().mockImplementation((value) => { | ||
countdown = value; | ||
}); | ||
const {result} = renderHook(() => | ||
useCountdownTimer(false, navigateMock, countdown, setCountdownMock), | ||
); | ||
act(() => { | ||
jest.advanceTimersByTime(3000); | ||
}); | ||
setTimeout(() => { | ||
expect(result.current).toBe(3); | ||
expect(navigateMock).not.toHaveBeenCalled(); | ||
}, 3000); | ||
}); | ||
|
||
it('should update countdown correctly', () => { | ||
const navigateMock = jest.fn(); | ||
let countdown = 3; | ||
const setCountdownMock = jest.fn().mockImplementation((value) => { | ||
countdown = value; | ||
}); | ||
const {result} = renderHook(() => | ||
useCountdownTimer(true, navigateMock, countdown, setCountdownMock), | ||
); | ||
act(() => { | ||
jest.advanceTimersByTime(2000); | ||
}); | ||
setTimeout(() => { | ||
expect(result.current).toBe(1); | ||
}, 2000); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import {signinHandler} from './handlers/signin-handler'; | ||
import {signupHandler} from './handlers/signup-handler'; | ||
import {emailVerificationHandler} from './handlers/emailVerification-handler'; | ||
|
||
const handlers = [...signinHandler, ...signupHandler]; | ||
const handlers = [ | ||
...signinHandler, | ||
...signupHandler, | ||
...emailVerificationHandler, | ||
]; | ||
|
||
export default handlers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {rest} from 'msw'; | ||
import {STATUS_CODES} from 'http'; | ||
|
||
export const emailVerificationHandler = [ | ||
rest.get('/api/auth/verify', (req, res, ctx) => { | ||
const {searchParams} = req.url; | ||
const token = searchParams.get('token'); | ||
if (token === 'exampleToken') { | ||
return res( | ||
ctx.status(200), | ||
ctx.json({ | ||
message: 'Verification successful', | ||
statusCode: 200, | ||
error: STATUS_CODES[200], | ||
}), | ||
); | ||
} else { | ||
return res( | ||
ctx.status(403), | ||
ctx.json({ | ||
message: 'Token Expired', | ||
statusCode: 403, | ||
error: STATUS_CODES[403], | ||
}), | ||
); | ||
} | ||
}), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters