Skip to content

Commit

Permalink
Merge pull request #241 from TogetherCrew/feat/announcements-mvp-change
Browse files Browse the repository at this point in the history
Feat/announcements mvp changes
  • Loading branch information
mehdi-torabiv authored Jan 24, 2024
2 parents 08180ad + 59498f8 commit 08ad8ad
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ function TcPrivateMessageContainer({
<TcIconContainer>
<MdOutlineAnnouncement size={20} />
</TcIconContainer>
<TcText text="Private Message" variant="body1" fontWeight="700" />
<TcText
text="Private Message (optional)"
variant="body1"
fontWeight="700"
/>
<FormControlLabel
className="mx-auto md:mx-0"
control={
Expand All @@ -158,7 +162,6 @@ function TcPrivateMessageContainer({
}
label={
<div className="flex items-center space-x-1">
<TcText text="Direct Message (optional)" variant="body1" />
<TcIconWithTooltip
tooltipText={
'Community members who have their DMs open will receive a DM. Members who have their DMs closed, will receive a private message inside the server (only they can see it). Additionally, a public message will always be sent with instructions to verify the legitimacy of the bot and announcement by checking the bot ID.'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,89 +1,60 @@
import React from 'react';
import { render, fireEvent, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { render, screen } from '@testing-library/react';
import TcPublicMessageContainer from './TcPublicMessageContainer';
import { ChannelContext } from '../../../../context/ChannelContext';
import { TokenContext } from '../../../../context/TokenContext';

const mockChannels = [
{ channelId: '1131241242', title: 'Channel 1', subChannels: [] },
{ channelId: '1242512553', title: 'Channel 2', subChannels: [] },
];
const mockToken = {
accessToken: 'mockAccessToken',
refreshToken: 'mockRefreshToken',
};

const mockSelectedSubChannels = {
channel1: { '1131241242': true },
channel2: { '1242512553': true },
const mockCommunity = {
name: 'Test Community',
platforms: [],
id: 'mockCommunityId',
users: [],
avatarURL: 'mockAvatarURL',
};

const mockChannelContext = {
channels: mockChannels,
selectedSubChannels: mockSelectedSubChannels,
loading: false,
refreshData: jest.fn(),
handleSubChannelChange: jest.fn(),
handleSelectAll: jest.fn(),
updateSelectedSubChannels: jest.fn(),
const mockTokenContextValue = {
token: mockToken,
community: mockCommunity,
updateToken: jest.fn(),
updateCommunity: jest.fn(),
deleteCommunity: jest.fn(),
clearToken: jest.fn(),
};

describe('TcPublicMessageContainer Tests', () => {
// Helper function to render the component with the necessary context
const renderComponent = (handlePublicAnnouncements = jest.fn()) =>
describe('TcPublicMessageContainer', () => {
beforeEach(() => {
render(
<ChannelContext.Provider value={mockChannelContext}>
<TcPublicMessageContainer
handlePublicAnnouncements={handlePublicAnnouncements}
/>
</ChannelContext.Provider>
<TokenContext.Provider value={mockTokenContextValue}>
<TcPublicMessageContainer handlePublicAnnouncements={jest.fn()} />
</TokenContext.Provider>
);

test('renders the component without crashing', () => {
renderComponent();
expect(screen.getByText('Public Message')).toBeInTheDocument();
});

test('initial state is set correctly', () => {
renderComponent();
expect(screen.getByPlaceholderText('Write your message here')).toHaveValue(
''
);
it('renders the "Public Message" text', () => {
expect(screen.getByText(/Public Message/i)).toBeInTheDocument();
});

test('allows the user to enter a message', () => {
renderComponent();
const messageInput = screen.getByPlaceholderText(
'Write your message here'
) as HTMLInputElement;
fireEvent.change(messageInput, { target: { value: 'Test Message' } });
expect(messageInput.value).toBe('Test Message');
it('renders the "Send message to:" text', () => {
expect(screen.getByText(/Send message to:/i)).toBeInTheDocument();
});

test('select channels dropdown is rendered', () => {
renderComponent();
expect(screen.getByLabelText('Select Channels')).toBeInTheDocument();
it('renders the message about bot delivery', () => {
const message =
/Our bot will deliver the announcement across chosen channels with the necessary access to share the specified message\./i;
expect(screen.getByText(message)).toBeInTheDocument();
});

test('handlePublicAnnouncements is called with correct data', () => {
const handlePublicAnnouncementsMock = jest.fn();
renderComponent(handlePublicAnnouncementsMock);

// Assuming there is a way to select channels in your UI, simulate that
// For example, if there's a button to confirm channel selection:
// fireEvent.click(screen.getByText('Confirm Channels'));

// Simulate entering a message
const messageInput = screen.getByPlaceholderText(
'Write your message here'
) as HTMLInputElement;
fireEvent.change(messageInput, { target: { value: 'Test Message' } });

// Assuming the function is called on some action, like a form submission or button click
// fireEvent.click(screen.getByText('Submit'));
it('renders the "Write message here:" text', () => {
expect(screen.getByText(/Write message here:/i)).toBeInTheDocument();
});

// Check if handlePublicAnnouncementsMock was called correctly
// Expect the mock to have been called with expected message and channels data
// This will depend on how your component calls the handlePublicAnnouncements function
expect(handlePublicAnnouncementsMock).toHaveBeenCalledWith({
message: 'Test Message',
selectedChannels: expect.anything(), // Replace with specific expectation
});
it('renders the auto-generated safety message prompt', () => {
const message =
/If you don’t write a custom message then this auto-generated safety message wlll be sent out/i;
expect(screen.getByText(message)).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ import TcText from '../../../shared/TcText';
import { MdAnnouncement, MdExpandMore } from 'react-icons/md';
import TcIconContainer from '../TcIconContainer';
import TcSelect from '../../../shared/TcSelect';
import {
Accordion,
AccordionDetails,
AccordionSummary,
FormControl,
InputLabel,
} from '@mui/material';
import { FormControl, InputLabel } from '@mui/material';
import TcInput from '../../../shared/TcInput';
import TcPublicMessagePreviewDialog from './TcPublicMessagePreviewDialog';
import { ChannelContext } from '../../../../context/ChannelContext';
import TcPlatformChannelList from '../../../communitySettings/platform/TcPlatformChannelList';
import { IGuildChannels } from '../../../../utils/types';
import { DiscordData } from '../../../../pages/announcements/edit-announcements';
import TcPermissionHints from '../../../global/TcPermissionHints';
import { useToken } from '../../../../context/TokenContext';

export interface FlattenedChannel {
id: string;
Expand All @@ -43,6 +38,7 @@ function TcPublicMessageContainer({
const channelContext = useContext(ChannelContext);

const { channels, selectedSubChannels } = channelContext;
const { community } = useToken();

const flattenChannels = (channels: IGuildChannels[]): FlattenedChannel[] => {
let flattened: FlattenedChannel[] = [];
Expand Down Expand Up @@ -71,7 +67,9 @@ function TcPublicMessageContainer({
setSelectedChannels(flattenChannels(channels));
}, [channels, selectedSubChannels]);

const [message, setMessage] = useState<string>('');
const [message, setMessage] = useState<string>(
`This message was sent to you because you’re part of ${community?.name}. To verify the legitimacy of this message, see the official announcement here ⁠👥together-crew⁠ and verify the bot ID If you don’t want to receive any more private message from ${community?.name}, please adjust your settings here: https://app.togethercrew.com/community-settings/`
);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setMessage(event.target.value);
Expand Down Expand Up @@ -121,7 +119,7 @@ function TcPublicMessageContainer({
<div>
<TcText text="Send message to:" variant="subtitle1" />
<TcText
text="The announcement will be sent by the a bot which will have access to send the following message within the selected channels"
text="Our bot will deliver the announcement across chosen channels with the necessary access to share the specified message."
variant="caption"
className="text-gray-400"
/>
Expand All @@ -148,13 +146,20 @@ function TcPublicMessageContainer({
</div>
</TcSelect>
</FormControl>
<TcText text="Write message here:" variant="subtitle1" />
<div className="flex items-center space-x-1">
<TcText text="Write message here:" variant="subtitle1" />
<TcText
text="If you don’t write a custom message then this auto-generated safety message wlll be sent out"
variant="caption"
className="text-gray-400"
/>
</div>
<FormControl variant="filled" fullWidth size="medium">
<TcInput
label="Message"
variant="filled"
placeholder="Write your message here"
rows={2}
rows={3}
multiline
value={message}
onChange={handleChange}
Expand Down
2 changes: 1 addition & 1 deletion src/components/layouts/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const Sidebar = () => {
>
{el.icon}
</div>
<p className="text-center text-sm">{el.name}</p>
<p className="text-center text-sm break-words">{el.name}</p>
</Link>
</li>
));
Expand Down
2 changes: 1 addition & 1 deletion src/context/TokenContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type TokenContextType = {
clearToken: () => void;
};

const TokenContext = createContext<TokenContextType | null>(null);
export const TokenContext = createContext<TokenContextType | null>(null);

type TokenProviderProps = {
children: ReactNode;
Expand Down

0 comments on commit 08ad8ad

Please sign in to comment.