Skip to content

Commit

Permalink
test: add Document tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Nov 25, 2024
1 parent bfdb484 commit fcd9b24
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/chats/MediaMessage/Previews/Document.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
@keypress.enter="$emit('download')"
>
<UnnnicIconSvg
data-testid="icon"
:icon="icon"
:scheme="highlight ? 'weni-600' : 'neutral-darkest'"
/>
<span
data-testid="filename"
class="filename"
style="white-space: nowrap"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { mount } from '@vue/test-utils';
import { beforeAll, beforeEach, describe, expect, vi } from 'vitest';

import Document from '../Document.vue';

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

const defaultWindowOpen = window.open;

beforeEach(() => {
window.open = defaultWindowOpen;
wrapper = mount(Document, {
props: {
fullFilename: 'example.pdf',
url: 'https://example.com/file.pdf',
highlight: false,
size: 'md',
},
});
});

beforeAll(() => {
window.open = defaultWindowOpen;
});

it('renders the filename correctly', () => {
const filenameElement = wrapper.find('[data-testid="filename"]');
expect(filenameElement.text()).toBe('example.pdf');
});

it('renders the correct icon scheme', async () => {
const icon = wrapper.findComponent('[data-testid="icon"]');
expect(icon.exists()).toBe(true);
expect(icon.props().scheme).toBe('neutral-darkest');
await wrapper.setProps({ highlight: true });
expect(icon.props().scheme).toBe('weni-600');
});

it('emits "download" event when clicked', async () => {
window.open = vi.fn();
const openFileSpy = vi.spyOn(wrapper.vm, 'openFile');
await wrapper.trigger('click');
expect(wrapper.emitted('download')).toHaveLength(1);
expect(openFileSpy).toHaveBeenCalledWith(wrapper.vm.url);
vi.clearAllMocks();
});

it('emits "download" event when clicked', async () => {
window.open = vi.fn();
await wrapper.trigger('keypress', { key: 'Enter' });
expect(wrapper.emitted('download')).toHaveLength(1);
});
});

0 comments on commit fcd9b24

Please sign in to comment.