Skip to content

Commit

Permalink
tests: add SelectFlow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Nov 26, 2024
1 parent 01dd843 commit 36b93ca
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/components/chats/FlowsTrigger/SelectFlow.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<!-- eslint-disable vuejs-accessibility/click-events-have-key-events -->
<template>
<div class="select-flow">
<UnnnicLabel :label="$t('flows_trigger.select')" />
<div
class="select-flow"
data-testid="select-flow-container"
>
<UnnnicLabel
:label="$t('flows_trigger.select')"
data-testid="select-flow-label"
/>
<UnnnicSelectSmart
v-model="flowUuid"
:options="templates"
autocomplete
autocompleteIconLeft
autocompleteClearOnFocus
data-testid="select-flow-input"
@update:model-value="getFlowTrigger(flowUuid?.[0].value)"
/>
</div>
Expand Down
74 changes: 74 additions & 0 deletions src/components/chats/FlowsTrigger/__tests__/SelectFlow.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { mount } from '@vue/test-utils';
import { expect, describe, it, vi, beforeEach } from 'vitest';

import FlowsTrigger from '@/services/api/resources/chats/flowsTrigger';

import SelectFlow from '../SelectFlow.vue';

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

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

beforeEach(() => {
wrapper = mount(SelectFlow, { props: { modelValue: '' } });
});

it('renders with the correct initial state', () => {
expect(wrapper.find('[data-testid="select-flow-container"]').exists()).toBe(
true,
);
expect(
wrapper.findComponent('[data-testid="select-flow-input"]').exists(),
).toBe(true);
});

it('fetches flows on mount and populates options', async () => {
await wrapper.vm.$nextTick();
expect(FlowsTrigger.getFlows).toHaveBeenCalled();

const selectOptions = wrapper.vm.templates;
expect(selectOptions).toEqual([
{ value: '', label: wrapper.vm.$t('search_or_select') },
{ value: 'flow-1', label: 'Flow 1' },
]);
});

it('emits update:modelValue when a flow is selected', async () => {
await wrapper.setData({
templates: [
{ value: '', label: wrapper.vm.$t('search_or_select') },
{ value: 'flow-1', label: 'Flow 1' },
],
flowUuid: [{ value: 'flow-1', label: 'Flow 1' }],
});

await wrapper.vm.getFlowTrigger('flow-1');

expect(wrapper.emitted('update:modelValue')[0]).toEqual(['flow-1']);
});

it('handles API errors gracefully', async () => {
FlowsTrigger.getFlows.mockRejectedValueOnce(new Error('API Error'));
const consoleLogSpy = vi.spyOn(console, 'log');

const wrapper = mount(SelectFlow, {
props: { modelValue: '' },
mocks: { $t: (key) => key },
});

await wrapper.vm.$nextTick();

expect(wrapper.vm.loadingFlows).toBe(false);
expect(wrapper.vm.templates).toEqual([
{ value: '', label: wrapper.vm.$t('search_or_select') },
]);
expect(consoleLogSpy).toHaveBeenCalledWith(new Error('API Error'));
});
});

0 comments on commit 36b93ca

Please sign in to comment.