Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-59740 + others - Add tests for various files in hooks/ #8448

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions app/hooks/android_back_handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {renderHook} from '@testing-library/react-hooks';
import {BackHandler} from 'react-native';

import NavigationStore from '@store/navigation_store';

import useAndroidHardwareBackHandler from './android_back_handler';

jest.mock('react-native', () => ({
BackHandler: {
addEventListener: jest.fn(),
},
}));

jest.mock('@store/navigation_store', () => ({
getVisibleScreen: jest.fn(),
}));

test('useAndroidHardwareBackHandler - calls callback when visible screen matches componentId', () => {
const componentId = 'About';
const callback = jest.fn();

(NavigationStore.getVisibleScreen as jest.Mock).mockReturnValue(componentId);

renderHook(() => useAndroidHardwareBackHandler(componentId, callback));

const hardwareBackPressHandler = (BackHandler.addEventListener as jest.Mock).mock.calls[0][1];
hardwareBackPressHandler();

expect(callback).toHaveBeenCalled();
});

test('useAndroidHardwareBackHandler - does not call callback when visible screen does not match componentId', () => {
const componentId = 'About';
const callback = jest.fn();

(NavigationStore.getVisibleScreen as jest.Mock).mockReturnValue('otherScreen');

renderHook(() => useAndroidHardwareBackHandler(componentId, callback));

const hardwareBackPressHandler = (BackHandler.addEventListener as jest.Mock).mock.calls[0][1];
hardwareBackPressHandler();

expect(callback).not.toHaveBeenCalled();
});

test('useAndroidHardwareBackHandler - removes event listener on unmount', () => {
const componentId = 'About';
const callback = jest.fn();
const remove = jest.fn();
(BackHandler.addEventListener as jest.Mock).mockReturnValue({remove});

const {unmount} = renderHook(() => useAndroidHardwareBackHandler(componentId, callback));

unmount();

expect(remove).toHaveBeenCalled();
});
128 changes: 128 additions & 0 deletions app/hooks/emoji_category_bar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// 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 {
selectEmojiCategoryBarSection,
setEmojiCategoryBarSection,
setEmojiCategoryBarIcons,
setEmojiSkinTone,
useEmojiCategoryBar,
useEmojiSkinTone,
} from './emoji_category_bar';

describe('EmojiCategoryBar', () => {
const mockSubject = {
next: jest.fn(),
value: {
icons: undefined,
currentIndex: 0,
selectedIndex: undefined,
skinTone: 'default',
},
subscribe: jest.fn().mockImplementation((callback) => {
callback(mockSubject.value);
return {unsubscribe: jest.fn()};
}),
};

beforeEach(() => {
jest.spyOn(BehaviorSubject.prototype, 'next').mockImplementation(mockSubject.next);
jest.spyOn(BehaviorSubject.prototype, 'subscribe').mockImplementation(mockSubject.subscribe);
jest.spyOn(BehaviorSubject.prototype, 'value', 'get').mockReturnValue(mockSubject.value);
});

afterEach(() => {
jest.clearAllMocks();
});

describe('setEmojiSkinTone', () => {
it('should update the skin tone in the state', () => {
setEmojiSkinTone('light');

expect(mockSubject.next).toHaveBeenCalledWith({
...mockSubject.value,
skinTone: 'light',
});
});
});

describe('useEmojiSkinTone', () => {
it('should return the current skin tone', () => {
const {result} = renderHook(() => useEmojiSkinTone());

expect(result.current).toBe('default');
});

it('should update the skin tone when it changes', () => {
const {result} = renderHook(() => useEmojiSkinTone());

act(() => {
mockSubject.subscribe.mock.calls[0][0]({skinTone: 'dark'});
});

expect(result.current).toBe('dark');
});
});

describe('selectEmojiCategoryBarSection', () => {
it('should update the selected index in the state', () => {
selectEmojiCategoryBarSection(1);

expect(mockSubject.next).toHaveBeenCalledWith({
...mockSubject.value,
selectedIndex: 1,
});
});
});

describe('setEmojiCategoryBarSection', () => {
it('should update the current index in the state', () => {
setEmojiCategoryBarSection(1);

expect(mockSubject.next).toHaveBeenCalledWith({
...mockSubject.value,
currentIndex: 1,
});
});
});

describe('setEmojiCategoryBarIcons', () => {
it('should update the icons in the state', () => {
const icons = [{key: 'smile', name: 'smile', icon: 'smile'}];
setEmojiCategoryBarIcons(icons);

expect(mockSubject.next).toHaveBeenCalledWith({
...mockSubject.value,
icons,
});
});
});

describe('useEmojiCategoryBar', () => {
it('should return the current state', () => {
const {result} = renderHook(() => useEmojiCategoryBar());

expect(result.current).toEqual(mockSubject.value);
});

it('should update the state when it changes', () => {
const {result} = renderHook(() => useEmojiCategoryBar());

const newState = {
icons: [{name: 'smile', icon: 'smile'}],
currentIndex: 1,
selectedIndex: 2,
skinTone: 'dark',
};

act(() => {
mockSubject.subscribe.mock.calls[0][0](newState);
});

expect(result.current).toEqual(newState);
});
});
});
83 changes: 83 additions & 0 deletions app/hooks/files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {renderHook} from '@testing-library/react-hooks';

