Skip to content

Commit

Permalink
tests: add ModalSendFlow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Nov 26, 2024
1 parent 67c13c8 commit 9e88921
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/components/chats/FlowsTrigger/ModalSendFlow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
<UnnnicModal
:text="$t('flows_trigger.send')"
class="modal-send-flow"
data-testid="modal-send-flow"
@close="$emit('close')"
>
<SelectFlow v-model="selectedFlow" />
<SelectFlow
v-model="selectedFlow"
data-testid="select-flow"
/>

<template #options>
<SendFlowButton
class="modal-send-flow__handler"
:contacts="contacts"
:selectedFlow="selectedFlow"
data-testid="send-flow-button"
@send-flow-finished="finishSendFlow"
/>
</template>
Expand Down
70 changes: 70 additions & 0 deletions src/components/chats/FlowsTrigger/__tests__/ModalSendFlow.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { mount } from '@vue/test-utils';
import { expect, describe, it, beforeEach, vi } from 'vitest';

import ModalSendFlow from '../ModalSendFlow.vue';

import callUnnnicAlert from '@/utils/callUnnnicAlert';
vi.mock('@/utils/callUnnnicAlert');

vi.mock('@/services/api/resources/chats/flowsTrigger', () => ({
default: {
getFlows: vi.fn(() =>
Promise.resolve([{ uuid: 'flow-1', name: 'Flow 1' }]),
),
},
}));

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

beforeEach(() => {
wrapper = mount(ModalSendFlow, {
props: { contacts: [{ uuid: '1', name: 'Contact 1' }] },
});
});

it('renders correctly', () => {
expect(
wrapper.findComponent('[data-testid="modal-send-flow"]').exists(),
).toBe(true);

expect(wrapper.findComponent('[data-testid="select-flow"]').exists()).toBe(
true,
);

expect(
wrapper.findComponent('[data-testid="send-flow-button"]').exists(),
).toBe(true);
});

it('emits close event when modal is closed', async () => {
const modal = wrapper.findComponent('[data-testid="modal-send-flow"]');
await modal.vm.$emit('close');
expect(wrapper.emitted('close')).toHaveLength(1);
});

it('updates selectedFlow when a flow is selected', async () => {
const selectFlow = wrapper.findComponent('[data-testid="select-flow"]');

await selectFlow.vm.$emit('update:modelValue', 'flow-uuid-123');

expect(wrapper.vm.selectedFlow).toBe('flow-uuid-123');
});

it('shows success alert and emits send-flow-finished after sending flow', async () => {
const sendFlowButton = wrapper.findComponent(
'[data-testid="send-flow-button"]',
);
await sendFlowButton.vm.$emit('send-flow-finished');

expect(callUnnnicAlert).toHaveBeenCalledWith({
props: {
text: wrapper.vm.$t('flows_trigger.successfully_triggered'),
type: 'success',
},
seconds: 5,
});

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

0 comments on commit 9e88921

Please sign in to comment.