Skip to content

Commit

Permalink
added unit test for app/components/draft/draft.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat-Dabade committed Nov 22, 2024
1 parent f2349ff commit b078a6b
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
181 changes: 181 additions & 0 deletions app/components/draft/draft.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';

import CompassIcon from '@components/compass_icon';
import FormattedText from '@components/formatted_text';
import {General} from '@constants';
import {renderWithEverything} from '@test/intl-test-helper';
import TestHelper from '@test/test_helper';

import Draft from './draft';

import type {Database} from '@nozbe/watermelondb';
import type ChannelModel from '@typings/database/models/servers/channel';
import type DraftModel from '@typings/database/models/servers/draft';

jest.mock('@components/formatted_text', () => jest.fn(() => null));
jest.mock('@components/formatted_time', () => jest.fn(() => null));
jest.mock('@components/compass_icon', () => jest.fn(() => null));

describe('Draft', () => {
let database: Database;

beforeAll(async () => {
const server = await TestHelper.setupServerDatabase();
database = server.database;
});
it('should render the draft with channel info and draft message', () => {
const props = {
channel: {type: General.OPEN_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel,
location: 'channel',
draft: {
updateAt: 1633024800000,
message: 'Hello, World!',
channelId: 'channel_id',
rootId: '',
files: [],
metadata: {},
} as unknown as DraftModel,
layoutWidth: 100,
isPostPriorityEnabled: false,
};
const {getByText} = renderWithEverything(
<Draft
channel={props.channel}
location={props.location}
draft={props.draft}
layoutWidth={props.layoutWidth}
isPostPriorityEnabled={props.isPostPriorityEnabled}
/>
, {database},
);

expect(FormattedText).toHaveBeenCalledWith(
expect.objectContaining({
id: 'channel_info.draft_in_channel',
defaultMessage: 'In:',
}),
expect.anything(),
);
expect(CompassIcon).toHaveBeenCalledWith(
expect.objectContaining({
name: 'globe',
}),
expect.anything(),
);
expect(getByText('Hello, World!')).toBeTruthy();
});

it('should match the file count', () => {
const props = {
channel: {type: General.OPEN_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel,
location: 'channel',
draft: {
updateAt: 1633024800000,
message: 'Hello, World!',
channelId: 'channel_id',
rootId: '',
files: [{
has_preview_image: false,
height: 0,
name: 'file1.txt',
extension: 'txt',
size: 64,
}, {
has_preview_image: false,
height: 0,
name: 'file2.pdf',
extension: 'txt',
size: 64,
}],
metadata: {},
} as unknown as DraftModel,
layoutWidth: 100,
isPostPriorityEnabled: false,
};
const {getAllByTestId} = renderWithEverything(
<Draft
channel={props.channel}
location={props.location}
draft={props.draft}
layoutWidth={props.layoutWidth}
isPostPriorityEnabled={props.isPostPriorityEnabled}
/>
, {database},
);
expect(getAllByTestId('file_attachment')).toHaveLength(2);
});

it('should render the draft with channel info and draft message for a thread', () => {
const props = {
channel: {type: General.OPEN_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel,
location: 'thread',
draft: {
updateAt: 1633024800000,
message: 'Hello, World!',
channelId: 'channel_id',
rootId: 'root_id',
files: [],
metadata: {},
} as unknown as DraftModel,
layoutWidth: 100,
isPostPriorityEnabled: false,
};
const {getByText} = renderWithEverything(
<Draft
channel={props.channel}
location={props.location}
draft={props.draft}
layoutWidth={props.layoutWidth}
isPostPriorityEnabled={props.isPostPriorityEnabled}
/>
, {database},
);

expect(FormattedText).toHaveBeenCalledWith(
expect.objectContaining({
id: 'channel_info.thread_in',
defaultMessage: 'Thread in:',
}),
expect.anything(),
);

expect(CompassIcon).toHaveBeenCalledWith(
expect.objectContaining({
name: 'globe',
}),
expect.anything(),
);
expect(getByText('Hello, World!')).toBeTruthy();
});

it('should render the draft with post priority', () => {
const props = {
channel: {type: General.OPEN_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel,
location: 'thread',
draft: {
updateAt: 1633024800000,
message: 'Hello, World!',
channelId: 'channel_id',
rootId: 'root_id',
files: [],
metadata: {priority: {priority: 'important', requested_ack: false}},
} as unknown as DraftModel,
layoutWidth: 100,
isPostPriorityEnabled: true,
};
const {getByText} = renderWithEverything(
<Draft
channel={props.channel}
location={props.location}
draft={props.draft}
layoutWidth={props.layoutWidth}
isPostPriorityEnabled={props.isPostPriorityEnabled}
/>
, {database},
);
expect(getByText('IMPORTANT')).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions app/components/files/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const Files = ({canDownloadFiles, failed, filesInfo, isReplyPost, layoutWidth, l
<View
style={[container, styles.marginTop]}
key={file.id}
testID='file_attachment'
>
<File
galleryIdentifier={galleryIdentifier}
Expand Down

0 comments on commit b078a6b

Please sign in to comment.