0
0
Fork 0

feat: refs #6893 improve VnSearchbar & create tests

This commit is contained in:
Jorge Penadés 2024-03-27 11:14:54 +01:00
parent 375461abd7
commit 20102a89ab
4 changed files with 69 additions and 34 deletions

View File

@ -10,11 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- (Tickets) => Se añade la opción de clonar ticket. #6951 - (Tickets) => Se añade la opción de clonar ticket. #6951
- (Parking) => Se añade la sección Parking. #5186
### Changed ### Changed
### Fixed ### Fixed
- (General) => Se corrige la redirección cuando hay 1 solo registro y cuando se aplica un filtro diferente al id al hacer una búsqueda general. #6893
## [2400.01] - 2024-01-04 ## [2400.01] - 2024-01-04
### Added ### Added

View File

@ -65,9 +65,9 @@ const props = defineProps({
}, },
}); });
const { currentRoute, push } = useRouter(); const router = useRouter();
const arrayData = useArrayData(props.dataKey, { ...props }); const arrayData = useArrayData(props.dataKey, { ...props });
const store = arrayData.store; const { store } = arrayData;
const searchText = ref(''); const searchText = ref('');
onMounted(() => { onMounted(() => {
@ -90,17 +90,17 @@ async function search() {
if (!props.redirect) return; if (!props.redirect) return;
if (props.customRouteRedirectName) if (props.customRouteRedirectName)
return push({ return router.push({
name: props.customRouteRedirectName, name: props.customRouteRedirectName,
params: { id: searchText.value }, params: { id: searchText.value },
}); });
const { matched: matches } = currentRoute.value; const { matched: matches } = router.currentRoute.value;
const { path } = matches.at(-1); const { path } = matches.at(-1);
const [, moduleName] = path.split('/'); const [, moduleName] = path.split('/');
if (!store.data.length || store.data.length > 1) if (!store.data.length || store.data.length > 1)
return push({ path: `/${moduleName}/list` }); return router.push({ path: `/${moduleName}/list` });
const targetId = store.data[0].id; const targetId = store.data[0].id;
let targetUrl; let targetUrl;
@ -108,7 +108,7 @@ async function search() {
if (path.endsWith('/list')) targetUrl = path.replace('/list', `/${targetId}/summary`); if (path.endsWith('/list')) targetUrl = path.replace('/list', `/${targetId}/summary`);
else if (path.includes(':id')) targetUrl = path.replace(':id', targetId); else if (path.includes(':id')) targetUrl = path.replace(':id', targetId);
await push({ path: targetUrl }); await router.push({ path: targetUrl });
} }
</script> </script>

View File

@ -1,19 +1,42 @@
/// <reference types="cypress" /> /// <reference types="cypress" />
describe('VnSearchBar', () => { describe('VnSearchBar', () => {
const employeeId = ' #1';
const salesPersonId = ' #18';
const idGap = '.q-item > .q-item__label';
const cardList = '.vn-card-list';
let url;
beforeEach(() => { beforeEach(() => {
cy.login('developer'); cy.login('developer');
cy.visit('/'); cy.visit('#/customer/list');
cy.url().then((currentUrl) => (url = currentUrl));
}); });
it('should redirect to new customer', () => { it('should redirect to customer summary page', () => {
cy.visit('#/customer/1112/basic-data') searchAndCheck('1', employeeId);
cy.openLeftMenu(); searchAndCheck('salesPerson', salesPersonId);
cy.get('.q-item > .q-item__label').should('have.text',' #1112') });
cy.closeLeftMenu();
it('should stay on the list page if there are several results or none', () => {
cy.writeSearchbar('salesA{enter}');
checkCardListAndUrl(2);
cy.clearSearchbar(); cy.clearSearchbar();
cy.writeSearchbar('1{enter}');
cy.openLeftMenu(); cy.writeSearchbar('0{enter}');
cy.get('.q-item > .q-item__label').should('have.text',' #1') checkCardListAndUrl(0);
cy.closeLeftMenu();
}); });
const searchAndCheck = (searchTerm, expectedText) => {
cy.clearSearchbar();
cy.writeSearchbar(`${searchTerm}{enter}`);
cy.get(idGap).should('have.text', expectedText);
};
const checkCardListAndUrl = (expectedLength) => {
cy.get(cardList).then(($cardList) => {
expect($cardList.find('.q-card').length).to.equal(expectedLength);
cy.url().then((currentUrl) => expect(currentUrl).to.equal(url));
});
};
}); });

View File

@ -1,11 +1,11 @@
import { vi, describe, expect, it, beforeAll, beforeEach, afterEach } from 'vitest'; import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest';
import { createWrapper, axios } from 'app/test/vitest/helper'; import { createWrapper } from 'app/test/vitest/helper';
import VnSearchbar from 'components/ui/VnSearchbar.vue'; import VnSearchbar from 'components/ui/VnSearchbar.vue';
// Probar a importar como plugin vue-router en archivo helper
describe('VnSearchBar', () => { describe('VnSearchBar', () => {
let vm; let vm;
let wrapper; let wrapper;
let pushSpy;
beforeAll(() => { beforeAll(() => {
wrapper = createWrapper(VnSearchbar, { wrapper = createWrapper(VnSearchbar, {
@ -16,7 +16,7 @@ describe('VnSearchBar', () => {
}, },
}); });
vm = wrapper.vm; vm = wrapper.vm;
vm.route.matched = [ vm.router.currentRoute.value.matched = [
{ {
path: '/', path: '/',
}, },
@ -30,24 +30,33 @@ describe('VnSearchBar', () => {
path: '/customer/:id/basic-data', path: '/customer/:id/basic-data',
}, },
]; ];
pushSpy = vi.spyOn(vm.router, 'push');
vi.spyOn(vm.arrayData, 'applyFilter');
}); });
afterEach(() => { beforeEach(() => (vm.store.data = [{ id: 1112, name: 'Trash' }]));
vi.clearAllMocks(); afterEach(() => vi.clearAllMocks());
});
it('should be defined', async () => { it('should be defined', async () => {
expect(vm.searchText).toBeDefined(); expect(vm.searchText).toBeDefined();
expect(vm.searchText).toEqual(''); expect(vm.searchText).toEqual('');
}); });
it('should redirect', async () => {
vi.spyOn(vm.router,'push'); it('should redirect to list page if there are several results', async () => {
vm.searchText = '1'; vm.store.data.push({ id: 1, name: 'employee' });
await vm.search(); await vm.search();
expect(vm.router.push).toHaveBeenCalledWith('/customer/1/basic-data'); expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/list' });
vm.searchText = '1112'; });
expect(vm.searchText).toEqual('1112');
vi.spyOn(vm.router,'push'); it('should redirect to list page if there is no results', async () => {
vm.store.data.pop();
await vm.search(); await vm.search();
expect(vm.router.push).toHaveBeenCalledWith('/customer/1112/basic-data'); expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/list' });
});
it('should redirect to basic-data page if there is only one result', async () => {
await vm.search();
expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/1112/basic-data' });
}); });
}); });