refactor: refs #6564 improve test structure and readability in UserPanel.spec.js
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2025-02-13 17:34:10 +01:00
parent 345dc4bfbc
commit 3c2c40c8e6
1 changed files with 50 additions and 52 deletions

View File

@ -1,65 +1,63 @@
import { vi, describe, expect, it, beforeEach, afterEach } from 'vitest'; import { vi, describe, expect, it, beforeEach, beforeAll, afterEach } from 'vitest';
import { createWrapper } from 'app/test/vitest/helper'; import { createWrapper } from 'app/test/vitest/helper';
import UserPanel from 'src/components/UserPanel.vue'; import UserPanel from 'src/components/UserPanel.vue';
import axios from 'axios'; import axios from 'axios';
import { useState } from 'src/composables/useState'; import { useState } from 'src/composables/useState';
vi.mock('src/utils/quasarLang', () => ({
default: vi.fn(),
}));
describe('UserPanel', () => { describe('UserPanel', () => {
let wrapper; let wrapper;
let vm; let vm;
let state; let state;
beforeEach(() => { beforeEach(() => {
wrapper = createWrapper(UserPanel, {}); wrapper = createWrapper(UserPanel, {});
state = useState(); state = useState();
state.setUser({ state.setUser({
id: 115, id: 115,
name: 'itmanagement', name: 'itmanagement',
nickname: 'itManagementNick', nickname: 'itManagementNick',
lang: 'en', lang: 'en',
darkMode: false, darkMode: false,
companyFk: 442, companyFk: 442,
warehouseFk: 1, warehouseFk: 1,
});
wrapper = wrapper.wrapper;
vm = wrapper.vm;
}); });
wrapper = wrapper.wrapper;
vm = wrapper.vm;
});
afterEach(() => { afterEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
}); });
it('should fetch warehouses data on mounted', async () => { it('should fetch warehouses data on mounted', async () => {
const fetchData = wrapper.findComponent({ name: 'FetchData' }); const fetchData = wrapper.findComponent({ name: 'FetchData' });
expect(fetchData.props('url')).toBe('Warehouses'); expect(fetchData.props('url')).toBe('Warehouses');
expect(fetchData.props('autoLoad')).toBe(true); expect(fetchData.props('autoLoad')).toBe(true);
}); });
it('should toggle dark mode correctly and update preferences', async () => { it('should toggle dark mode correctly and update preferences', async () => {
await vm.saveDarkMode(true); await vm.saveDarkMode(true);
expect(axios.patch).toHaveBeenCalledWith('/UserConfigs/115', { darkMode: true }); expect(axios.patch).toHaveBeenCalledWith('/UserConfigs/115', { darkMode: true });
expect(vm.user.darkMode).toBe(true); expect(vm.user.darkMode).toBe(true);
await vm.updatePreferences(); vm.updatePreferences();
expect(vm.darkMode).toBe(true); expect(vm.darkMode).toBe(true);
}); });
it('should change user language and update preferences', async () => { it('should change user language and update preferences', async () => {
const userLanguage = 'es'; const userLanguage = 'es';
await vm.saveLanguage(userLanguage); await vm.saveLanguage(userLanguage);
expect(axios.patch).toHaveBeenCalledWith('/VnUsers/115', { lang: userLanguage }); expect(axios.patch).toHaveBeenCalledWith('/VnUsers/115', { lang: userLanguage });
expect(vm.user.lang).toBe(userLanguage); expect(vm.user.lang).toBe(userLanguage);
await vm.updatePreferences(); vm.updatePreferences();
expect(vm.locale).toBe(userLanguage); expect(vm.locale).toBe(userLanguage);
}); });
it('should update user data', async () => { it('should update user data', async () => {
const key = 'name'; const key = 'name';
const value = 'itboss'; const value = 'itboss';
await vm.saveUserData(key, value); await vm.saveUserData(key, value);
expect(axios.post).toHaveBeenCalledWith('UserConfigs/setUserConfig', { [key]: value }); expect(axios.post).toHaveBeenCalledWith('UserConfigs/setUserConfig', {
}); [key]: value,
}); });
});
});