From 59a69368bf68af82e5f9cda17fb2de1be31da757 Mon Sep 17 00:00:00 2001 From: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Date: Sat, 25 Nov 2023 05:25:46 +0530 Subject: [PATCH] Added one important test (#7683) * Added one important test * Update app/actions/local/category.test.ts --------- Co-authored-by: Elias Nahum --- app/actions/local/category.test.ts | 69 ++++++++++++++++++++++++++++++ app/actions/local/category.ts | 5 ++- 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 app/actions/local/category.test.ts diff --git a/app/actions/local/category.test.ts b/app/actions/local/category.test.ts new file mode 100644 index 000000000..bdbe152a0 --- /dev/null +++ b/app/actions/local/category.test.ts @@ -0,0 +1,69 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import DatabaseManager from '@database/manager'; + +import {handleConvertedGMCategories} from './category'; + +import type ServerDataOperator from '@database/operator/server_data_operator'; + +describe('handleConvertedGMCategories', () => { + const serverUrl = 'baseHandler.test.com'; + const channelId = 'channel_id_1'; + const teamId1 = 'team_id_1'; + const teamId2 = 'team_id_2'; + const team: Team = { + id: teamId1, + } as Team; + + let operator: ServerDataOperator; + + beforeEach(async () => { + await DatabaseManager.init([serverUrl]); + operator = DatabaseManager.serverDatabases[serverUrl]!.operator; + }); + + it('base case', async () => { + await operator.handleTeam({teams: [team], prepareRecordsOnly: false}); + + const defaultCategory: Category = { + id: 'default_category_id', + team_id: teamId1, + type: 'channels', + } as Category; + + const customCategory: Category = { + id: 'custom_category_id', + team_id: teamId2, + type: 'custom', + } as Category; + + const dmCategory: Category = { + id: 'dm_category_id', + team_id: teamId1, + type: 'direct_messages', + } as Category; + + await operator.handleCategories({categories: [defaultCategory, customCategory, dmCategory], prepareRecordsOnly: false}); + + const dmCategoryChannel: CategoryChannel = { + id: 'dm_category_channel_id', + category_id: 'dm_category_id', + channel_id: channelId, + sort_order: 1, + }; + + const customCategoryChannel: CategoryChannel = { + id: 'custom_category_channel_id', + category_id: 'dm_category_id', + channel_id: channelId, + sort_order: 1, + }; + await operator.handleCategoryChannels({categoryChannels: [dmCategoryChannel, customCategoryChannel], prepareRecordsOnly: false}); + + const {models, error} = await handleConvertedGMCategories(serverUrl, channelId, teamId1, true); + expect(error).toBeUndefined(); + expect(models).toBeDefined(); + expect(models!.length).toBe(3); // two for removing channel for a custom and a DM category, and one for adding it to default channels category + }); +}); diff --git a/app/actions/local/category.ts b/app/actions/local/category.ts index 9d853daf4..c390e0992 100644 --- a/app/actions/local/category.ts +++ b/app/actions/local/category.ts @@ -134,8 +134,9 @@ export async function handleConvertedGMCategories(serverUrl: string, channelId: const channelCategory = categories.find((category) => category.type === CHANNELS_CATEGORY); if (!channelCategory) { - logError('Failed to find default category when handling category of converted GM'); - return {}; + const error = 'Failed to find default category when handling category of converted GM'; + logError(error); + return {error}; } const models: Model[] = [];