* 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
53 lines
1.4 KiB
TypeScript
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);
|
|
});
|
|
});
|