-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0a2645
commit 01dd843
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/components/chats/FlowsTrigger/__tests__/SendFlowButton.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |