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

81 lines
2.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {renderHook} from '@testing-library/react-hooks';
import {Navigation} from 'react-native-navigation';
import useNavButtonPressed from './navigation_button_pressed';
jest.mock('react-native-navigation', () => ({
Navigation: {
events: jest.fn().mockReturnValue({
registerComponentListener: jest.fn(),
}),
},
}));
describe('hooks/useNavButtonPressed', () => {
const componentId = 'test-component-id';
const buttonId = 'test-button-id';
let callback: jest.Mock;
let unsubscribeMock: jest.Mock;
beforeEach(() => {
callback = jest.fn();
unsubscribeMock = jest.fn();
(Navigation.events().registerComponentListener as jest.Mock).mockReturnValue({
remove: unsubscribeMock,
});
});
afterEach(() => {
jest.clearAllMocks();
});
it('should register navigation button listener', () => {
renderHook(() => useNavButtonPressed(buttonId, componentId, callback));
expect(Navigation.events().registerComponentListener).toHaveBeenCalledWith(
expect.any(Object),
componentId,
);
});
it('should call callback when matching button is pressed', () => {
renderHook(() => useNavButtonPressed(buttonId, componentId, callback));
const listener = (Navigation.events().registerComponentListener as jest.Mock).mock.calls[0][0];
listener.navigationButtonPressed({buttonId});
expect(callback).toHaveBeenCalledTimes(1);
});
it('should not call callback when different button is pressed', () => {
renderHook(() => useNavButtonPressed(buttonId, componentId, callback));
const listener = (Navigation.events().registerComponentListener as jest.Mock).mock.calls[0][0];
listener.navigationButtonPressed({buttonId: 'different-button'});
expect(callback).not.toHaveBeenCalled();
});
it('should unsubscribe listener on unmount', () => {
const {unmount} = renderHook(() => useNavButtonPressed(buttonId, componentId, callback));
unmount();
expect(unsubscribeMock).toHaveBeenCalledTimes(1);
});
it('should re-register listener when deps change', () => {
const {rerender} = renderHook(
({dep}) => useNavButtonPressed(buttonId, componentId, callback, [dep]),
{initialProps: {dep: 1}},
);
rerender({dep: 2});
expect(Navigation.events().registerComponentListener).toHaveBeenCalledTimes(2);
expect(unsubscribeMock).toHaveBeenCalledTimes(1);
});
});