Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for mobile draft #8363

Open
wants to merge 61 commits into
base: mobile-drafts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
7a954e7
refactor: started with draft, done until new tabs for draft
Rajat-Dabade Sep 14, 2024
bf64bce
refactor: change the query and added the screen for draft
Rajat-Dabade Sep 16, 2024
d7e3aea
added condition for fetching draft for channel delete or not
Rajat-Dabade Sep 17, 2024
421fdf2
refactor: added draft screen
Rajat-Dabade Sep 20, 2024
c32df46
linter fixes
Rajat-Dabade Sep 22, 2024
1eefbde
Added draft post component
Rajat-Dabade Sep 24, 2024
6cb3094
added avatar and header display name for the draft post list
Rajat-Dabade Sep 25, 2024
1dbe020
added channel info component
Rajat-Dabade Oct 2, 2024
978a8cb
channel info completed
Rajat-Dabade Oct 7, 2024
f069326
proper naming
Rajat-Dabade Oct 7, 2024
9b4d50f
added image file markdown acknowledgement support
Rajat-Dabade Oct 7, 2024
743d994
draft actions
Rajat-Dabade Oct 9, 2024
bf7767e
Fix the draft receiver in drafts
Rajat-Dabade Oct 14, 2024
0aae855
separated send message handler
Rajat-Dabade Oct 14, 2024
bf446e3
Done with send drafts
Rajat-Dabade Oct 14, 2024
cf37827
done with delete drafts
Rajat-Dabade Oct 15, 2024
0a1fc59
change save to send draft
Rajat-Dabade Oct 15, 2024
b7b5053
handle lengthy message with show more button
Rajat-Dabade Oct 15, 2024
594f167
done with persistent message edit, send and delete drafts
Rajat-Dabade Oct 17, 2024
ea46f34
added alert for sending message
Rajat-Dabade Oct 17, 2024
b895070
added update at time for the drafts
Rajat-Dabade Oct 17, 2024
b93b340
Merge branch 'main' into mobile-drafts
mattermost-build Oct 22, 2024
086ec82
en.json extract fix
Rajat-Dabade Oct 22, 2024
98d58c3
Updated dependencies for useCallback
Rajat-Dabade Oct 23, 2024
902e2ea
refactor: added drafts list to animated list
Rajat-Dabade Oct 24, 2024
3fea445
added swipeable component and delete conformation for drafts
Rajat-Dabade Oct 26, 2024
2607869
done with rendering of images in markdown for drafts
Rajat-Dabade Oct 30, 2024
5e99dce
en.json issue fixed
Rajat-Dabade Nov 5, 2024
d26155c
fix en.json issue
Rajat-Dabade Nov 5, 2024
c7e36be
refactor: en.json fix
Rajat-Dabade Nov 5, 2024
9fe6ab7
addressed review comments
Rajat-Dabade Nov 12, 2024
a8edbd2
updated image metadata handling code
Rajat-Dabade Nov 12, 2024
3db3fe3
Merge branch 'main' into mobile-drafts
mattermost-build Nov 12, 2024
790062a
linter fixes
Rajat-Dabade Nov 13, 2024
9d272a8
added the empty draft screen
Rajat-Dabade Nov 18, 2024
a0c7f77
linter fix
Rajat-Dabade Nov 18, 2024
8754963
style fix
Rajat-Dabade Nov 18, 2024
8b58c92
back button an android takes to the channel list page
Rajat-Dabade Nov 18, 2024
cc8b130
en.json fix
Rajat-Dabade Nov 18, 2024
4c11090
draft actions theme compatible
Rajat-Dabade Nov 26, 2024
dccc8e1
CSS fix for draft channel_info and avatar component
Rajat-Dabade Nov 26, 2024
d8cd992
removed the badge icon and change font style drafts
Rajat-Dabade Nov 26, 2024
943f780
fix send alert sender name for GMs
Rajat-Dabade Nov 26, 2024
f5eb96f
updated snapshot
Rajat-Dabade Nov 26, 2024
d54de5f
added test for app/actions/local/draft.ts
Rajat-Dabade Nov 18, 2024
091830a
added test for app/components/channel_info/avatar/index.tsx
Rajat-Dabade Nov 18, 2024
6ecdc5e
added test for app/components/channel_info/channel_info.tsx
Rajat-Dabade Nov 19, 2024
9a1c026
added unit test for app/components/draft/draft.tsx
Rajat-Dabade Nov 22, 2024
85b4105
added test for app/components/draft/draft_post/draft_message/index.tsx
Rajat-Dabade Nov 26, 2024
48d3595
added comparing snapshot for previously added test
Rajat-Dabade Nov 26, 2024
2bb023e
Added unit test for app/components/draft/draft_post/index.tsx
Rajat-Dabade Nov 26, 2024
58a9ba4
Added jest unit test from app/components/drafts_buttton/drafts_button…
Rajat-Dabade Nov 26, 2024
05785df
added test for app/screens/draft_options/delete_draft/index.tsx
Rajat-Dabade Nov 26, 2024
75cc239
added test case from edit_draft and send_draft
Rajat-Dabade Nov 26, 2024
3195d14
added test from app/screens/draft_options/index.tsx
Rajat-Dabade Nov 26, 2024
84041df
Added unit test for app/screens/global_drafts/components/draft_empty_…
Rajat-Dabade Nov 26, 2024
7fd7aff
updated snapshot
Rajat-Dabade Nov 26, 2024
c1529d4
added testId to the drafts components
Rajat-Dabade Nov 26, 2024
41054b4
updated send draft test id
Rajat-Dabade Nov 26, 2024
1dfbe6f
Merge branch 'mobile-drafts' into mobile-draft-test
Rajat-Dabade Nov 26, 2024
d91e8a1
udpated snapshot
Rajat-Dabade Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/actions/local/draft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
addFilesToDraft,
removeDraft,
updateDraftPriority,
updateDraftMarkdownImageMetadata,
} from './draft';

