From b26d1f7945f684380cf09d825bafe0b155679c76 Mon Sep 17 00:00:00 2001 From: jtubau Date: Fri, 10 Jan 2025 14:59:29 +0100 Subject: [PATCH] test: refs #7073 add unit tests for VnDms component --- src/components/common/__tests__/VnDms.spec.js | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/components/common/__tests__/VnDms.spec.js diff --git a/src/components/common/__tests__/VnDms.spec.js b/src/components/common/__tests__/VnDms.spec.js new file mode 100644 index 000000000..cdb273fbb --- /dev/null +++ b/src/components/common/__tests__/VnDms.spec.js @@ -0,0 +1,130 @@ +import { createWrapper, axios, flushPromises } from 'app/test/vitest/helper'; +import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest'; +import VnDms from 'src/components/common/VnDms.vue'; + +class MockFormData { + constructor() { + this.entries = {}; + } + + append(key, value) { + if (!key) { + throw new Error('Key is required for FormData.append'); + } + this.entries[key] = value; + } + + get(key) { + return this.entries[key] || null; + } + + getAll() { + return this.entries; + } +} + +global.FormData = MockFormData; + +describe('VnDms', () => { + let wrapper; + let vm; + let postMock; + + const postResponseMock = { data: { success: true } }; + + const data = { + hasFile: true, + hasFileAttached: true, + reference: 'DMS-test', + warehouseFk: 1, + companyFk: 2, + dmsTypeFk: 3, + description: 'This is a test description', + files: { name: 'example.txt', content: new Blob(['file content'], { type: 'text/plain' })}, + }; + + const expectedBody = { + hasFile: true, + hasFileAttached: true, + reference: 'DMS-test', + warehouseId: 1, + companyId: 2, + dmsTypeId: 3, + description: 'This is a test description', + }; + + beforeAll(() => { + wrapper = createWrapper(VnDms, { + propsData: { + url: '/test', + formInitialData: { id: 1, reference: 'test' }, + model: 'Worker', + } + }); + wrapper = wrapper.wrapper; + vm = wrapper.vm; + vi.spyOn(vm, '$emit'); + }); + + beforeEach(() => { + postMock = vi.spyOn(axios, 'post').mockResolvedValue(postResponseMock); + vm.dms = data; + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + describe('mapperDms', () => { + it('should map DMS data correctly and add file to FormData', () => { + const [formData, params] = vm.mapperDms(data); + + expect(formData.get('example.txt')).toBe(data.files); + expect(expectedBody).toEqual(params.params); + }); + + it('should map DMS data correctly without file', () => { + delete data.files; + + const [formData, params] = vm.mapperDms(data); + + expect(formData.getAll()).toEqual({}); + expect(expectedBody).toEqual(params.params); + }); + }); + + describe('getUrl', () => { + it('should returns prop url when is set', async () => { + expect(vm.getUrl()).toBe('/test'); + }); + + it('should returns url dms/"props.formInitialData.id"/updateFile when prop url is null', async () => { + await wrapper.setProps({ url: null }); + expect(vm.getUrl()).toBe('dms/1/updateFile'); + }); + + it('should returns url "props.model"/"route.params.id"/uploadFile when formInitialData is null', async () => { + await wrapper.setProps({ formInitialData: null }); + vm.route.params.id = '123'; + expect(vm.getUrl()).toBe('Worker/123/uploadFile'); + }); + }); + + describe('save', () => { + it('should save data correctly', async () => { + await vm.save(); + expect(postMock).toHaveBeenCalledWith(vm.getUrl(), expect.any(FormData), { params: expectedBody }); + expect(wrapper.emitted('onDataSaved')).toBeTruthy(); + //expect(emitMock).toHaveBeenCalledWith('onDataSaved', expectedBody, postResponseMock); + }); + }); + + describe('defaultData', () => { + it('should set default data correctly', async () => { + await wrapper.setProps({ formInitialData: null }); + vm.defaultData(); + + expect(vm.dms.reference).toBe('123'); + }); + }); +}); \ No newline at end of file