mattermost-mobile/app/hooks/file_upload_error.test.ts
Rajat Dabade dae4b75720
Ability to show quick action and add files to edit post (#8926)
* Viewing Files in Edit mode in mobile with ability to delete and save

* Added upload attachment to keyboard tracker view

* using state instread of use ref

* Minor

* Added tests

* intl extract

* new function getFiles
ById, batch file deletion and tests

* Files fetching in edit options and tests

* Removed DeviceEventEmitter and used React context

* Added support to check minimum required version to show edit file attachments

* resolve forward ref issue

* Minor

* memotized props for context and observe config with value

* Import fixes

* Ability to show quick action and add files to edit post

* type safety for EditPostContext

* Reverted back the post priority props

* constant shift

* Added test for QuickAction to show slashcomand

* Added test for edit_post, upload_item and upload_remove

* Added test for Edit_post_input and edit_post index

* fix the height issue between attachment and keyboard due to safeArea

* Minor: removed debugging border color

* Addressed dev review comments

* Import fixes

* Address UX comments

* Fixed props for UploadItem and remove effective Edit mode

* handled save button disabled state when uploading attachments

* handled newly added and retry file removal without alert message

* Test updated

* Added test for input_quick_action index.tsx

* Added test for not in edit mode for upload_item index

* Added test for upload_remove component when not in edit mode

* added tests for file_upload_error hook

* Added test for calling callback when in edit mode

* Test for edit post input for server version check
2025-06-25 16:04:08 +05:30

138 lines
4.6 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 {UPLOAD_ERROR_SHOW_INTERVAL} from '@constants/files';
import useFileUploadError from './file_upload_error';
jest.useFakeTimers();
describe('useFileUploadError', () => {
afterEach(() => {
jest.clearAllTimers();
});
it('should initialize with no upload error', () => {
const {result} = renderHook(() => useFileUploadError());
expect(result.current.uploadError).toBeNull();
expect(typeof result.current.newUploadError).toBe('function');
});
it('should set upload error when newUploadError is called', () => {
const {result} = renderHook(() => useFileUploadError());
const errorMessage = 'Upload failed';
act(() => {
result.current.newUploadError(errorMessage);
});
expect(result.current.uploadError).toBe(errorMessage);
});
it('should clear upload error after timeout', () => {
const {result} = renderHook(() => useFileUploadError());
const errorMessage = 'Upload failed';
act(() => {
result.current.newUploadError(errorMessage);
});
expect(result.current.uploadError).toBe(errorMessage);
act(() => {
jest.advanceTimersByTime(UPLOAD_ERROR_SHOW_INTERVAL);
});
expect(result.current.uploadError).toBeNull();
});
it('should handle different types of error messages', () => {
const {result} = renderHook(() => useFileUploadError());
const complexErrorMessage = 'Complex error: File size too large (5MB), maximum allowed is 2MB';
act(() => {
result.current.newUploadError(complexErrorMessage);
});
expect(result.current.uploadError).toBe(complexErrorMessage);
});
it('should clear previous timeout when new error is set', () => {
const {result} = renderHook(() => useFileUploadError());
const firstError = 'First error';
const secondError = 'Second error';
// Set first error
act(() => {
result.current.newUploadError(firstError);
});
expect(result.current.uploadError).toBe(firstError);
// Set second error before first timeout completes
act(() => {
jest.advanceTimersByTime(2000); // Advance partway through first timeout
result.current.newUploadError(secondError);
});
expect(result.current.uploadError).toBe(secondError);
// Advance to where first timeout would have completed
act(() => {
jest.advanceTimersByTime(3000); // Total 5000ms from first error
});
// Error should still be second error (first timeout was cleared)
expect(result.current.uploadError).toBe(secondError);
// Complete second timeout
act(() => {
jest.advanceTimersByTime(2000); // Complete 5000ms from second error
});
expect(result.current.uploadError).toBeNull();
});
it('should not clear error before timeout completes', () => {
const {result} = renderHook(() => useFileUploadError());
const errorMessage = 'Upload failed';
act(() => {
result.current.newUploadError(errorMessage);
});
expect(result.current.uploadError).toBe(errorMessage);
// Advance time but not to full timeout
act(() => {
jest.advanceTimersByTime(UPLOAD_ERROR_SHOW_INTERVAL - 1000);
});
// Error should still be present
expect(result.current.uploadError).toBe(errorMessage);
});
it('should handle multiple consecutive errors correctly', () => {
const {result} = renderHook(() => useFileUploadError());
const error1 = 'Error 1';
const error2 = 'Error 2';
const error3 = 'Error 3';
// Set first error
act(() => {
result.current.newUploadError(error1);
});
expect(result.current.uploadError).toBe(error1);
// Quickly set second error
act(() => {
result.current.newUploadError(error2);
});
expect(result.current.uploadError).toBe(error2);
// Quickly set third error
act(() => {
result.current.newUploadError(error3);
});
expect(result.current.uploadError).toBe(error3);
// Only the last timeout should be active
act(() => {
jest.advanceTimersByTime(UPLOAD_ERROR_SHOW_INTERVAL);
});
expect(result.current.uploadError).toBeNull();
});
});