Skip to content

Commit

Permalink
tests: add SendFlowButton tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Nov 26, 2024
1 parent e0a2645 commit 01dd843
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/components/chats/FlowsTrigger/SendFlowButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
size="small"
type="primary"
iconLeft="send"
data-testid="send-flow-button"
@click="sendFlow"
/>
</template>
Expand Down
91 changes: 91 additions & 0 deletions src/components/chats/FlowsTrigger/__tests__/SendFlowButton.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { mount } from '@vue/test-utils';
import { expect, describe, it, vi, beforeEach } from 'vitest';

import SendFlowButton from '../SendFlowButton.vue';
import { createTestingPinia } from '@pinia/testing';
import FlowsTrigger from '@/services/api/resources/chats/flowsTrigger';

vi.mock('@/services/api/resources/chats/flowsTrigger', () => ({
default: { sendFlow: vi.fn() },
}));

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

beforeEach(() => {
wrapper = mount(SendFlowButton, {
global: { plugins: [createTestingPinia()] },
props: { selectedFlow: '' },
});
});

it('renders with the correct initial state', () => {
const button = wrapper.find('[data-testid="send-flow-button"]');
expect(button.exists()).toBe(true);
expect(button.attributes('disabled')).toBeDefined();
});

it('disables button when selectedFlow is empty', async () => {
const button = wrapper.find('[data-testid="send-flow-button"]');
expect(button.attributes('disabled')).toBeDefined();

await wrapper.setProps({ selectedFlow: 'flow-uuid' });
expect(button.attributes('disabled')).toBeUndefined();
});

it('emits events when sendFlow is called', async () => {
await wrapper.setProps({
selectedFlow: 'flow-uuid',
contacts: [{ external_id: 'contact-1' }],
});

await wrapper.find('[data-testid="send-flow-button"]').trigger('click');

expect(wrapper.emitted('send-flow-started')).toHaveLength(1);

expect(wrapper.emitted('send-flow-finished')).toHaveLength(1);
});

it('calls FlowsTrigger.sendFlow with correct parameters', async () => {
const contacts = [
{ external_id: 'contact-1', name: 'Contact 1' },
{ uuid: 'contact-2', name: 'Contact 2' },
];

await wrapper.setProps({ selectedFlow: 'flow-uuid', contacts });

await wrapper.find('[data-testid="send-flow-button"]').trigger('click');

expect(FlowsTrigger.sendFlow).toHaveBeenCalledWith({
flow: 'flow-uuid',
contacts: ['contact-1'],
room: undefined,
contact_name: 'Contact 1',
});

expect(FlowsTrigger.sendFlow).toHaveBeenCalledWith({
flow: 'flow-uuid',
contacts: ['contact-2'],
room: undefined,
contact_name: 'Contact 2',
});
});

it('handles errors gracefully during flow sending', async () => {
await wrapper.setProps({
selectedFlow: 'flow-uuid',
contacts: [{ external_id: 'contact-1' }],
selectedContact: { external_id: 'contact-1' },
});
const consoleError = vi.spyOn(console, 'error');

FlowsTrigger.sendFlow.mockRejectedValueOnce(new Error('API error'));

await wrapper.find('[data-testid="send-flow-button"]').trigger('click');

expect(FlowsTrigger.sendFlow).toHaveBeenCalled();
expect(consoleError).toHaveBeenCalledWith(new Error('API error'));

expect(wrapper.emitted('send-flow-finished')).toHaveLength(1);
});
});

0 comments on commit 01dd843

Please sign in to comment.