mattermost-mobile/app/hooks/did_update.test.tsx
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

53 lines
1.4 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 useDidUpdate from './did_update';
describe('useDidUpdate', () => {
test('should not call callback on mount', () => {
const callback = jest.fn();
renderHook(() => useDidUpdate(callback));
expect(callback).not.toHaveBeenCalled();
});
test('should call callback when dependencies change', () => {
const callback = jest.fn();
const {rerender} = renderHook(({dep}) => useDidUpdate(callback, [dep]), {
initialProps: {dep: 1},
});
expect(callback).not.toHaveBeenCalled();
act(() => {
rerender({dep: 2});
});
expect(callback).toHaveBeenCalledTimes(1);
});
test('should maintain mounted state across re-renders with same deps', () => {
const callback = jest.fn();
const {rerender} = renderHook(({dep}) => useDidUpdate(callback, [dep]), {
initialProps: {dep: 1},
});
expect(callback).not.toHaveBeenCalled();
// Re-render with same dependency
act(() => {
rerender({dep: 1});
});
expect(callback).not.toHaveBeenCalled();
// Change dependency
act(() => {
rerender({dep: 2});
});
expect(callback).toHaveBeenCalledTimes(1);
});
});