Skip to content

Commit

Permalink
tests: add SelectedContactsSection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Nov 26, 2024
1 parent 0c0cc65 commit 67c13c8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/chats/FlowsTrigger/SelectedContactsSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<section
v-if="contacts.length > 0"
class="selected-contacts-section"
data-testid="selected-contacts-section"
@click="$emit('click')"
@keypress.enter="$emit('click')"
>
Expand All @@ -12,6 +13,7 @@
:text="contact.name"
hasCloseIcon
scheme="background-snow"
data-testid="contact-tag"
@close="$emit('remove-contact', contact)"
/>
</section>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { mount } from '@vue/test-utils';
import { expect, describe, it, beforeEach } from 'vitest';

import SelectedContactsSection from '../SelectedContactsSection.vue';

const contactsMock = [
{ uuid: '1', name: 'Contact 1' },
{ uuid: '2', name: 'Contact 2' },
];

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

beforeEach(() => {
wrapper = mount(SelectedContactsSection, { props: { contacts: [] } });
});

it('renders only if contacts are provided', async () => {
expect(
wrapper.find('[data-testid="selected-contacts-section"]').exists(),
).toBe(false);
await wrapper.setProps({ contacts: [{ uuid: '1', name: 'Contact 1' }] });
expect(
wrapper.find('[data-testid="selected-contacts-section"]').exists(),
).toBe(true);
});

it('displays contacts as tags', async () => {
await wrapper.setProps({ contacts: contactsMock });

const tags = wrapper.findAllComponents('[data-testid="contact-tag"]');
expect(tags).toHaveLength(contactsMock.length);
expect(tags[0].props('text')).toBe(contactsMock[0].name);
expect(tags[1].props('text')).toBe(contactsMock[1].name);
});

it('emits remove-contact event with correct contact on tag close', async () => {
await wrapper.setProps({ contacts: [contactsMock[0]] });

const tag = wrapper.findComponent('[data-testid="contact-tag"]');

await tag.vm.$emit('close');

expect(wrapper.emitted('remove-contact')[0]).toEqual([contactsMock[0]]);
});

it('emits click event when section is clicked or Enter is pressed', async () => {
await wrapper.setProps({ contacts: [contactsMock[0]] });

await wrapper.trigger('click');
expect(wrapper.emitted('click')).toHaveLength(1);

await wrapper.trigger('keypress.enter');
expect(wrapper.emitted('click')).toHaveLength(2);
});
});

0 comments on commit 67c13c8

Please sign in to comment.