visua-vue/test/VAlert.spec.ts
2025-07-27 23:17:04 +02:00

61 lines
1.7 KiB
TypeScript

import { mount, flushPromises } from '@vue/test-utils';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import VAlert from '../src/components/alert/VAlert.vue';
import Toast from 'primevue/toast';
import ToastService from 'primevue/toastservice';
vi.mock('primevue/toastservice', () => ({
default: {
install: () => {},
add: vi.fn()
}
}));
describe('VAlert.vue', () => {
let wrapper: ReturnType<typeof mount>;
beforeEach(() => {
const container = document.createElement('div');
container.id = 'app';
document.body.appendChild(container);
wrapper = mount(VAlert, {
attachTo: '#app',
global: {
components: { Toast },
plugins: [ToastService]
},
props: {
position: 'top-right'
}
});
});
// 💡 Helper pour injecter un toast et attendre le rendu
const renderToast = async (payload: { severity: string; summary: string; detail: string; life: number }) => {
wrapper.vm.$toast?.add(payload);
await new Promise(resolve => setTimeout(resolve, 50));
await flushPromises();
};
it('renders the Toast container via teleport', async () => {
await renderToast({
severity: 'success',
summary: 'Test Title',
detail: 'Test description',
life: 3000
});
const toast = document.querySelector('.p-toast');
expect(toast).toBeTruthy();
});
it('responds to "close" and "life-end" events', async () => {
await wrapper.vm.$emit('close', { id: 'test-alert' });
await wrapper.vm.$emit('life-end', { id: 'test-alert' });
expect(wrapper.emitted()).toHaveProperty('close');
expect(wrapper.emitted()).toHaveProperty('life-end');
});
});