Merge branch 'dev' into 7050-testCrudModel
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
This commit is contained in:
commit
c27a7fc206
|
@ -0,0 +1,149 @@
|
||||||
|
import { describe, expect, it, beforeAll, vi, afterAll } from 'vitest';
|
||||||
|
import { createWrapper, axios } from 'app/test/vitest/helper';
|
||||||
|
import FormModel from 'src/components/FormModel.vue';
|
||||||
|
|
||||||
|
describe('FormModel', () => {
|
||||||
|
const model = 'mockModel';
|
||||||
|
const url = 'mockUrl';
|
||||||
|
const formInitialData = { mockKey: 'mockVal' };
|
||||||
|
|
||||||
|
describe('modelValue', () => {
|
||||||
|
it('should use the provided model', () => {
|
||||||
|
const { vm } = mount({ propsData: { model } });
|
||||||
|
expect(vm.modelValue).toBe(model);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should use the route meta title when model is not provided', () => {
|
||||||
|
const { vm } = mount({});
|
||||||
|
expect(vm.modelValue).toBe('formModel_mockTitle');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('onMounted()', () => {
|
||||||
|
let mockGet;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
mockGet = vi.spyOn(axios, 'get').mockResolvedValue({ data: {} });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
mockGet.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not fetch when has formInitialData', () => {
|
||||||
|
mount({ propsData: { url, model, autoLoad: true, formInitialData } });
|
||||||
|
expect(mockGet).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch when there is url and auto-load', () => {
|
||||||
|
mount({ propsData: { url, model, autoLoad: true } });
|
||||||
|
expect(mockGet).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not observe changes', () => {
|
||||||
|
const { vm } = mount({
|
||||||
|
propsData: { url, model, observeFormChanges: false, formInitialData },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(vm.hasChanges).toBe(true);
|
||||||
|
vm.reset();
|
||||||
|
expect(vm.hasChanges).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should observe changes', async () => {
|
||||||
|
const { vm } = mount({
|
||||||
|
propsData: { url, model, formInitialData },
|
||||||
|
});
|
||||||
|
vm.state.set(model, formInitialData);
|
||||||
|
expect(vm.hasChanges).toBe(false);
|
||||||
|
|
||||||
|
vm.formData.mockKey = 'newVal';
|
||||||
|
await vm.$nextTick();
|
||||||
|
expect(vm.hasChanges).toBe(true);
|
||||||
|
vm.formData.mockKey = 'mockVal';
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('trimData()', () => {
|
||||||
|
let vm;
|
||||||
|
beforeAll(() => {
|
||||||
|
vm = mount({}).vm;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should trim whitespace from string values', () => {
|
||||||
|
const data = { key1: ' value1 ', key2: ' value2 ' };
|
||||||
|
const trimmedData = vm.trimData(data);
|
||||||
|
expect(trimmedData).toEqual({ key1: 'value1', key2: 'value2' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not modify non-string values', () => {
|
||||||
|
const data = { key1: 123, key2: true, key3: null, key4: undefined };
|
||||||
|
const trimmedData = vm.trimData(data);
|
||||||
|
expect(trimmedData).toEqual(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('save()', async () => {
|
||||||
|
it('should not call if there are not changes', async () => {
|
||||||
|
const { vm } = mount({ propsData: { url, model } });
|
||||||
|
|
||||||
|
await vm.save();
|
||||||
|
expect(vm.hasChanges).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call axios.patch with the right data', async () => {
|
||||||
|
const spy = vi.spyOn(axios, 'patch').mockResolvedValue({ data: {} });
|
||||||
|
const { vm } = mount({ propsData: { url, model, formInitialData } });
|
||||||
|
vm.formData.mockKey = 'newVal';
|
||||||
|
await vm.$nextTick();
|
||||||
|
await vm.save();
|
||||||
|
expect(spy).toHaveBeenCalled();
|
||||||
|
vm.formData.mockKey = 'mockVal';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call axios.post with the right data', async () => {
|
||||||
|
const spy = vi.spyOn(axios, 'post').mockResolvedValue({ data: {} });
|
||||||
|
const { vm } = mount({
|
||||||
|
propsData: { url, model, formInitialData, urlCreate: 'mockUrlCreate' },
|
||||||
|
});
|
||||||
|
vm.formData.mockKey = 'newVal';
|
||||||
|
await vm.$nextTick();
|
||||||
|
await vm.save();
|
||||||
|
expect(spy).toHaveBeenCalled();
|
||||||
|
vm.formData.mockKey = 'mockVal';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should use the saveFn', async () => {
|
||||||
|
const { vm } = mount({
|
||||||
|
propsData: { url, model, formInitialData, saveFn: () => {} },
|
||||||
|
});
|
||||||
|
const spyPatch = vi.spyOn(axios, 'patch').mockResolvedValue({ data: {} });
|
||||||
|
const spySaveFn = vi.spyOn(vm.$props, 'saveFn');
|
||||||
|
|
||||||
|
vm.formData.mockKey = 'newVal';
|
||||||
|
await vm.$nextTick();
|
||||||
|
await vm.save();
|
||||||
|
expect(spyPatch).not.toHaveBeenCalled();
|
||||||
|
expect(spySaveFn).toHaveBeenCalled();
|
||||||
|
vm.formData.mockKey = 'mockVal';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reload the data after save', async () => {
|
||||||
|
const { vm } = mount({
|
||||||
|
propsData: { url, model, formInitialData, reload: true },
|
||||||
|
});
|
||||||
|
vi.spyOn(axios, 'patch').mockResolvedValue({ data: {} });
|
||||||
|
|
||||||
|
vm.formData.mockKey = 'newVal';
|
||||||
|
await vm.$nextTick();
|
||||||
|
await vm.save();
|
||||||
|
vm.formData.mockKey = 'mockVal';
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function mount({ propsData = {} }) {
|
||||||
|
return createWrapper(FormModel, {
|
||||||
|
propsData,
|
||||||
|
});
|
||||||
|
}
|
|
@ -26,7 +26,7 @@ const locationProperties = [
|
||||||
(obj) => obj.country?.name,
|
(obj) => obj.country?.name,
|
||||||
];
|
];
|
||||||
|
|
||||||
const formatLocation = (obj, properties) => {
|
const formatLocation = (obj, properties = locationProperties) => {
|
||||||
const parts = properties.map((prop) => {
|
const parts = properties.map((prop) => {
|
||||||
if (typeof prop === 'string') {
|
if (typeof prop === 'string') {
|
||||||
return obj[prop];
|
return obj[prop];
|
||||||
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
import { createWrapper } from 'app/test/vitest/helper';
|
||||||
|
import VnLocation from 'components/common/VnLocation.vue';
|
||||||
|
import { vi, afterEach, expect, it, beforeEach, describe } from 'vitest';
|
||||||
|
|
||||||
|
function buildComponent(data) {
|
||||||
|
return createWrapper(VnLocation, {
|
||||||
|
global: {
|
||||||
|
props: {
|
||||||
|
location: data
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}).vm;
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('formatLocation', () => {
|
||||||
|
let locationBase;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
locationBase = {
|
||||||
|
postcode: '46680',
|
||||||
|
city: 'Algemesi',
|
||||||
|
province: { name: 'Valencia' },
|
||||||
|
country: { name: 'Spain' }
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the postcode, city, province and country', () => {
|
||||||
|
const location = { ...locationBase };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.formatLocation(location)).toEqual('46680, Algemesi(Valencia), Spain');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the postcode and country', () => {
|
||||||
|
const location = { ...locationBase, city: undefined };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.formatLocation(location)).toEqual('46680, Spain');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the city, province and country', () => {
|
||||||
|
const location = { ...locationBase, postcode: undefined };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.formatLocation(location)).toEqual('Algemesi(Valencia), Spain');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the country', () => {
|
||||||
|
const location = { ...locationBase, postcode: undefined, city: undefined, province: undefined };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.formatLocation(location)).toEqual('Spain');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('showLabel', () => {
|
||||||
|
let locationBase;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
locationBase = {
|
||||||
|
code: '46680',
|
||||||
|
town: 'Algemesi',
|
||||||
|
province: 'Valencia',
|
||||||
|
country: 'Spain'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show the label with postcode, city, province and country', () => {
|
||||||
|
const location = { ...locationBase };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.showLabel(location)).toEqual('46680, Algemesi(Valencia), Spain');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show the label with postcode and country', () => {
|
||||||
|
const location = { ...locationBase, town: undefined };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.showLabel(location)).toEqual('46680, Spain');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show the label with city, province and country', () => {
|
||||||
|
const location = { ...locationBase, code: undefined };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.showLabel(location)).toEqual('Algemesi(Valencia), Spain');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show the label with country', () => {
|
||||||
|
const location = { ...locationBase, code: undefined, town: undefined, province: undefined };
|
||||||
|
const vm = buildComponent(location);
|
||||||
|
expect(vm.showLabel(location)).toEqual('Spain');
|
||||||
|
});
|
||||||
|
});
|
|
@ -26,7 +26,7 @@ vi.mock('vue-router', () => ({
|
||||||
params: {
|
params: {
|
||||||
id: 1,
|
id: 1,
|
||||||
},
|
},
|
||||||
meta: { moduleName: 'mockName' },
|
meta: { moduleName: 'mockModuleName' },
|
||||||
matched: [{ path: 'mockName/list' }],
|
matched: [{ path: 'mockName/list' }],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -35,7 +35,7 @@ vi.mock('vue-router', () => ({
|
||||||
matched: [],
|
matched: [],
|
||||||
query: {},
|
query: {},
|
||||||
params: {},
|
params: {},
|
||||||
meta: { moduleName: 'mockName' },
|
meta: { moduleName: 'mockModuleName', title: 'mockTitle', name: 'mockName' },
|
||||||
path: 'mockSection/list',
|
path: 'mockSection/list',
|
||||||
}),
|
}),
|
||||||
onBeforeRouteLeave: () => {},
|
onBeforeRouteLeave: () => {},
|
||||||
|
|
Loading…
Reference in New Issue