-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/no ref/tests for trainers #140
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
784e356
feat: tests for hooks of trainer-add
unkosan f688a21
fix: testing-library/react for hooks
unkosan 166142b
feat: added test for trainer-home
unkosan 1be760c
fix: testing-library/react for hooks of trainer-home
unkosan 1f7b3ef
fix: applied suggestion from coderabbit
unkosan 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 hidden or 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
274 changes: 274 additions & 0 deletions
274
frontend/components/trainer-add/navigator-preprocess/hooks/__tests__/use-pagenation.test.tsx
This file contains hidden or 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,274 @@ | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { usePreprocessSelexData } from '../use-pagenation'; | ||
| import { useRouter } from 'next/router'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { preprocessSelexData } from '../../../redux/selex-data'; | ||
| import { clearPreprocessingDirty } from '../../../redux/preprocessing-config'; | ||
|
|
||
| // Mock the dependencies | ||
| jest.mock('next/router', () => ({ | ||
| useRouter: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('react-redux', () => ({ | ||
| useDispatch: jest.fn(), | ||
| useSelector: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../../redux/selex-data', () => ({ | ||
| preprocessSelexData: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../../redux/preprocessing-config', () => ({ | ||
| clearPreprocessingDirty: jest.fn(), | ||
| })); | ||
|
|
||
| describe('usePreprocessSelexData', () => { | ||
| // Setup common mocks | ||
| const mockPush = jest.fn(); | ||
| const mockDispatch = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| // Mock router | ||
| (useRouter as jest.Mock).mockReturnValue({ | ||
| push: mockPush, | ||
| }); | ||
|
|
||
| // Mock dispatch | ||
| (useDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
|
|
||
| // Mock successful dispatch for async actions | ||
| mockDispatch.mockImplementation((action) => { | ||
| if (typeof action === 'function') { | ||
| return Promise.resolve(); | ||
| } | ||
| return action; | ||
| }); | ||
| }); | ||
|
|
||
| it('should return the correct initial values', () => { | ||
| // Mock selector values | ||
| (useSelector as jest.Mock).mockImplementation((selector) => { | ||
| // Simulate the Redux state | ||
| const state = { | ||
| preprocessingConfig: { | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| isDirty: false, | ||
| isValidParams: true, | ||
| }, | ||
| pageConfig: { | ||
| experimentName: 'Test Experiment', | ||
| }, | ||
| }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => usePreprocessSelexData()); | ||
|
|
||
| expect(result.current.isLoading).toBe(false); | ||
| expect(typeof result.current.handleClickNext).toBe('function'); | ||
| expect(typeof result.current.handleClickBack).toBe('function'); | ||
| expect(result.current.canProceed).toBe(true); | ||
| }); | ||
|
|
||
| it('should navigate back when handleClickBack is called', () => { | ||
| // Mock selector values | ||
| (useSelector as jest.Mock).mockImplementation((selector) => { | ||
| // Simulate the Redux state | ||
| const state = { | ||
| preprocessingConfig: { | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| isDirty: false, | ||
| isValidParams: true, | ||
| }, | ||
| pageConfig: { | ||
| experimentName: 'Test Experiment', | ||
| }, | ||
| }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => usePreprocessSelexData()); | ||
|
|
||
| act(() => { | ||
| result.current.handleClickBack(); | ||
| }); | ||
|
|
||
| expect(mockPush).toHaveBeenCalledWith('/trainer'); | ||
| }); | ||
|
|
||
| it('should navigate to next page without preprocessing when isDirty is false', async () => { | ||
| // Mock selector values | ||
| (useSelector as jest.Mock).mockImplementation((selector) => { | ||
| // Simulate the Redux state | ||
| const state = { | ||
| preprocessingConfig: { | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| isDirty: false, | ||
| isValidParams: true, | ||
| }, | ||
| pageConfig: { | ||
| experimentName: 'Test Experiment', | ||
| }, | ||
| }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => usePreprocessSelexData()); | ||
|
|
||
| await act(async () => { | ||
| await result.current.handleClickNext(); | ||
| }); | ||
|
|
||
| expect(mockPush).toHaveBeenCalledWith('?page=raptgen'); | ||
| expect(mockDispatch).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should preprocess data and navigate to next page when isDirty is true', async () => { | ||
| // Mock selector values | ||
| (useSelector as jest.Mock).mockImplementation((selector) => { | ||
| // Simulate the Redux state | ||
| const state = { | ||
| preprocessingConfig: { | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| isDirty: true, | ||
| isValidParams: true, | ||
| }, | ||
| pageConfig: { | ||
| experimentName: 'Test Experiment', | ||
| }, | ||
| }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| // Mock the action creators | ||
| (preprocessSelexData as unknown as jest.Mock).mockReturnValue({ type: 'PREPROCESS_SELEX_DATA' }); | ||
| (clearPreprocessingDirty as unknown as jest.Mock).mockReturnValue({ type: 'CLEAR_PREPROCESSING_DIRTY' }); | ||
|
|
||
| const { result } = renderHook(() => usePreprocessSelexData()); | ||
|
|
||
| await act(async () => { | ||
| await result.current.handleClickNext(); | ||
| }); | ||
|
|
||
| expect(preprocessSelexData).toHaveBeenCalledWith({ | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| }); | ||
|
|
||
| expect(clearPreprocessingDirty).toHaveBeenCalled(); | ||
| expect(mockPush).toHaveBeenCalledWith('?page=raptgen'); | ||
| }); | ||
|
|
||
| it('should handle errors during preprocessing', async () => { | ||
| // Mock selector values | ||
| (useSelector as jest.Mock).mockImplementation((selector) => { | ||
| // Simulate the Redux state | ||
| const state = { | ||
| preprocessingConfig: { | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| isDirty: true, | ||
| isValidParams: true, | ||
| }, | ||
| pageConfig: { | ||
| experimentName: 'Test Experiment', | ||
| }, | ||
| }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| // Mock error in dispatch | ||
| const mockError = new Error('Preprocessing failed'); | ||
| mockDispatch.mockRejectedValueOnce(mockError); | ||
|
|
||
| // Spy on console.error | ||
| jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
|
||
| const { result } = renderHook(() => usePreprocessSelexData()); | ||
|
|
||
| await act(async () => { | ||
| await result.current.handleClickNext(); | ||
| }); | ||
|
|
||
unkosan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expect(console.error).toHaveBeenCalledWith(mockError); | ||
| expect(result.current.isLoading).toBe(false); | ||
| expect(mockPush).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should disable proceeding when params are invalid', () => { | ||
| // Mock selector values | ||
| (useSelector as jest.Mock).mockImplementation((selector) => { | ||
| // Simulate the Redux state | ||
| const state = { | ||
| preprocessingConfig: { | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| isDirty: false, | ||
| isValidParams: false, | ||
| }, | ||
| pageConfig: { | ||
| experimentName: 'Test Experiment', | ||
| }, | ||
| }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => usePreprocessSelexData()); | ||
|
|
||
| expect(result.current.canProceed).toBe(false); | ||
| }); | ||
|
|
||
| it('should disable proceeding when experiment name is missing', () => { | ||
| // Mock selector values | ||
| (useSelector as jest.Mock).mockImplementation((selector) => { | ||
| // Simulate the Redux state | ||
| const state = { | ||
| preprocessingConfig: { | ||
| forwardAdapter: 'ACGT', | ||
| reverseAdapter: 'TGCA', | ||
| targetLength: 30, | ||
| tolerance: 2, | ||
| minCount: 10, | ||
| isDirty: false, | ||
| isValidParams: true, | ||
| }, | ||
| pageConfig: { | ||
| experimentName: '', | ||
| }, | ||
| }; | ||
| return selector(state); | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => usePreprocessSelexData()); | ||
|
|
||
| expect(result.current.canProceed).toBe(false); | ||
| }); | ||
| }); | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.