Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harshilsharma63 authored and Rajat-Dabade committed Feb 25, 2025
1 parent 12f1f50 commit c94555a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 91 deletions.
88 changes: 0 additions & 88 deletions app/components/drafts_buttton/__tests__/drafts_button.test.tsx

This file was deleted.

121 changes: 121 additions & 0 deletions app/components/drafts_buttton/drafts_button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import {DeviceEventEmitter} from 'react-native';

import {switchToGlobalDrafts} from '@actions/local/draft';
import DraftsButton from '@components/drafts_buttton/drafts_button';
import {Events} from '@constants';
import {DRAFT} from '@constants/screens';
import {renderWithIntlAndTheme} from '@test/intl-test-helper';
import {act, fireEvent} from '@testing-library/react-native';

jest.mock('@actions/local/draft', () => ({
switchToGlobalDrafts: jest.fn(),
}));

describe('components/drafts_button/DraftsButton', () => {
beforeEach(() => {
jest.clearAllMocks();
});

const baseProps = {
draftsCount: 0,
scheduledPostCount: 0,
scheduledPostHasError: false,
};

test('should show draft count when greater than 0', () => {
const props = {
...baseProps,
draftsCount: 5,
};

const {getByTestId} = renderWithIntlAndTheme(
<DraftsButton {...props}/>,
);

expect(getByTestId('channel_list.drafts.count')).toHaveTextContent('5');
});

test('should show scheduled post count when greater than 0', () => {
const props = {
...baseProps,
scheduledPostCount: 3,
};

const {getByTestId} = renderWithIntlAndTheme(
<DraftsButton {...props}/>,
);

expect(getByTestId('channel_list.scheduled_post.count')).toHaveTextContent('3');
});

test('should show both drafts and scheduled post count when greater than 0', () => {
const props = {
...baseProps,
scheduledPostCount: 3,
draftsCount: 5,
};

const {getByTestId} = renderWithIntlAndTheme(
<DraftsButton {...props}/>,
);

expect(getByTestId('channel_list.drafts.count')).toHaveTextContent('5');
expect(getByTestId('channel_list.scheduled_post.count')).toHaveTextContent('3');
});

test('should show both drafts and scheduled post count when greater than 0 and scheduled post has error', () => {
const props = {
...baseProps,
scheduledPostCount: 3,
draftsCount: 5,
scheduledPostHasError: true,
};

const {getByTestId} = renderWithIntlAndTheme(
<DraftsButton {...props}/>,
);

expect(getByTestId('channel_list.drafts.count')).toHaveTextContent('5');
expect(getByTestId('channel_list.scheduled_post.count')).toHaveTextContent('3');

const countElement = getByTestId('channel_list.scheduled_post.count.container');
expect(countElement).toBeVisible();
expect(countElement).toHaveStyle({backgroundColor: expect.any(String)});
});

test('should apply error styles when scheduled post has error', () => {
const props = {
...baseProps,
scheduledPostCount: 1,
scheduledPostHasError: true,
};

const {getByTestId} = renderWithIntlAndTheme(
<DraftsButton {...props}/>,
);

const countElement = getByTestId('channel_list.scheduled_post.count.container');
expect(countElement).toBeVisible();
expect(countElement).toHaveStyle({backgroundColor: expect.any(String)});
});

test('should handle press and emit events', () => {
const emitSpy = jest.spyOn(DeviceEventEmitter, 'emit');

const {getByTestId} = renderWithIntlAndTheme(
<DraftsButton {...baseProps}/>,
);

const button = getByTestId('channel_list.drafts.button');
act(() => {
fireEvent.press(button);
});

expect(emitSpy).toHaveBeenCalledWith(Events.ACTIVE_SCREEN, DRAFT);
expect(switchToGlobalDrafts).toHaveBeenCalled();
});
});
12 changes: 9 additions & 3 deletions app/components/drafts_buttton/drafts_button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ const DraftsButton: React.FC<DraftListProps> = ({
<View style={customStyles.countBadgeContainer}>
{
draftsCount > 0 &&
<View style={customStyles.countContainer}>
<View
testID='channel_list.drafts.count.container'
style={customStyles.countContainer}
>
<CompassIcon
name='pencil-outline'
size={14}
Expand All @@ -150,15 +153,18 @@ const DraftsButton: React.FC<DraftListProps> = ({
}
{
scheduledPostCount > 0 &&
<View style={[customStyles.countContainer, scheduledPostHasError && customStyles.errorCountContainer]}>
<View
testID='channel_list.scheduled_post.count.container'
style={[customStyles.countContainer, scheduledPostHasError && customStyles.errorCountContainer]}
>
<CompassIcon
name='clock-send-outline'
size={14}
color={theme.sidebarText}
style={scheduledPostHasError ? customStyles.badgeOpacityWithError : customStyles.opacity}
/>
<Text
testID='channel_list.schedued_post.count'
testID='channel_list.scheduled_post.count'
style={[customStyles.count, scheduledPostHasError && customStyles.badgeOpacityWithError]}
>
{scheduledPostCount}
Expand Down
10 changes: 10 additions & 0 deletions app/screens/home/channel_list/categories_list/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('components/categories_list', () => {
moreThanOneTeam={false}
hasChannels={true}
draftsCount={0}
scheduledPostHasError={false}
scheduledPostCount={0}
/>,
{database},
);
Expand All @@ -50,6 +52,8 @@ describe('components/categories_list', () => {
moreThanOneTeam={false}
hasChannels={true}
draftsCount={0}
scheduledPostCount={0}
scheduledPostHasError={false}
/>,
{database},
);
Expand All @@ -67,6 +71,8 @@ describe('components/categories_list', () => {
moreThanOneTeam={false}
hasChannels={true}
draftsCount={1}
scheduledPostCount={0}
scheduledPostHasError={false}
/>,
{database},
);
Expand All @@ -85,6 +91,8 @@ describe('components/categories_list', () => {
moreThanOneTeam={false}
hasChannels={true}
draftsCount={0}
scheduledPostCount={0}
scheduledPostHasError={false}
/>,
{database},
);
Expand All @@ -108,6 +116,8 @@ describe('components/categories_list', () => {
moreThanOneTeam={true}
hasChannels={false}
draftsCount={0}
scheduledPostCount={0}
scheduledPostHasError={false}
/>,
{database},
);
Expand Down

0 comments on commit c94555a

Please sign in to comment.