mattermost-mobile/app/client/rest/files.test.ts
Joram Wilander 4fedb846bc
MM-59683 + more - Add tests to client/rest (#8447)
* Add tests to client/rest/users

* Add tests to client/rest/channels

* Add tests to client/rest/posts

* Add tests to client/rest/teams

* Add tests to client/rest/general

* Minor clean-up

* Add tests to client/rest/file

* Add tests to client/rest/threads

* Add tests to client/rest/emojis

* Add tests to client/rest/integrations

* Add tests to client/rest/categories

* Add tests to client/rest/groups

* Add tests to client/rest/channel_bookmark

* Add tests to client/rest/preferences

* Add tests to client/rest/tos

* Add tests to client/rest/nps and client/rest/plugins

* Minor clean-up

* Fix type error with proper client.searchFiles definition

* add more test to cover 100%

* add teamId test

* improve users test

---------

Co-authored-by: Rahim Rahman <rahim.rahman@mattermost.com>
2025-01-07 08:23:28 -05:00

165 lines
5.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import TestHelper from '@test/test_helper';
import type ClientBase from './base';
import type {ClientFilesMix} from './files';
let client: ClientFilesMix & ClientBase;
beforeAll(() => {
client = TestHelper.createClient();
client.doFetch = jest.fn();
client.apiClient.upload = jest.fn();
});
test('getFileUrl', () => {
const fileId = 'file_id';
const timestamp = 123456;
const expectedBaseUrl = `${client.apiClient.baseUrl}${client.getFileRoute(fileId)}`;
const expectedUrl = `${expectedBaseUrl}?${timestamp}`;
const result = client.getFileUrl(fileId, timestamp);
expect(result).toBe(expectedUrl);
// Test with zero timestamp
const resultZero = client.getFileUrl(fileId, 0);
expect(resultZero).toBe(expectedBaseUrl);
});
test('getFileThumbnailUrl', () => {
const fileId = 'file_id';
const timestamp = 123456;
const expectedBaseUrl = `${client.apiClient.baseUrl}${client.getFileRoute(fileId)}/thumbnail`;
const expectedUrl = `${expectedBaseUrl}?${timestamp}`;
const result = client.getFileThumbnailUrl(fileId, timestamp);
expect(result).toBe(expectedUrl);
// Test with zero timestamp
const resultZero = client.getFileThumbnailUrl(fileId, 0);
expect(resultZero).toBe(expectedBaseUrl);
});
test('getFilePreviewUrl', () => {
const fileId = 'file_id';
const timestamp = 123456;
const expectedBaseUrl = `${client.apiClient.baseUrl}${client.getFileRoute(fileId)}/preview`;
const expectedUrl = `${expectedBaseUrl}?${timestamp}`;
const result = client.getFilePreviewUrl(fileId, timestamp);
expect(result).toBe(expectedUrl);
// Test with zero timestamp
const resultZero = client.getFilePreviewUrl(fileId, 0);
expect(resultZero).toBe(expectedBaseUrl);
});
test('getFilePublicLink', async () => {
const fileId = 'file_id';
const expectedUrl = `${client.getFileRoute(fileId)}/link`;
const expectedOptions = {method: 'get'};
await client.getFilePublicLink(fileId);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('uploadAttachment', () => {
const file = {localPath: '/path/to/file'} as FileInfo;
const channelId = 'channel_id';
const onProgress = jest.fn();
const onComplete = jest.fn();
const onError = jest.fn();
const skipBytes = 1;
const isBookmark = true;
const expectedUrl = `${client.getFilesRoute()}?bookmark=true`;
const expectedOptions = {
skipBytes,
method: 'POST',
multipart: {
data: {
channel_id: channelId,
},
},
timeoutInterval: 180000,
};
(client.apiClient.upload as jest.Mock).mockReturnValue({
progress: jest.fn().mockReturnThis(),
then: jest.fn().mockReturnThis(),
catch: jest.fn().mockReturnThis(),
cancel: jest.fn(),
});
client.uploadAttachment(file, channelId, onProgress, onComplete, onError, skipBytes, isBookmark);
expect(client.apiClient.upload).toHaveBeenCalledWith(expectedUrl, file.localPath, expectedOptions);
// Test with default values
const expectedDefaultOptions = {
skipBytes: 0,
method: 'POST',
multipart: {
data: {
channel_id: channelId,
},
},
timeoutInterval: 180000,
};
client.uploadAttachment(file, channelId, onProgress, onComplete, onError);
expect(client.apiClient.upload).toHaveBeenCalledWith(client.getFilesRoute(), file.localPath, expectedDefaultOptions);
});
test('uploadAttachment throws error when file has no localPath', () => {
const file = {} as FileInfo;
const channelId = 'channel_id';
expect(() => {
client.uploadAttachment(
file,
channelId,
jest.fn(),
jest.fn(),
jest.fn(),
);
}).toThrow('file does not have local path defined');
});
test('searchFilesWithParams', async () => {
const teamId = 'team_id';
const params = {terms: 'search terms', is_or_search: true};
const expectedUrl = `${client.getTeamRoute(teamId)}/files/search`;
const expectedOptions = {method: 'post', body: params};
await client.searchFilesWithParams(teamId, params);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('searchFilesWithParams without teamId', async () => {
const params = {terms: 'search terms', is_or_search: true};
const expectedUrl = `${client.getFilesRoute()}/search`;
const expectedOptions = {method: 'post', body: params};
await client.searchFilesWithParams('', params);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('searchFiles', async () => {
const teamId = 'team_id';
const terms = 'search terms';
const isOrSearch = true;
const params = {terms, is_or_search: isOrSearch};
const expectedUrl = `${client.getTeamRoute(teamId)}/files/search`;
const expectedOptions = {method: 'post', body: params};
await client.searchFiles(teamId, terms, isOrSearch);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});