-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated tests and created custom hook #266
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3bf4685
WIP : Fixing test for email verification using msw
joyguptaa 36056ac
Test : Updated test with msw
joyguptaa 4584e5e
Feat : Added custom redirect hook
joyguptaa 7460dc1
Feat : Utilised custom redirect hook
joyguptaa bd571f4
Merge remote-tracking branch 'origin' into test/adding-msw-to-email-v…
joyguptaa 51de9f0
Chore : Updated yarn lock
joyguptaa 21b7547
Chore : Updated test file name
joyguptaa 4e4fde1
Chore : Removed unused dependency
joyguptaa 497df4a
Chore : Moved test file for a hook
joyguptaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,66 @@ | ||
import {renderHook, act} from '@testing-library/react-hooks'; | ||
import useCountdownTimer from '../useCountdownTimer'; // Assuming this is the correct import path for your custom hook | ||
|
||
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 |
---|---|---|
@@ -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 |
---|---|---|
@@ -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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please remove this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done