|
| 1 | +import { type ReactNode } from 'react'; |
| 2 | +import { render } from '@testing-library/react-native'; |
| 3 | +import { Provider } from 'react-redux'; |
| 4 | + |
| 5 | +import { mockedStore } from '../../reducers/mockedStore'; |
| 6 | +import { setActiveUsers } from '../../actions/activeUsers'; |
| 7 | +import { connectSuccess, connectRequest, disconnect } from '../../actions/connect'; |
| 8 | +import { initStore } from '../../lib/store/auxStore'; |
| 9 | +import { selectServerSuccess } from '../../actions/server'; |
| 10 | +import RoomHeaderContainer from './index'; |
| 11 | + |
| 12 | +const mockChild = jest.fn<any, [Record<string, unknown>]>(() => null); |
| 13 | +jest.mock('./RoomHeader', () => ({ |
| 14 | + __esModule: true, |
| 15 | + default: (props: Record<string, unknown>) => { |
| 16 | + mockChild(props); |
| 17 | + return null; |
| 18 | + } |
| 19 | +})); |
| 20 | + |
| 21 | +const Wrapper = ({ children }: { children: ReactNode }) => <Provider store={mockedStore}>{children}</Provider>; |
| 22 | + |
| 23 | +const defaultProps = { |
| 24 | + title: 'John Doe', |
| 25 | + type: 'd', |
| 26 | + roomUserId: 'user-123', |
| 27 | + onPress: jest.fn() |
| 28 | +}; |
| 29 | + |
| 30 | +const renderContainer = (props: Record<string, unknown> = {}) => |
| 31 | + render(<RoomHeaderContainer {...defaultProps} {...props} />, { wrapper: Wrapper }); |
| 32 | + |
| 33 | +describe('RoomHeaderContainer', () => { |
| 34 | + beforeAll(() => { |
| 35 | + initStore(mockedStore); |
| 36 | + }); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + mockedStore.dispatch(disconnect()); |
| 41 | + }); |
| 42 | + |
| 43 | + const connect = () => { |
| 44 | + mockedStore.dispatch(selectServerSuccess({ server: 'https://example.com', version: '6.0.0', name: 'Test' })); |
| 45 | + mockedStore.dispatch(connectSuccess()); |
| 46 | + }; |
| 47 | + |
| 48 | + it('should pass subtitle as Connecting when meteor is connecting for non-DM room', () => { |
| 49 | + mockedStore.dispatch(connectRequest()); |
| 50 | + mockedStore.dispatch(selectServerSuccess({ server: 'https://example.com', version: '6.0.0', name: 'Test' })); |
| 51 | + |
| 52 | + renderContainer({ type: 'c', roomUserId: undefined }); |
| 53 | + |
| 54 | + const props = mockChild.mock.calls[0][0]; |
| 55 | + expect(props.subtitle).toBe('Connecting...'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should pass subtitle as Waiting for network... when not connected for non-DM room', () => { |
| 59 | + mockedStore.dispatch(selectServerSuccess({ server: 'https://example.com', version: '6.0.0', name: 'Test' })); |
| 60 | + |
| 61 | + renderContainer({ type: 'c', roomUserId: undefined }); |
| 62 | + |
| 63 | + const props = mockChild.mock.calls[0][0]; |
| 64 | + expect(props.subtitle).toBe('Waiting for network...'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should pass presence status for DM room when connected and activeUser exists', () => { |
| 68 | + connect(); |
| 69 | + mockedStore.dispatch( |
| 70 | + setActiveUsers({ 'user-123': { status: 'online', statusText: 'Working from home', statusExpiresAt: undefined } }) |
| 71 | + ); |
| 72 | + |
| 73 | + renderContainer(); |
| 74 | + |
| 75 | + const props = mockChild.mock.calls[0][0]; |
| 76 | + expect(props.subtitle).toBe('Working from home'); |
| 77 | + expect(props.type).toBe('d'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should pass statusExpiresAt for DM room when activeUser has it', () => { |
| 81 | + connect(); |
| 82 | + mockedStore.dispatch( |
| 83 | + setActiveUsers({ |
| 84 | + 'user-123': { status: 'away', statusText: 'In a meeting', statusExpiresAt: '2026-06-20T13:00:00.000Z' } |
| 85 | + }) |
| 86 | + ); |
| 87 | + |
| 88 | + renderContainer(); |
| 89 | + |
| 90 | + const props = mockChild.mock.calls[0][0]; |
| 91 | + expect(props.subtitle).toBe('In a meeting'); |
| 92 | + expect(props.statusExpiresAt).toBe('2026-06-20T13:00:00.000Z'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should use presence label when statusText is empty', () => { |
| 96 | + connect(); |
| 97 | + mockedStore.dispatch(setActiveUsers({ 'user-123': { status: 'busy', statusText: '', statusExpiresAt: undefined } })); |
| 98 | + |
| 99 | + renderContainer(); |
| 100 | + |
| 101 | + const props = mockChild.mock.calls[0][0]; |
| 102 | + expect(props.subtitle).toBe('Busy'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not pass statusExpiresAt for non-DM room', () => { |
| 106 | + connect(); |
| 107 | + mockedStore.dispatch( |
| 108 | + setActiveUsers({ 'user-123': { status: 'away', statusText: 'Away', statusExpiresAt: '2026-06-20T13:00:00.000Z' } }) |
| 109 | + ); |
| 110 | + |
| 111 | + renderContainer({ type: 'c', roomUserId: undefined }); |
| 112 | + |
| 113 | + const props = mockChild.mock.calls[0][0]; |
| 114 | + expect(props.statusExpiresAt).toBeUndefined(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should pass subtitleProp when connected and no activeUser', () => { |
| 118 | + connect(); |
| 119 | + |
| 120 | + renderContainer({ subtitle: 'Custom subtitle', roomUserId: undefined, type: 'c' }); |
| 121 | + |
| 122 | + const props = mockChild.mock.calls[0][0]; |
| 123 | + expect(props.subtitle).toBe('Custom subtitle'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should pass roomUserId to RoomHeader', () => { |
| 127 | + connect(); |
| 128 | + |
| 129 | + renderContainer(); |
| 130 | + |
| 131 | + const props = mockChild.mock.calls[0][0]; |
| 132 | + expect(props.roomUserId).toBe('user-123'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle activeUser with offline status', () => { |
| 136 | + connect(); |
| 137 | + mockedStore.dispatch(setActiveUsers({ 'user-123': { status: 'offline', statusText: '', statusExpiresAt: undefined } })); |
| 138 | + |
| 139 | + renderContainer(); |
| 140 | + |
| 141 | + const props = mockChild.mock.calls[0][0]; |
| 142 | + expect(props.subtitle).toBe('Offline'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should use statusText even when status is undefined', () => { |
| 146 | + connect(); |
| 147 | + mockedStore.dispatch( |
| 148 | + setActiveUsers({ |
| 149 | + 'user-123': { status: undefined, statusText: 'Custom text', statusExpiresAt: undefined } as any |
| 150 | + }) |
| 151 | + ); |
| 152 | + |
| 153 | + renderContainer(); |
| 154 | + |
| 155 | + const props = mockChild.mock.calls[0][0]; |
| 156 | + expect(props.subtitle).toBe('Custom text'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should pass onPress to RoomHeader', () => { |
| 160 | + connect(); |
| 161 | + |
| 162 | + renderContainer(); |
| 163 | + |
| 164 | + const props = mockChild.mock.calls[0][0]; |
| 165 | + expect(props.onPress).toBe(defaultProps.onPress); |
| 166 | + }); |
| 167 | +}); |
0 commit comments