Skip to content

Commit 333b594

Browse files
authored
Add unit tests for app/actions/local/file and app/actions/local/group (#8077)
* Add tests for actions/local/file * Add tests for actions/local/group
1 parent 6031e8d commit 333b594

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

app/actions/local/file.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2+
// See LICENSE.txt for license information.
3+
4+
import DatabaseManager from '@database/manager';
5+
6+
import {
7+
updateLocalFile,
8+
updateLocalFilePath,
9+
} from './file';
10+
11+
import type ServerDataOperator from '@database/operator/server_data_operator';
12+
import type FileModel from '@typings/database/models/servers/file';
13+
14+
describe('updateLocalFiles', () => {
15+
let operator: ServerDataOperator;
16+
const serverUrl = 'baseHandler.test.com';
17+
const fileInfo: FileInfo = {
18+
id: 'fileid',
19+
clientId: 'clientid',
20+
localPath: 'path1',
21+
} as FileInfo;
22+
23+
beforeEach(async () => {
24+
await DatabaseManager.init([serverUrl]);
25+
operator = DatabaseManager.serverDatabases[serverUrl]!.operator;
26+
});
27+
28+
afterEach(async () => {
29+
await DatabaseManager.destroyServerDatabase(serverUrl);
30+
});
31+
32+
it('updateLocalFile - handle not found database', async () => {
33+
const {error} = await updateLocalFile('foo', fileInfo) as {error: unknown};
34+
expect(error).toBeTruthy();
35+
});
36+
37+
it('updateLocalFile', async () => {
38+
const models = await updateLocalFile(serverUrl, fileInfo) as FileModel[];
39+
expect(models).toBeDefined();
40+
expect(models.length).toBe(1);
41+
expect(models![0].id).toBe('fileid');
42+
});
43+
44+
it('updateLocalFilePath - handle not found database', async () => {
45+
const {error} = await updateLocalFilePath('foo', fileInfo.id as string, 'newpath');
46+
expect(error).toBeTruthy();
47+
});
48+
49+
it('updateLocalFilePath', async () => {
50+
await operator.handleFiles({files: [fileInfo], prepareRecordsOnly: false});
51+
52+
const {error} = await updateLocalFilePath(serverUrl, fileInfo.id as string, 'newpath');
53+
expect(error).toBeUndefined();
54+
});
55+
});

app/actions/local/group.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2+
// See LICENSE.txt for license information.
3+
4+
import * as remoteGroups from '@actions/remote/groups';
5+
import DatabaseManager from '@database/manager';
6+
7+
import {
8+
searchGroupsByName,
9+
searchGroupsByNameInTeam,
10+
searchGroupsByNameInChannel,
11+
} from './group';
12+
13+
import type ServerDataOperator from '@database/operator/server_data_operator';
14+
15+
jest.mock('@actions/remote/groups');
16+
17+
const mockedRemoteGroups = jest.mocked(remoteGroups);
18+
19+
describe('searchGroups', () => {
20+
let operator: ServerDataOperator;
21+
const serverUrl = 'baseHandler.test.com';
22+
const teamId = 'teamid1';
23+
const channelId = 'channelid1';
24+
const group: Group = {
25+
id: 'kjlw9j1ttnxwig7tnqgebg7dtipno',
26+
name: 'groupname',
27+
display_name: 'Test',
28+
source: 'custom',
29+
remote_id: 'iuh4r89egnslnvakjsdjhg',
30+
description: 'Test description',
31+
member_count: 0,
32+
allow_reference: true,
33+
create_at: 0,
34+
update_at: 0,
35+
delete_at: 0,
36+
} as Group;
37+
38+
beforeEach(async () => {
39+
await DatabaseManager.init([serverUrl]);
40+
operator = DatabaseManager.serverDatabases[serverUrl]!.operator;
41+
});
42+
43+
afterEach(async () => {
44+
await DatabaseManager.destroyServerDatabase(serverUrl);
45+
});
46+
47+
it('searchGroupsByName - handle not found database', async () => {
48+
const models = await searchGroupsByName('foo', 'test');
49+
expect(models).toBeDefined();
50+
expect(models.length).toBe(0);
51+
});
52+
53+
it('searchGroupsByName - no groups', async () => {
54+
const models = await searchGroupsByName(serverUrl, 'test');
55+
expect(models).toBeDefined();
56+
expect(models.length).toBe(0);
57+
});
58+
59+
it('searchGroupsByName - fetch failed', async () => {
60+
await operator.handleGroups({groups: [group], prepareRecordsOnly: false});
61+
mockedRemoteGroups.fetchGroupsForAutocomplete.mockReturnValueOnce(Promise.reject(new Error('fail')));
62+
63+
const models = await searchGroupsByName(serverUrl, group.name);
64+
expect(models).toBeDefined();
65+
expect(models.length).toBe(1);
66+
expect(models[0].id).toBe(group.id);
67+
});
68+
69+
it('searchGroupsByNameInTeam - handle not found database', async () => {
70+
const models = await searchGroupsByNameInTeam('foo', 'test', teamId);
71+
expect(models).toBeDefined();
72+
expect(models.length).toBe(0);
73+
});
74+
75+
it('searchGroupsByNameInTeam - no groups', async () => {
76+
const models = await searchGroupsByNameInTeam(serverUrl, 'test', teamId);
77+
expect(models).toBeDefined();
78+
expect(models.length).toBe(0);
79+
});
80+
81+
it('searchGroupsByNameInTeam - fetch failed', async () => {
82+
await operator.handleGroups({groups: [group], prepareRecordsOnly: false});
83+
await operator.handleGroupTeamsForTeam({groups: [group], teamId, prepareRecordsOnly: false});
84+
85+
mockedRemoteGroups.fetchFilteredTeamGroups.mockReturnValueOnce(Promise.reject(new Error('fail')));
86+
87+
const models = await searchGroupsByNameInTeam(serverUrl, group.name, teamId);
88+
expect(models).toBeDefined();
89+
expect(models.length).toBe(1);
90+
expect(models[0].id).toBe(group.id);
91+
});
92+
93+
it('searchGroupsByNameInChannel - handle not found database', async () => {
94+
const models = await searchGroupsByNameInChannel('foo', 'test', channelId);
95+
expect(models).toBeDefined();
96+
expect(models.length).toBe(0);
97+
});
98+
99+
it('searchGroupsByNameInChannel - no groups', async () => {
100+
const models = await searchGroupsByNameInChannel(serverUrl, 'test', channelId);
101+
expect(models).toBeDefined();
102+
expect(models.length).toBe(0);
103+
});
104+
105+
it('searchGroupsByNameInChannel - fetch failed', async () => {
106+
await operator.handleGroups({groups: [group], prepareRecordsOnly: false});
107+
await operator.handleGroupChannelsForChannel({groups: [group], channelId, prepareRecordsOnly: false});
108+
109+
mockedRemoteGroups.fetchFilteredChannelGroups.mockReturnValueOnce(Promise.reject(new Error('fail')));
110+
111+
const models = await searchGroupsByNameInChannel(serverUrl, group.name, channelId);
112+
expect(models).toBeDefined();
113+
expect(models.length).toBe(1);
114+
expect(models[0].id).toBe(group.id);
115+
});
116+
});

0 commit comments

Comments
 (0)