Skip to content

Commit

Permalink
tests: add Media tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Nov 25, 2024
1 parent 26e6ec5 commit e5ca40f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/chats/MediaMessage/Previews/Media.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
icon="button-play-1"
size="md"
class="media-preview__video-play-icon"
data-testid="play-button"
/>
<video
ref="video"
:src="src"
:alt="alt"
class="media-preview__media"
muted
data-testid="media-video"
@click="handleClick"
@keypress.enter="handleClick"
/>
Expand All @@ -23,6 +25,7 @@
:src="src"
:alt="alt"
class="media-preview__media"
data-testid="media-image"
@click="handleClick"
@keypress.enter="handleClick"
/>
Expand Down
65 changes: 65 additions & 0 deletions src/components/chats/MediaMessage/Previews/__tests__/Media.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { mount } from '@vue/test-utils';
import { beforeEach, describe, expect } from 'vitest';

import Media from '../Media.vue';

describe('MediaPreview', () => {
let wrapper;

beforeEach(() => {
wrapper = mount(Media, {
props: {
src: 'https://example.com/media.mp4',
alt: 'Example media',
isVideo: false,
},
});
});

it('renders an image when isVideo is false', () => {
const img = wrapper.find('[data-testid="media-image"]');
expect(img.exists()).toBe(true);
expect(img.attributes('src')).toBe('https://example.com/media.mp4');
expect(img.attributes('alt')).toBe('Example media');

const video = wrapper.find('[data-testid="media-video"]');
expect(video.exists()).toBe(false);
});

it('renders a video and play icon when isVideo is true', async () => {
await wrapper.setProps({ isVideo: true });

const video = wrapper.find('[data-testid="media-video"]');
expect(video.exists()).toBe(true);
expect(video.attributes('src')).toBe('https://example.com/media.mp4');
expect(video.attributes('alt')).toBe('Example media');
const playIcon = wrapper.findComponent('[data-testid="play-button"]');
expect(playIcon.exists()).toBe(true);
});

it('emits "click" event when the image is clicked', async () => {
const img = wrapper.find('[data-testid="media-image"]');
await img.trigger('click');
expect(wrapper.emitted('click')).toHaveLength(1);
});

it('emits "click" event when the video is clicked', async () => {
await wrapper.setProps({ isVideo: true });
const video = wrapper.find('[data-testid="media-video"]');
await video.trigger('click');
expect(wrapper.emitted('click')).toHaveLength(1);
});

it('emits "click" event when the Enter key is pressed on an image', async () => {
const img = wrapper.find('[data-testid="media-image"]');
await img.trigger('keypress', { key: 'Enter' });
expect(wrapper.emitted('click')).toHaveLength(1);
});

it('emits "click" event when the Enter key is pressed on a video', async () => {
await wrapper.setProps({ isVideo: true });
const video = wrapper.find('[data-testid="media-video"]');
await video.trigger('keypress', { key: 'Enter' });
expect(wrapper.emitted('click')).toHaveLength(1);
});
});

0 comments on commit e5ca40f

Please sign in to comment.