Skip to content

Commit

Permalink
Add unit tests for app/actions/local/file and app/actions/local/group (
Browse files Browse the repository at this point in the history
…#8077)

* Add tests for actions/local/file

* Add tests for actions/local/group
  • Loading branch information
jwilander authored Jul 11, 2024
1 parent 6031e8d commit 333b594
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
55 changes: 55 additions & 0 deletions app/actions/local/file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import DatabaseManager from '@database/manager';

import {
updateLocalFile,
updateLocalFilePath,
} from './file';

import type ServerDataOperator from '@database/operator/server_data_operator';
import type FileModel from '@typings/database/models/servers/file';

describe('updateLocalFiles', () => {
let operator: ServerDataOperator;
const serverUrl = 'baseHandler.test.com';
const fileInfo: FileInfo = {
id: 'fileid',
clientId: 'clientid',
localPath: 'path1',
} as FileInfo;

beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
operator = DatabaseManager.serverDatabases[serverUrl]!.operator;
});

afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});

it('updateLocalFile - handle not found database', async () => {
const {error} = await updateLocalFile('foo', fileInfo) as {error: unknown};
expect(error).toBeTruthy();
});

it('updateLocalFile', async () => {
const models = await updateLocalFile(serverUrl, fileInfo) as FileModel[];
expect(models).toBeDefined();
expect(models.length).toBe(1);
expect(models![0].id).toBe('fileid');
});

it('updateLocalFilePath - handle not found database', async () => {
const {error} = await updateLocalFilePath('foo', fileInfo.id as string, 'newpath');
expect(error).toBeTruthy();
});

it('updateLocalFilePath', async () => {
await operator.handleFiles({files: [fileInfo], prepareRecordsOnly: false});

const {error} = await updateLocalFilePath(serverUrl, fileInfo.id as string, 'newpath');
expect(error).toBeUndefined();
});
});
116 changes: 116 additions & 0 deletions app/actions/local/group.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import * as remoteGroups from '@actions/remote/groups';
import DatabaseManager from '@database/manager';

import {
searchGroupsByName,
searchGroupsByNameInTeam,
searchGroupsByNameInChannel,
} from './group';

import type ServerDataOperator from '@database/operator/server_data_operator';

jest.mock('@actions/remote/groups');

const mockedRemoteGroups = jest.mocked(remoteGroups);

describe('searchGroups', () => {
let operator: ServerDataOperator;
const serverUrl = 'baseHandler.test.com';
const teamId = 'teamid1';
const channelId = 'channelid1';
const group: Group = {
id: 'kjlw9j1ttnxwig7tnqgebg7dtipno',
name: 'groupname',
display_name: 'Test',
source: 'custom',
remote_id: 'iuh4r89egnslnvakjsdjhg',
description: 'Test description',
member_count: 0,
allow_reference: true,
create_at: 0,
update_at: 0,
delete_at: 0,
} as Group;

beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
operator = DatabaseManager.serverDatabases[serverUrl]!.operator;
});

afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});

it('searchGroupsByName - handle not found database', async () => {
const models = await searchGroupsByName('foo', 'test');
expect(models).toBeDefined();
expect(models.length).toBe(0);
});

it('searchGroupsByName - no groups', async () => {
const models = await searchGroupsByName(serverUrl, 'test');
expect(models).toBeDefined();
expect(models.length).toBe(0);
});

it('searchGroupsByName - fetch failed', async () => {
await operator.handleGroups({groups: [group], prepareRecordsOnly: false});
mockedRemoteGroups.fetchGroupsForAutocomplete.mockReturnValueOnce(Promise.reject(new Error('fail')));

const models = await searchGroupsByName(serverUrl, group.name);
expect(models).toBeDefined();
expect(models.length).toBe(1);
expect(models[0].id).toBe(group.id);
});

it('searchGroupsByNameInTeam - handle not found database', async () => {
const models = await searchGroupsByNameInTeam('foo', 'test', teamId);
expect(models).toBeDefined();
expect(models.length).toBe(0);
});

it('searchGroupsByNameInTeam - no groups', async () => {
const models = await searchGroupsByNameInTeam(serverUrl, 'test', teamId);
expect(models).toBeDefined();
expect(models.length).toBe(0);
});

it('searchGroupsByNameInTeam - fetch failed', async () => {
await operator.handleGroups({groups: [group], prepareRecordsOnly: false});
await operator.handleGroupTeamsForTeam({groups: [group], teamId, prepareRecordsOnly: false});

mockedRemoteGroups.fetchFilteredTeamGroups.mockReturnValueOnce(Promise.reject(new Error('fail')));

const models = await searchGroupsByNameInTeam(serverUrl, group.name, teamId);
expect(models).toBeDefined();
expect(models.length).toBe(1);
expect(models[0].id).toBe(group.id);
});

it('searchGroupsByNameInChannel - handle not found database', async () => {
const models = await searchGroupsByNameInChannel('foo', 'test', channelId);
expect(models).toBeDefined();
expect(models.length).toBe(0);
});

it('searchGroupsByNameInChannel - no groups', async () => {
const models = await searchGroupsByNameInChannel(serverUrl, 'test', channelId);
expect(models).toBeDefined();
expect(models.length).toBe(0);
});

it('searchGroupsByNameInChannel - fetch failed', async () => {
await operator.handleGroups({groups: [group], prepareRecordsOnly: false});
await operator.handleGroupChannelsForChannel({groups: [group], channelId, prepareRecordsOnly: false});

mockedRemoteGroups.fetchFilteredChannelGroups.mockReturnValueOnce(Promise.reject(new Error('fail')));

const models = await searchGroupsByNameInChannel(serverUrl, group.name, channelId);
expect(models).toBeDefined();
expect(models.length).toBe(1);
expect(models[0].id).toBe(group.id);
});
});

0 comments on commit 333b594

Please sign in to comment.