|
| 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