From efb0162f9ca7a565dcd0654465da70edc852f8b4 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 20 Jan 2025 10:58:49 +0100 Subject: [PATCH] feat: refs #7068 created VnVisibleColumns unit test --- .../__tests__/VnVisibleColumns.spec.js | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/components/VnTable/__tests__/VnVisibleColumns.spec.js diff --git a/src/components/VnTable/__tests__/VnVisibleColumns.spec.js b/src/components/VnTable/__tests__/VnVisibleColumns.spec.js new file mode 100644 index 000000000..3589ca676 --- /dev/null +++ b/src/components/VnTable/__tests__/VnVisibleColumns.spec.js @@ -0,0 +1,119 @@ +import { describe, expect, it, beforeEach, vi } from 'vitest'; +import { createWrapper } from 'app/test/vitest/helper'; +import VnVisibleColumn from '../VnVisibleColumn.vue'; +import { axios } from 'app/test/vitest/helper'; + +describe('VnVisibleColumns', () => { + let wrapper; + let vm; + let columnsMock = []; + beforeEach(() => { + wrapper = createWrapper(VnVisibleColumn, { + propsData: { + tableCode: 'testTable', + skip: ['skippedColumn'], + }, + }); + vm = wrapper.vm; + }); + + describe('setUserConfigViewData()', () => { + it('should initialize localColumns with visible configuration', () => { + columnsMock = [ + { name: 'columnMockName', label: undefined }, + { name: 'columnMockAddress', label: undefined }, + { name: 'columnMockId', label: undefined }, + ]; + vm.columns = columnsMock; + const configuration = { + columnMockName: true, + columnMockAddress: false, + columnMockId: true, + }; + + vm.setUserConfigViewData(configuration, false); + + expect(vm.localColumns).toEqual([ + { name: 'columnMockName', label: undefined, visible: true }, + { name: 'columnMockAddress', label: undefined, visible: false }, + { name: 'columnMockId', label: undefined, visible: true }, + ]); + }); + + it('should skip columns based on props', () => { + columnsMock = [ + { name: 'columnMockName', label: undefined }, + { name: 'columnMockId', label: undefined }, + { name: 'skippedColumn', label: 'Skipped Column' }, + ]; + const configuration = { + columnMockName: true, + skippedColumn: false, + columnMockId: true, + }; + vm.columns = columnsMock; + + vm.setUserConfigViewData(configuration, false); + + expect(vm.localColumns).toEqual([ + { name: 'columnMockName', label: undefined, visible: true }, + { name: 'columnMockId', label: undefined, visible: true }, + ]); + }); + }); + + describe('toggleMarkAll()', () => { + it('should set all localColumns to visible=true', () => { + vm.localColumns = [ + { name: 'columnMockName', visible: false }, + { name: 'columnMockId', visible: false }, + ]; + + vm.toggleMarkAll(true); + + expect(vm.localColumns.every((col) => col.visible)).toBe(true); + }); + + it('should set all localColumns to visible=false', () => { + vm.localColumns = [ + { name: 'columnMockName', visible: true }, + { name: 'columnMockId', visible: true }, + ]; + + vm.toggleMarkAll(false); + + expect(vm.localColumns.every((col) => col.visible)).toBe(false); + }); + }); + + describe('saveConfig()', () => { + it('should call setUserConfigViewData and axios.post with correct params', async () => { + const mockAxiosPost = vi.spyOn(axios, 'post').mockResolvedValue({ + data: [{ id: 1 }], + }); + + vm.localColumns = [ + { name: 'columnMockName', visible: true }, + { name: 'columnMockId', visible: false }, + ]; + + await vm.saveConfig(); + + expect(mockAxiosPost).toHaveBeenCalledWith('UserConfigViews/crud', { + creates: [ + { + userFk: vm.user.id, + tableCode: vm.tableCode, + tableConfig: vm.tableCode, + configuration: { + columnMockName: true, + columnMockId: false, + }, + }, + ], + }); + + mockAxiosPost.mockRestore(); + }); + }); +});