diff --git a/app/views/RoomView/components/LeftButtons.tsx b/app/views/RoomView/components/LeftButtons.tsx index 082a11bfc52..8525621b738 100644 --- a/app/views/RoomView/components/LeftButtons.tsx +++ b/app/views/RoomView/components/LeftButtons.tsx @@ -19,6 +19,8 @@ const styles = StyleSheet.create({ } }); +const BLANK_UNREADS_LABEL = ' '; + interface ILeftButtonsProps { rid?: string; tmid?: string; @@ -39,7 +41,7 @@ const LeftButtons = ({ rid, tmid, roomStore }: ILeftButtonsProps): ReactElement const unreadsCount = useUnreadsCount(rid); if (!isMasterDetail || tmid) { - let label: string | undefined; + let label = BLANK_UNREADS_LABEL; let marginLeft = 0; let fontSize = 0; if (unreadsCount) { diff --git a/app/views/RoomView/components/__tests__/LeftButtons.test.tsx b/app/views/RoomView/components/__tests__/LeftButtons.test.tsx index 03d27d17263..5c015efd708 100644 --- a/app/views/RoomView/components/__tests__/LeftButtons.test.tsx +++ b/app/views/RoomView/components/__tests__/LeftButtons.test.tsx @@ -1,19 +1,25 @@ import { render, screen } from '@testing-library/react-native'; +import { DefaultTheme, ThemeProvider } from '@react-navigation/native'; +import { type ReactNode } from 'react'; +import { Text } from 'react-native'; import LeftButtons from '../LeftButtons'; import { type RoomStore } from '~/views/RoomView/definitions'; jest.mock('@react-navigation/native', () => ({ + ...jest.requireActual('@react-navigation/native'), useNavigation: () => ({ navigate: jest.fn() }) })); jest.mock('~/lib/hooks/navigation', () => ({ useAppNavigation: () => ({ goBack: jest.fn() }) })); +let mockIsMasterDetail = true; jest.mock('~/lib/hooks/useMasterDetail', () => ({ - useMasterDetail: () => true + useMasterDetail: () => mockIsMasterDetail })); +let mockUnreadsCount: number | null = null; jest.mock('~/views/RoomView/hooks/useUnreadsCount', () => ({ - useUnreadsCount: () => null + useUnreadsCount: () => mockUnreadsCount })); jest.mock('~/views/RoomView/hooks/useGoRoomActionsView', () => ({ useGoRoomActionsView: () => jest.fn() @@ -40,9 +46,15 @@ jest.mock('zustand', () => ({ const roomStore = {} as RoomStore; +const NavigationThemeWrapper = ({ children }: { children: ReactNode }) => ( + {children} +); + describe('LeftButtons', () => { beforeEach(() => { mockRoomState = { room: { id: 'rid-1', rid: 'rid-1', t: 'c', name: 'general' } }; + mockIsMasterDetail = true; + mockUnreadsCount = null; }); it('reflects the current room name from the store', () => { @@ -67,4 +79,20 @@ describe('LeftButtons', () => { expect(screen.getByTestId('left-buttons-avatar')).toHaveProp('text', 'preview-room'); }); + + it('keeps the back button label elements mounted while unreads come and go', () => { + mockIsMasterDetail = false; + const { rerender } = render(, { wrapper: NavigationThemeWrapper }); + const countLabelTexts = () => screen.UNSAFE_queryAllByType(Text).length; + const labelTextsWithoutUnreads = countLabelTexts(); + + mockUnreadsCount = 3; + rerender(); + expect(screen.getAllByText('3').length).toBeGreaterThan(0); + expect(countLabelTexts()).toBe(labelTextsWithoutUnreads); + + mockUnreadsCount = 0; + rerender(); + expect(countLabelTexts()).toBe(labelTextsWithoutUnreads); + }); });