import {getLocalFileInfo} from '@actions/local/file';
import {buildFilePreviewUrl, buildFileUrl} from '@actions/remote/file';
import {useServerUrl} from '@context/server';
import {isGif, isImage, isVideo} from '@utils/file';
import {getImageSize} from '@utils/gallery';

import {useImageAttachments, useChannelBookmarkFiles} from './files';

import type {ChannelBookmarkModel} from '@database/models/server';

jest.mock('@context/server');
jest.mock('@actions/local/file');
jest.mock('@actions/remote/file');
jest.mock('@utils/file');
jest.mock('@utils/gallery');

describe('useImageAttachments', () => {
it('should separate images and non-images correctly', () => {
const serverUrl = 'https://example.com';
(useServerUrl as jest.Mock).mockReturnValue(serverUrl);

const filesInfo = [
{id: '1', localPath: 'path/to/image1', has_preview_image: true},
{id: '2', localPath: 'path/to/video1', has_preview_image: false},
{id: '3', localPath: 'path/to/file1', has_preview_image: false},
] as FileInfo[];

(isImage as jest.Mock).mockImplementation((file) => file.id === '1');
(isVideo as jest.Mock).mockImplementation((file) => file.id === '2');
(isGif as jest.Mock).mockImplementation(() => false);
(buildFileUrl as jest.Mock).mockImplementation((url, id) => `${url}/files/${id}`);
(buildFilePreviewUrl as jest.Mock).mockImplementation((url, id) => `${url}/files/${id}/preview`);

const {result} = renderHook(() => useImageAttachments(filesInfo, true));

expect(result.current.images).toHaveLength(2);
expect(result.current.nonImages).toHaveLength(1);
});
});

describe('useChannelBookmarkFiles', () => {
it('should fetch and set file info correctly', async () => {
const serverUrl = 'https://example.com';
(useServerUrl as jest.Mock).mockReturnValue(serverUrl);

const bookmarks = [
{fileId: '1', ownerId: 'user1'},
{fileId: '2', ownerId: 'user2'},
] as ChannelBookmarkModel[];

const file1 = {id: '1', localPath: 'path/to/image1', has_preview_image: true, toFileInfo: jest.fn().mockReturnValue({id: '1'})};
const file2 = {id: '2', localPath: 'path/to/video1', has_preview_image: false, toFileInfo: jest.fn().mockReturnValue({id: '2'})};

(getLocalFileInfo as jest.Mock).mockImplementation((url, id) => {
if (id === '1') {
return {file: file1};
} else if (id === '2') {
return {file: file2};
}
return {file: null};
});

(isImage as jest.Mock).mockImplementation((file) => file.id === '1');
(isVideo as jest.Mock).mockImplementation((file) => file.id === '2');
(isGif as jest.Mock).mockImplementation(() => false);
(buildFileUrl as jest.Mock).mockImplementation((url, id) => `${url}/files/${id}`);
(buildFilePreviewUrl as jest.Mock).mockImplementation((url, id) => `${url}/files/${id}/preview`);
(getImageSize as jest.Mock).mockImplementation(() => ({width: 100, height: 100}));

const {result, waitForNextUpdate} = renderHook(() => useChannelBookmarkFiles(bookmarks, true));

await waitForNextUpdate();

expect(result.current).toHaveLength(2);
expect(result.current[0].id).toBe('1');
expect(result.current[1].id).toBe('2');
});
});
Loading
Loading