import React, { useState } from 'react'; import { fireEvent, render, renderHook } from '@testing-library/react-native'; import { useTheme } from '../../../theme'; import { Tab, Tabs } from '..'; import { getThemedStyles } from '../styles'; const onChangeMock = jest.fn(); const ControlledTabs = ({ onChangeCallback, selectedIndex, }: { onChangeCallback?: jest.Mock; selectedIndex?: number; }) => { const [index, setIndex] = useState(selectedIndex); const onChangeHandler = (nextIndex: number) => { setIndex(nextIndex); onChangeCallback?.(nextIndex); }; return ( Sign In Create Account Disabled Tab ); }; describe('Tabs', () => { beforeEach(() => { onChangeMock.mockClear(); }); it('can be used as a controlled component', () => { const { queryAllByRole } = render( ); const tabs = queryAllByRole('tab'); fireEvent.press(tabs[1]); expect(onChangeMock).toBeCalledWith(1); fireEvent.press(tabs[0]); expect(onChangeMock).toBeCalledWith(0); }); it('does not allow disabled Tabs to be selected', () => { const { queryAllByRole } = render( ); const tabs = queryAllByRole('tab'); fireEvent.press(tabs[2]); expect(onChangeMock).not.toHaveBeenCalled(); expect(tabs[2].props.accessibilityState).toHaveProperty('disabled', true); }); it('applies theme and custom styling', () => { const customStyles = { backgroundColor: 'blue' }; const { getByRole, toJSON } = render( Sign In Create Account ); expect(toJSON()).toMatchSnapshot(); const { result } = renderHook(() => useTheme()); const themedStyle = getThemedStyles(result.current); const tablist = getByRole('tablist'); expect(tablist.props.style).toStrictEqual([ themedStyle.tabList, customStyles, ]); }); });