mattermost-mobile/app/hooks/team_switch.test.ts
Joram Wilander 4187e9f81a
MM-59741 + others - Add tests for remaining app/hooks files (#8517)
* test: Add tests for useHandleSendMessage hook

* test: Add tests for header hooks

* test: Add tests for device hooks

* test: Add tests for useChannelSwitch hook

* test: Add tests for useTeamSwitch hook

* test: Add tests for useInputPropagation hook

* test: Add tests for useWhyDidYouUpdate hook

* test: Add tests for useFreeze hook

* test: Add tests for useAutocompleteDefaultAnimatedValues hook

* test: Add tests for usePersistentNotificationProps hook

* test: Add tests for useFetchingThreadState hook

* test: Add tests for useBackNavigation hook

* test: Add tests for useShowMoreAnimatedStyle hook

* test: Add tests for useTeamsLoading hook

* test: Add tests for useNavButtonPressed hook

* test: Add tests for useDidUpdate hook

* test: Add tests for useAppBinding hook

* Fix type error and test
2025-01-28 08:51:50 -05:00

54 lines
1.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {act, renderHook} from '@testing-library/react-hooks';
import {DeviceEventEmitter} from 'react-native';
import {Events} from '@constants';
import {useTeamSwitch} from './team_switch';
jest.useFakeTimers();
describe('useTeamSwitch', () => {
it('should initialize with loading false', () => {
const {result} = renderHook(() => useTeamSwitch());
expect(result.current).toBe(false);
});
it('should set loading true when switching starts', () => {
const {result} = renderHook(() => useTeamSwitch());
act(() => {
DeviceEventEmitter.emit(Events.TEAM_SWITCH, true);
});
expect(result.current).toBe(true);
});
it('should set loading false when switching ends', () => {
const {result} = renderHook(() => useTeamSwitch());
act(() => {
DeviceEventEmitter.emit(Events.TEAM_SWITCH, true);
});
expect(result.current).toBe(true);
act(() => {
DeviceEventEmitter.emit(Events.TEAM_SWITCH, false);
jest.runAllTimers();
});
expect(result.current).toBe(false);
});
it('should cleanup event listener on unmount', () => {
const removeMock = jest.fn();
jest.spyOn(DeviceEventEmitter, 'addListener').mockImplementation(() => ({
remove: removeMock,
} as any));
const {unmount} = renderHook(() => useTeamSwitch());
unmount();
expect(removeMock).toHaveBeenCalled();
});
});