import { flushPromises, mount } from '@vue/test-utils'; import { PRICING_URL } from '@/constants'; import UploadDicomToCloudModal from '@/components/demo/UploadDicomToCloudModal.vue'; import { VBtn, VCard, VDialog } from 'vuetify/components'; import { Upload } from '@3cr/viewer-types-ts'; import { v4 } from 'uuid'; import { useViewerStore } from '@/stores/viewer.store'; import { storeToRefs } from 'pinia'; vi.mock('uuid', async (importOriginal) => { return { ...(await importOriginal()), v4: vi.fn(), }; }); describe('UploadDicomToCloudModal tests', () => { it('should mount', () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props }); expect(wrapper).toBeTruthy(); }); it('should close modal', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props }); const close = wrapper.findComponentByTestId('close'); await close.trigger('click'); expect(wrapper.emitted('update:modal')).toStrictEqual([[false]]); }); it('should close modal via cancel', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props }); const close = wrapper.findComponentByTestId('cancel'); await close.trigger('click'); expect(wrapper.emitted('update:modal')).toStrictEqual([[false]]); }); it('should close modal', async () => { const props = { modal: false }; const wrapper = mount(UploadDicomToCloudModal, { props }); const dialog = wrapper.findComponent(VDialog); await dialog.setValue(true); expect(wrapper.emitted('update:modal')).toStrictEqual([[true]]); }); it('should open pricing url', async () => { vi.stubGlobal('open', vi.fn()); const spy = vi.spyOn(window, 'open'); const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props }); const button = wrapper.findComponentByTestId('done'); await button.trigger('click'); expect(spy).toHaveBeenCalledWith(PRICING_URL, '_self'); vi.restoreAllMocks(); }); it('should get current progress 50%', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const progress = { progress: 0.5, } as Upload; expect(wrapper.vm.getProgress(progress)).toBe(50); }); it('should get current progress 95%', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const progress = { progress: 0.95, } as Upload; expect(wrapper.vm.getProgress(progress)).toBe(95); }); it('should get current progress 0', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const progress = {} as Upload; expect(wrapper.vm.getProgress(progress)).toBe(0); }); it('should delete no file', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const parent = wrapper.findComponent(VCard); expect(parent.findAll('.file-item').length).toBe(0); const file = { id: '123', } as Upload; wrapper.vm.deleteFile(file); await flushPromises(); expect(parent.findAll('.file-item').length).toBe(0); }); it('should execute interval n + 1 times', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const numRevolutions = 10; const intervalId = 0; const clearIntervalSpy = vi.spyOn(window, 'clearInterval'); const spy = vi .spyOn(window, 'setInterval') .mockImplementation((fn: any): any => { // Run this after the interval has been returned setTimeout(() => { for (let i = 0; i < numRevolutions; i++) { fn(); } fn(); expect(clearIntervalSpy).toHaveBeenCalledWith(intervalId); }, 1); return intervalId; }); await wrapper.vm.handleFilesDropped([new File([], 'something different')]); expect(spy).toHaveBeenCalled(); }); it('should delete 1 file', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const mockedId = '123'; const parent = wrapper.findComponent(VCard); vi.mocked(v4).mockReturnValue(mockedId as any); await wrapper.vm.handleFilesDropped([new File([], 'something different')]); await flushPromises(); expect(parent.findAll('.file-item').length).toBe(1); const file = { id: mockedId, } as Upload; wrapper.vm.deleteFile(file); await flushPromises(); expect(parent.findAll('.file-item').length).toBe(0); }); it('should delete 1 file through template', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const mockedId = '123'; const parent = wrapper.findComponent(VCard); vi.mocked(v4).mockReturnValue(mockedId as any); await wrapper.vm.handleFilesDropped([new File([], 'something different')]); await flushPromises(); expect(parent.findAll('.file-item').length).toBe(1); await parent.findAllComponents(VBtn)[1].trigger('click'); await flushPromises(); expect(parent.findAll('.file-item').length).toBe(0); }); it('should retry 1 file', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const mockedId = '123'; const parent = wrapper.findComponent(VCard); vi.mocked(v4).mockReturnValue(mockedId as any); await wrapper.vm.handleFilesDropped([new File([], 'something different')]); await flushPromises(); const file = { id: mockedId, status: 'failed', } as Upload; wrapper.vm.retryUpload(file); await flushPromises(); expect(parent.findAll('.file-item').length).toBe(1); }); it('should retry 1 file through template', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const mockedId = '123'; const parent = wrapper.findComponent(VCard); const file = { id: mockedId, status: 'failed', } as Upload; wrapper.vm.droppedFiles.push(file); await flushPromises(); await parent.findAll('a')[0].trigger('click'); await flushPromises(); expect(parent.findAll('.file-item').length).toBe(1); }); it('should show uploading linear', async () => { const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const mockedId = '123'; const parent = wrapper.findComponent(VCard); const file = { id: mockedId, title: 'hello world', status: 'uploading', } as Upload; wrapper.vm.droppedFiles.push(file); await flushPromises(); expect(parent.find('.progress-container').exists()).toBe(true); }); it('should onComplete', async () => { const { options } = storeToRefs(useViewerStore()); options.value.onCloudStorage = vi.fn(); const props = { modal: true }; const wrapper = mount(UploadDicomToCloudModal, { props, global: { stubs: { VProgressLinear: true, }, }, }); const ext = [ new File([], 'something different.dcm'), new File([], 'something different.notExt'), ]; await wrapper.vm.handleFilesDropped(ext); expect(options.value.onCloudStorage).toHaveBeenCalledWith([ext[0]]); }); });