import { describe, it, expect, vi, afterEach } from 'vitest' import { mount } from '@vue/test-utils' import { nextTick } from 'vue' import UploadWorkflow from '../UploadWorkflow.vue' import { locales as fileListLocales } from '@/components/FileList/UploadItem/locales' import { locales as FileUploadLocales } from '@/components/FileUpload/locales' import { SySelect } from '@/components' describe('UploadWorkflow', () => { afterEach(() => { vi.clearAllMocks() document.body.innerHTML = '' }) it('render the upload list', async () => { const wrapper = mount(UploadWorkflow, { props: { uploadList: [ { id: 'ID', title: 'Carte d\'identité', }, { id: 'bill', title: 'Facture de soin', }, ], }, }) expect(wrapper.html()).toMatchSnapshot() expect(wrapper.findAll('.file-item')).toHaveLength(2) expect(wrapper.find('.sy-file-upload').isVisible()).toBeTruthy() }) it('handles empty upload list without crashing', async () => { const wrapper = mount(UploadWorkflow, { props: { uploadList: [], }, }) expect(wrapper.html()).toMatchSnapshot() expect(wrapper.findAll('.file-item')).toHaveLength(0) expect(wrapper.find('.sy-file-upload').exists()).toBeFalsy() }) it('handles undefined upload list without crashing', async () => { const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) const wrapper = mount(UploadWorkflow, { /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ props: {} as any, }) warnSpy.mockRestore() expect(wrapper.html()).toMatchSnapshot() expect(wrapper.findAll('.file-item')).toHaveLength(0) expect(wrapper.find('.sy-file-upload').exists()).toBeFalsy() }) it('shows the file in the list when set with the list button', async () => { const wrapper = mount(UploadWorkflow, { props: { uploadList: [ { id: 'ID', title: 'Carte d\'identité', }, { id: 'bill', title: 'Facture de soin', }, ], }, }) await wrapper.find('.file-item button').trigger('click') const file: File = new File([''], 'theFilename.pdf', { type: 'application/pdf', }) await wrapper.find('.sy-file-upload').trigger('drop', { dataTransfer: { files: [file], }, }) expect(wrapper.find('.file-item').text()).toContain('theFilename.pdf') expect(wrapper.emitted('update:modelValue')).toBeTruthy() }) it('shows the item as error in the list when set with the list button', async () => { const wrapper = mount(UploadWorkflow, { props: { uploadList: [ { id: 'ID', title: 'Carte d\'identité', }, { id: 'bill', title: 'Facture de soin', }, ], }, }) await wrapper.findAll('.file-item button')[1]?.trigger('click') const file: File = new File([''], 'theFilename.invalid', { type: 'application/invalid', }) const input = wrapper.find('input') input.element.files = [file] as unknown as FileList await input.trigger('change') expect(wrapper.emitted('error')).toEqual([ [ [ FileUploadLocales.errorExtension('theFilename.invalid', [ 'pdf', 'jpg', 'jpeg', 'png', ]), ], ], ]) expect(wrapper.findAll('.file-item')[1]?.text()).toContain( fileListLocales.error, ) }) it('accept the file when we use the FileUpload component with many items in the uploadList', async () => { const wrapper = mount(UploadWorkflow, { props: { uploadList: [ { id: 'ID', title: 'Carte d\'identité', }, { id: 'bill', title: 'Facture de soin', }, ], }, global: { stubs: { VDialog: { template: '