From 5f75d3a0f13923eac703001359027fbaa33223a5 Mon Sep 17 00:00:00 2001 From: provira Date: Tue, 21 Jan 2025 12:56:30 +0100 Subject: [PATCH 1/3] feat: refs #7065 created unit tests for UserPanel --- src/components/__tests__/UserPanel.spec.js | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/components/__tests__/UserPanel.spec.js diff --git a/src/components/__tests__/UserPanel.spec.js b/src/components/__tests__/UserPanel.spec.js new file mode 100644 index 000000000..55bc51e66 --- /dev/null +++ b/src/components/__tests__/UserPanel.spec.js @@ -0,0 +1,67 @@ +import { vi, describe, expect, it, beforeEach, beforeAll, afterEach } from 'vitest'; +import { createWrapper } from 'app/test/vitest/helper'; +import UserPanel from 'src/components/UserPanel.vue'; +import axios from 'axios'; +import { useState } from 'src/composables/useState'; + +vi.mock('axios'); + +describe('UserPanel', () => { + let wrapper; + let vm; + let state; + let axiosPatchMock; + let axiosPostMock; + + beforeAll(() => { + axiosPatchMock = vi.spyOn(axios, 'patch').mockResolvedValue({}); + axiosPostMock = vi.spyOn(axios, 'post').mockResolvedValue({}); + }); + + beforeEach(() => { + wrapper = createWrapper(UserPanel, {}); + state = useState(); + state.setUser({ + id: 115, + name: 'itmanagement', + nickname: 'itManagementNick', + lang: 'en', + darkMode: false, + companyFk: 442, + warehouseFk: 1, + }); + wrapper = wrapper.wrapper; + vm = wrapper.vm; + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should fetch warehouses data on mounted', async () => { + const fetchData = wrapper.findComponent({ name: 'FetchData' }); + expect(fetchData.props('url')).toBe('Warehouses'); + expect(fetchData.props('autoLoad')).toBe(true); + }); + + it('should toggle dark mode correctly and update preferences', async () => { + await vm.saveDarkMode(true); + expect(axiosPatchMock).toHaveBeenCalledWith('/UserConfigs/115', { darkMode: true }); + expect(vm.user.darkMode).toBe(true); + vm.updatePreferences(); + expect(vm.darkMode).toBe(true); + }); + + it('should change user language and update preferences', async () => { + await vm.saveLanguage('es'); + expect(axiosPatchMock).toHaveBeenCalledWith('/VnUsers/115', { lang: 'es' }); + expect(vm.user.lang).toBe('es'); + vm.updatePreferences(); + expect(vm.locale).toBe('es'); + }); + + it('should update user data', async () => { + await vm.saveUserData('name', 'itboss'); + expect(axiosPostMock).toHaveBeenCalledWith('UserConfigs/setUserConfig', { ['name']: 'itboss' }); + }); +}); -- 2.40.1 From c8cd14f9910b6b56258a0fea4dbd1ed8c13366a6 Mon Sep 17 00:00:00 2001 From: provira Date: Tue, 28 Jan 2025 10:07:51 +0100 Subject: [PATCH 2/3] fix: refs #7065 made consts for repeated values --- src/components/__tests__/UserPanel.spec.js | 24 +++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/components/__tests__/UserPanel.spec.js b/src/components/__tests__/UserPanel.spec.js index 55bc51e66..f77c7b318 100644 --- a/src/components/__tests__/UserPanel.spec.js +++ b/src/components/__tests__/UserPanel.spec.js @@ -10,13 +10,6 @@ describe('UserPanel', () => { let wrapper; let vm; let state; - let axiosPatchMock; - let axiosPostMock; - - beforeAll(() => { - axiosPatchMock = vi.spyOn(axios, 'patch').mockResolvedValue({}); - axiosPostMock = vi.spyOn(axios, 'post').mockResolvedValue({}); - }); beforeEach(() => { wrapper = createWrapper(UserPanel, {}); @@ -46,22 +39,25 @@ describe('UserPanel', () => { it('should toggle dark mode correctly and update preferences', async () => { await vm.saveDarkMode(true); - expect(axiosPatchMock).toHaveBeenCalledWith('/UserConfigs/115', { darkMode: true }); + expect(axios.patch).toHaveBeenCalledWith('/UserConfigs/115', { darkMode: true }); expect(vm.user.darkMode).toBe(true); vm.updatePreferences(); expect(vm.darkMode).toBe(true); }); it('should change user language and update preferences', async () => { - await vm.saveLanguage('es'); - expect(axiosPatchMock).toHaveBeenCalledWith('/VnUsers/115', { lang: 'es' }); - expect(vm.user.lang).toBe('es'); + const userLanguage = 'es'; + await vm.saveLanguage(userLanguage); + expect(axios.patch).toHaveBeenCalledWith('/VnUsers/115', { lang: userLanguage }); + expect(vm.user.lang).toBe(userLanguage); vm.updatePreferences(); - expect(vm.locale).toBe('es'); + expect(vm.locale).toBe(userLanguage); }); it('should update user data', async () => { - await vm.saveUserData('name', 'itboss'); - expect(axiosPostMock).toHaveBeenCalledWith('UserConfigs/setUserConfig', { ['name']: 'itboss' }); + const key = 'name'; + const value = 'itboss'; + await vm.saveUserData(key, value); + expect(axios.post).toHaveBeenCalledWith('UserConfigs/setUserConfig', { [key]: value }); }); }); -- 2.40.1 From 11cfca5d806371abf1498c9214b674af93b89c1a Mon Sep 17 00:00:00 2001 From: provira Date: Mon, 3 Feb 2025 11:43:48 +0100 Subject: [PATCH 3/3] fix: refs #7065 removed unnecessary code --- src/components/__tests__/UserPanel.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/__tests__/UserPanel.spec.js b/src/components/__tests__/UserPanel.spec.js index f77c7b318..ac20f911e 100644 --- a/src/components/__tests__/UserPanel.spec.js +++ b/src/components/__tests__/UserPanel.spec.js @@ -4,8 +4,6 @@ import UserPanel from 'src/components/UserPanel.vue'; import axios from 'axios'; import { useState } from 'src/composables/useState'; -vi.mock('axios'); - describe('UserPanel', () => { let wrapper; let vm; -- 2.40.1