-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c762e37
commit 9c967a8
Showing
1 changed file
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useSelector } from 'react-redux'; | ||
import { useChainId } from './hooks'; | ||
import { selectChainId } from '../selectors/networkController'; | ||
|
||
// Mock useSelector from react-redux | ||
jest.mock('react-redux', () => ({ | ||
useSelector: jest.fn(), | ||
})); | ||
|
||
// Mock the selector | ||
jest.mock('../selectors/networkController', () => ({ | ||
selectChainId: jest.fn(), | ||
})); | ||
|
||
describe('useChainId hook', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); // Clear mocks after each test to avoid test contamination | ||
}); | ||
|
||
it('should return the chain ID from the redux store', () => { | ||
const mockChainId = 1; | ||
|
||
// Mock the selector to return the mockChainId | ||
(useSelector as jest.Mock).mockImplementation((selectorFn) => { | ||
if (selectorFn === selectChainId) { | ||
return mockChainId; | ||
} | ||
}); | ||
|
||
const { result } = renderHook(() => useChainId()); | ||
|
||
expect(result.current).toBe(mockChainId); | ||
}); | ||
|
||
it('should return undefined if the chain ID is not set', () => { | ||
// Mock the selector to return undefined | ||
(useSelector as jest.Mock).mockImplementation((selectorFn) => { | ||
if (selectorFn === selectChainId) { | ||
return undefined; | ||
} | ||
}); | ||
|
||
const { result } = renderHook(() => useChainId()); | ||
|
||
expect(result.current).toBeUndefined(); | ||
}); | ||
}); |