Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 17 additions & 13 deletions src/components/Attachment/__tests__/Attachment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ const Media = (props) => <div data-testid='media-attachment'>{props.customTestId
const AttachmentActions = () => <div data-testid='attachment-actions'></div>;
const Image = (props) => <div data-testid='image-attachment'>{props.customTestId}</div>;
const File = (props) => <div data-testid='file-attachment'>{props.customTestId}</div>;
const Gallery = (props) => (
const ModalGallery = (props) => (
<div data-testid='gallery-attachment'>{props.customTestId}</div>
);
const Giphy = (props) => <div data-testid='giphy-attachment'>{props.customTestId}</div>;
const Geolocation = (props) => (
<div data-testid={'geolocation-attachment'}>{props.customTestId}</div>
);
Expand Down Expand Up @@ -62,10 +63,11 @@ const renderComponent = (props) =>
Audio={Audio}
Card={Card}
File={File}
Gallery={Gallery}
Geolocation={Geolocation}
Giphy={Giphy}
Image={Image}
Media={Media}
ModalGallery={ModalGallery}
{...props}
/>
</ChannelStateProvider>,
Expand Down Expand Up @@ -142,15 +144,11 @@ describe('attachment', () => {
expect(screen.getByTestId(UNSUPPORTED_ATTACHMENT_TEST_ID)).toBeInTheDocument();
});

const cases = [
const cardCases = [
{
attachments: [ATTACHMENTS.scraped.unrecognized],
case: 'not recognized, but has title_link or og_scrape_url',
},
{
attachments: [ATTACHMENTS.scraped.giphy],
case: 'giphy',
},
{
attachments: [ATTACHMENTS.scraped.image],
case: 'image',
Expand All @@ -165,18 +163,24 @@ describe('attachment', () => {
},
];
it.each`
attachments | case
${cases[0].attachments} | ${cases[0].case}
${cases[1].attachments} | ${cases[1].case}
${cases[2].attachments} | ${cases[2].case}
${cases[3].attachments} | ${cases[3].case}
${cases[4].attachments} | ${cases[4].case}
attachments | case
${cardCases[0].attachments} | ${cardCases[0].case}
${cardCases[1].attachments} | ${cardCases[1].case}
${cardCases[2].attachments} | ${cardCases[2].case}
${cardCases[3].attachments} | ${cardCases[3].case}
`('should render Card if attachment type is $case', async ({ attachments }) => {
renderComponent({ attachments });
await waitFor(() => {
expect(screen.getByTestId('card-attachment')).toBeInTheDocument();
});
});

it('should render Giphy if attachment type is giphy', async () => {
renderComponent({ attachments: [ATTACHMENTS.scraped.giphy] });
await waitFor(() => {
expect(screen.getByTestId('giphy-attachment')).toBeInTheDocument();
});
});
});

describe('combines scraped & uploaded content', () => {
Expand Down
27 changes: 20 additions & 7 deletions src/components/Attachment/__tests__/Audio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '@testing-library/jest-dom';

import { Audio } from '../Audio';
import { generateAudioAttachment, generateMessage } from '../../../mock-builders';
import { prettifyFileSize } from '../../MessageInput/hooks/utils';
import { prettifyFileSize } from '../../MessageComposer/hooks/utils';
import { WithAudioPlayback } from '../../AudioPlayback';
import { MessageProvider } from '../../../context';

Expand All @@ -14,6 +14,9 @@ jest.mock('../../../context/ChatContext', () => ({
jest.mock('../../../context/TranslationContext', () => ({
useTranslationContext: () => ({ t: (s) => tSpy(s) }),
}));
jest.mock('../../Notifications', () => ({
useNotificationTarget: () => 'channel',
}));

const addErrorSpy = jest.fn();
const mockClient = {
Expand Down Expand Up @@ -183,23 +186,33 @@ describe('Audio', () => {
});

it('registers error if pausing the audio after 2000ms of inactivity failed', async () => {
jest.useFakeTimers('modern');
jest.useFakeTimers({ now: Date.now() });
renderComponent({ og: audioAttachment });

jest
.spyOn(HTMLAudioElement.prototype, 'play')
.mockImplementationOnce(() => sleep(3000));
jest.spyOn(HTMLAudioElement.prototype, 'pause').mockImplementationOnce(() => {
throw new Error('');
});
const pauseSpy = jest
.spyOn(HTMLAudioElement.prototype, 'pause')
.mockImplementationOnce(() => {
throw new Error('');
});

await clickToPlay();

jest.advanceTimersByTime(2000);
await act(() => {
jest.advanceTimersByTime(2001);
});

// The safety timeout should have tried to pause and caught the error
await waitFor(() => {
expectAddErrorMessage('Failed to play the recording');
expect(pauseSpy).toHaveBeenCalled();
});

// After the error, the play button should be shown (not pause)
expect(playButton()).toBeInTheDocument();
expect(pauseButton()).not.toBeInTheDocument();

jest.useRealTimers();
});

Expand Down
115 changes: 8 additions & 107 deletions src/components/Attachment/__tests__/Card.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import React from 'react';
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { cleanup, render, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';

import { Card } from '../LinkPreview/Card';

import {
ChannelActionProvider,
MessageProvider,
TranslationContext,
} from '../../../context';
import { ChannelActionProvider, TranslationContext } from '../../../context';
import { ChannelStateProvider } from '../../../context/ChannelStateContext';
import { ChatProvider } from '../../../context/ChatContext';
import { ComponentProvider } from '../../../context/ComponentContext';
Expand All @@ -17,7 +13,6 @@ import {
generateChannel,
generateGiphyAttachment,
generateMember,
generateMessage,
generateUser,
getOrCreateChannelApi,
getTestClientWithUser,
Expand Down Expand Up @@ -286,112 +281,18 @@ describe('Card', () => {
});
});

it('should display trimmed URL in caption if author_name is not available', async () => {
const { getByText } = await renderCard({
it('should display URL in source link if author_name is not available', async () => {
const ogScrapeUrl =
'https://www.theverge.com/2020/6/15/21291288/sony-ps5-software-user-interface-ui-design-dashboard-teaser-video';
const { getByTestId } = await renderCard({
cardProps: {
og_scrape_url:
'https://www.theverge.com/2020/6/15/21291288/sony-ps5-software-user-interface-ui-design-dashboard-teaser-video',
og_scrape_url: ogScrapeUrl,
title: 'test',
},
chatContext: { chatClient },
});
await waitFor(() => {
expect(getByText('theverge.com')).toBeInTheDocument();
});
});

it('differentiates between in thread and in channel audio player', async () => {
const createdAudios = []; //HTMLAudioElement[]
const RealAudio = window.Audio;
const spy = jest.spyOn(window, 'Audio').mockImplementation(function AudioMock(
...args
) {
const el = new RealAudio(...args);
createdAudios.push(el);
return el;
});

const audioAttachment = {
...dummyAttachment,
image_url: undefined,
thumb_url: undefined,
title: 'test',
type: 'audio',
};

const message = generateMessage();

render(
<ChatProvider value={{}}>
<ChannelStateProvider value={{}}>
<WithAudioPlayback allowConcurrentPlayback>
<MessageProvider value={{ message }}>
<Card {...audioAttachment} />
</MessageProvider>
<MessageProvider value={{ message, threadList: true }}>
<Card {...audioAttachment} />
</MessageProvider>
</WithAudioPlayback>
</ChannelStateProvider>
</ChatProvider>,
);
const playButtons = screen.queryAllByTestId('play-audio');
expect(playButtons.length).toBe(2);
await Promise.all(
playButtons.map(async (button) => {
await fireEvent.click(button);
}),
);
await waitFor(() => {
expect(createdAudios).toHaveLength(2);
});
spy.mockRestore();
});

it('keeps a single copy of audio player for the same requester', async () => {
const createdAudios = []; //HTMLAudioElement[]
const RealAudio = window.Audio;
const spy = jest.spyOn(window, 'Audio').mockImplementation(function AudioMock(
...args
) {
const el = new RealAudio(...args);
createdAudios.push(el);
return el;
});

const audioAttachment = {
...dummyAttachment,
image_url: undefined,
thumb_url: undefined,
title: 'test',
type: 'audio',
};

const message = generateMessage();
render(
<ChatProvider value={{}}>
<ChannelStateProvider value={{}}>
<WithAudioPlayback allowConcurrentPlayback>
<MessageProvider value={{ message }}>
<Card {...audioAttachment} />
</MessageProvider>
<MessageProvider value={{ message }}>
<Card {...audioAttachment} />
</MessageProvider>
</WithAudioPlayback>
</ChannelStateProvider>
</ChatProvider>,
);
const playButtons = screen.queryAllByTestId('play-audio');
expect(playButtons.length).toBe(2);
await Promise.all(
playButtons.map(async (button) => {
await fireEvent.click(button);
}),
);
await waitFor(() => {
expect(createdAudios).toHaveLength(1);
expect(getByTestId('card-source-link')).toBeInTheDocument();
});
spy.mockRestore();
});
});
Loading
Loading