import type ServerDataOperator from '@database/operator/server_data_operator';
Expand Down Expand Up @@ -234,3 +235,41 @@ describe('updateDraftPriority', () => {
expect(result.draft.metadata?.priority?.priority).toBe(postPriority.priority);
});
});

describe('updateDraftMarkdownImageMetadata', () => {
const imageMetadata = {images: {width: 100, height: 100}};
it('handle not found database', async () => {
const {error} = await updateDraftMarkdownImageMetadata({
serverUrl: 'foo',
channelId,
rootId: '',
imageMetadata: {images: {width: 100, height: 100}},
});
expect(error).toBeTruthy();
});

it('handles no draft', async () => {
const result = await updateDraftMarkdownImageMetadata({
serverUrl,
channelId,
rootId: '',
imageMetadata,
});
expect(result.draft).toBeUndefined();
});

it('updates metadata for an existing draft', async () => {
await operator.handleDraft({drafts: [draft], prepareRecordsOnly: false});

const {draft: updatedDraft, error} = await updateDraftMarkdownImageMetadata({
serverUrl,
channelId,
rootId: '',
imageMetadata,
});

expect(error).toBeUndefined();
expect(updatedDraft).toBeDefined();
expect(updatedDraft?.metadata?.images).toEqual(imageMetadata);
});
});
94 changes: 94 additions & 0 deletions app/actions/local/draft.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {DeviceEventEmitter, Image} from 'react-native';

import {Navigation, Screens} from '@constants';
import DatabaseManager from '@database/manager';
import {getDraft} from '@queries/servers/drafts';
import {goToScreen} from '@screens/navigation';
import {isTablet} from '@utils/helpers';
import {logError} from '@utils/log';

export const switchToGlobalDrafts = async () => {
const isTablelDevice = isTablet();
if (isTablelDevice) {
DeviceEventEmitter.emit(Navigation.NAVIGATION_HOME, Screens.GLOBAL_DRAFTS);
} else {
goToScreen(Screens.GLOBAL_DRAFTS, '', {}, {topBar: {visible: false}});
}
};

export async function updateDraftFile(serverUrl: string, channelId: string, rootId: string, file: FileInfo, prepareRecordsOnly = false) {
try {
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
Expand Down Expand Up @@ -197,3 +211,83 @@ export async function updateDraftPriority(serverUrl: string, channelId: string,
return {error};
}
}

export async function updateDraftMarkdownImageMetadata({
serverUrl,
channelId,
rootId,
imageMetadata,
prepareRecordsOnly = false,
}: {
serverUrl: string;
channelId: string;
rootId: string;
imageMetadata: Dictionary<PostImage | undefined>;
prepareRecordsOnly?: boolean;
}) {
try {
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const draft = await getDraft(database, channelId, rootId);
if (draft) {
draft.prepareUpdate((d) => {
d.metadata = {
...d.metadata,
images: imageMetadata,
};
d.updateAt = Date.now();
});
if (!prepareRecordsOnly) {
await operator.batchRecords([draft], 'updateDraftImageMetadata');
}
}
return {draft};
} catch (error) {
logError('Failed updateDraftImages', error);
return {error};
}
}

async function getImageMetadata(url: string) {
let height = 0;
let width = 0;
let format;
try {
await new Promise((resolve, reject) => {
Image.getSize(
url,
(imageWidth, imageHeight) => {
width = imageWidth;
height = imageHeight;
resolve(null);
},
(error) => {
logError('Failed to get image size', error);
reject(error);
},
);
});
} catch (error) {
width = 0;
height = 0;
}
const match = url.match(/\.(\w+)(?=\?|$)/);
if (match) {
format = match[1];
}
return {
height,
width,
format,
frame_count: 1,
};
}

export async function parseMarkdownImages(markdown: string, imageMetadata: Dictionary<PostImage | undefined>) {
let match;
const imageRegex = /!\[.*?\]\((https:\/\/[^\s)]+)\)/g;
while ((match = imageRegex.exec(markdown)) !== null) {
const imageUrl = match[1];
// eslint-disable-next-line no-await-in-loop
imageMetadata[imageUrl] = await getImageMetadata(imageUrl);
}
}
Loading