From 9c967a8fd3f30daf7125ad1591998472daf05923 Mon Sep 17 00:00:00 2001 From: Andrea Salvatore Date: Mon, 14 Oct 2024 10:17:00 +0200 Subject: [PATCH] chore: adds unit tests --- app/selectors/hooks.test.ts | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/selectors/hooks.test.ts diff --git a/app/selectors/hooks.test.ts b/app/selectors/hooks.test.ts new file mode 100644 index 00000000000..a91b37efbec --- /dev/null +++ b/app/selectors/hooks.test.ts @@ -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(); + }); +});