Skip to content

Commit

Permalink
chore: adds unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreahaku committed Oct 14, 2024
1 parent c762e37 commit 9c967a8
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/selectors/hooks.test.ts
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();
});
});

0 comments on commit 9c967a8

Please sign in to comment.