diff --git a/src/components/common/VnDmsList.vue b/src/components/common/VnDmsList.vue index 1040c6791..424781a26 100644 --- a/src/components/common/VnDmsList.vue +++ b/src/components/common/VnDmsList.vue @@ -17,7 +17,7 @@ import { useSession } from 'src/composables/useSession'; const route = useRoute(); const quasar = useQuasar(); const { t } = useI18n(); -const rows = ref(); +const rows = ref([]); const dmsRef = ref(); const formDialog = ref({}); const token = useSession().getTokenMultimedia(); @@ -389,6 +389,14 @@ defineExpose({ +
+
+ {{ t('No data to display') }} +
+
diff --git a/src/components/common/__tests__/VnNotes.spec.js b/src/components/common/__tests__/VnNotes.spec.js index 8f24a7f14..2603bf03c 100644 --- a/src/components/common/__tests__/VnNotes.spec.js +++ b/src/components/common/__tests__/VnNotes.spec.js @@ -1,51 +1,78 @@ -import { describe, it, expect, vi, beforeAll, afterEach, beforeEach } from 'vitest'; +import { + describe, + it, + expect, + vi, + beforeAll, + afterEach, + beforeEach, + afterAll, +} from 'vitest'; import { createWrapper, axios } from 'app/test/vitest/helper'; import VnNotes from 'src/components/ui/VnNotes.vue'; +import vnDate from 'src/boot/vnDate'; describe('VnNotes', () => { let vm; let wrapper; let spyFetch; let postMock; - let expectedBody; - const mockData= {name: 'Tony', lastName: 'Stark', text: 'Test Note', observationTypeFk: 1}; - - function generateExpectedBody() { - expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; - } - - async function setTestParams(text, observationType, type){ - vm.newNote.text = text; - vm.newNote.observationTypeFk = observationType; - wrapper.setProps({ selectType: type }); - } - - beforeAll(async () => { - vi.spyOn(axios, 'get').mockReturnValue({ data: [] }); - + let patchMock; + let expectedInsertBody; + let expectedUpdateBody; + const defaultOptions = { + url: '/test', + body: { name: 'Tony', lastName: 'Stark' }, + selectType: false, + saveUrl: null, + justInput: false, + }; + function generateWrapper( + options = defaultOptions, + text = null, + observationType = null, + ) { + vi.spyOn(axios, 'get').mockResolvedValue({ data: [] }); wrapper = createWrapper(VnNotes, { - propsData: { - url: '/test', - body: { name: 'Tony', lastName: 'Stark' }, - } + propsData: options, }); wrapper = wrapper.wrapper; vm = wrapper.vm; - }); + vm.newNote.text = text; + vm.newNote.observationTypeFk = observationType; + } + + function createSpyFetch() { + spyFetch = vi.spyOn(vm.$refs.vnPaginateRef, 'fetch'); + } + + function generateExpectedBody() { + expectedInsertBody = { + ...vm.$props.body, + ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }, + }; + expectedUpdateBody = { ...vm.$props.body, ...{ notes: vm.newNote.text } }; + } beforeEach(() => { - postMock = vi.spyOn(axios, 'post').mockResolvedValue(mockData); - spyFetch = vi.spyOn(vm.vnPaginateRef, 'fetch').mockImplementation(() => vi.fn()); + postMock = vi.spyOn(axios, 'post'); + patchMock = vi.spyOn(axios, 'patch'); }); afterEach(() => { vi.clearAllMocks(); - expectedBody = {}; + expectedInsertBody = {}; + expectedUpdateBody = {}; + }); + + afterAll(() => { + vi.restoreAllMocks(); }); describe('insert', () => { - it('should not call axios.post and vnPaginateRef.fetch if newNote.text is null', async () => { - await setTestParams( null, null, true ); + it('should not call axios.post and vnPaginateRef.fetch when newNote.text is null', async () => { + generateWrapper({ selectType: true }); + createSpyFetch(); await vm.insert(); @@ -53,8 +80,9 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); - it('should not call axios.post and vnPaginateRef.fetch if newNote.text is empty', async () => { - await setTestParams( "", null, false ); + it('should not call axios.post and vnPaginateRef.fetch when newNote.text is empty', async () => { + generateWrapper(null, ''); + createSpyFetch(); await vm.insert(); @@ -62,8 +90,9 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); - it('should not call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is true', async () => { - await setTestParams( "Test Note", null, true ); + it('should not call axios.post and vnPaginateRef.fetch when observationTypeFk is null and selectType is true', async () => { + generateWrapper({ selectType: true }, 'Test Note'); + createSpyFetch(); await vm.insert(); @@ -71,37 +100,57 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); - it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is false', async () => { - await setTestParams( "Test Note", null, false ); - + it('should call axios.post and vnPaginateRef.fetch when observationTypeFk is missing and selectType is false', async () => { + generateWrapper(null, 'Test Note'); + createSpyFetch(); generateExpectedBody(); await vm.insert(); - expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); - expect(spyFetch).toHaveBeenCalled(); - }); - - it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is setted and selectType is false', async () => { - await setTestParams( "Test Note", 1, false ); - - generateExpectedBody(); - - await vm.insert(); - - expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); + expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody); expect(spyFetch).toHaveBeenCalled(); }); it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => { - await setTestParams( "Test Note", 1, true ); - + generateWrapper({ selectType: true }, 'Test Note', 1); + createSpyFetch(); generateExpectedBody(); - + await vm.insert(); - expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); + expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody); expect(spyFetch).toHaveBeenCalled(); }); }); -}); \ No newline at end of file + + describe('update', () => { + it('should call axios.patch with saveUrl when saveUrl is set and justInput is true', async () => { + generateWrapper({ + url: '/business', + justInput: true, + saveUrl: '/saveUrlTest', + }); + generateExpectedBody(); + + await vm.update(); + + expect(patchMock).toHaveBeenCalledWith(vm.$props.saveUrl, expectedUpdateBody); + }); + + it('should call axios.patch with url when saveUrl is not set and justInput is true', async () => { + generateWrapper({ + url: '/business', + body: { workerFk: 1110 }, + justInput: true, + }); + generateExpectedBody(); + + await vm.update(); + + expect(patchMock).toHaveBeenCalledWith( + `${vm.$props.url}/${vm.$props.body.workerFk}`, + expectedUpdateBody, + ); + }); + }); +}); diff --git a/src/components/ui/VnNotes.vue b/src/components/ui/VnNotes.vue index 1690a94ba..5b1d6e726 100644 --- a/src/components/ui/VnNotes.vue +++ b/src/components/ui/VnNotes.vue @@ -1,6 +1,6 @@