mattermost-mobile/app/hooks/teams_loading.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

59 lines
1.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {renderHook, act} from '@testing-library/react-hooks';
import {BehaviorSubject} from 'rxjs';
import {getLoadingTeamChannelsSubject} from '@store/team_load_store';
import {useTeamsLoading} from './teams_loading';
jest.mock('@store/team_load_store', () => ({
getLoadingTeamChannelsSubject: jest.fn(),
}));
describe('useTeamsLoading', () => {
const mockSubject = new BehaviorSubject(0);
const serverUrl = 'https://example.com';
beforeEach(() => {
(getLoadingTeamChannelsSubject as jest.Mock).mockReturnValue(mockSubject);
});
afterEach(() => {
jest.clearAllMocks();
mockSubject.next(0);
});
it('should initialize with loading false', () => {
const {result} = renderHook(() => useTeamsLoading(serverUrl));
expect(result.current).toBe(false);
});
it('should update loading state when subject emits non-zero value', () => {
const {result} = renderHook(() => useTeamsLoading(serverUrl));
act(() => {
mockSubject.next(1);
});
expect(result.current).toBe(true);
act(() => {
mockSubject.next(0);
});
expect(result.current).toBe(false);
});
it('should unsubscribe on unmount', () => {
const unsubscribeSpy = jest.fn();
const subscription = {unsubscribe: unsubscribeSpy};
jest.spyOn(mockSubject, 'pipe').mockReturnValue({
subscribe: () => subscription,
} as any);
const {unmount} = renderHook(() => useTeamsLoading(serverUrl));
unmount();
expect(unsubscribeSpy).toHaveBeenCalled();
});
});