diff --git a/package.json b/package.json index c4f5b828a..1d729b82f 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,7 @@ "validator": "^13.9.0", "vue": "^3.3.4", "vue-i18n": "^9.2.2", - "vue-router": "^4.2.1", - "vue-router-mock": "^0.2.0" + "vue-router": "^4.2.1" }, "devDependencies": { "@intlify/unplugin-vue-i18n": "^0.8.1", diff --git a/src/components/ui/VnSearchbar.vue b/src/components/ui/VnSearchbar.vue index 8220aa3db..eca957092 100644 --- a/src/components/ui/VnSearchbar.vue +++ b/src/components/ui/VnSearchbar.vue @@ -87,15 +87,10 @@ async function search() { }); if (!props.redirect) return; - const rows = store.data; - const module = route.matched[1]; - if (rows.length === 1) { - const [firstRow] = rows; - await router.push({ path: `${module.path}/${firstRow.id}` }); - } else if (route.matched.length > 3) { - await router.push({ path: `/${module.path}` }); - arrayData.updateStateParams(); - } + const { matched: matches } = route; + const { path } = matches[matches.length-1]; + const newRoute = path.replace(':id', searchText.value); + await router.push(newRoute); } diff --git a/test/cypress/integration/vnSearchBar.spec.js b/test/cypress/integration/vnSearchBar.spec.js new file mode 100644 index 000000000..d6dea0780 --- /dev/null +++ b/test/cypress/integration/vnSearchBar.spec.js @@ -0,0 +1,19 @@ +/// +describe('VnSearchBar', () => { + beforeEach(() => { + cy.login('developer'); + cy.visit('/'); + }); + + it('should redirect to new customer', () => { + cy.visit('#/customer/1112/basic-data') + cy.openLeftMenu(); + cy.get('.q-item > .q-item__label').should('have.text',' #1112') + cy.closeLeftMenu(); + cy.clearSearchbar(); + cy.writeSearchbar('1{enter}'); + cy.openLeftMenu(); + cy.get('.q-item > .q-item__label').should('have.text',' #1') + cy.closeLeftMenu(); + }); +}); diff --git a/test/cypress/support/commands.js b/test/cypress/support/commands.js index fc3a593ad..7dc6ffab7 100755 --- a/test/cypress/support/commands.js +++ b/test/cypress/support/commands.js @@ -163,6 +163,23 @@ Cypress.Commands.add('openRightMenu', (element) => { cy.get('#actions-append').click(); }); +Cypress.Commands.add('openLeftMenu', (element) => { + if (element) cy.waitForElement(element); + cy.get('.q-toolbar > .q-btn--round.q-btn--dense > .q-btn__content > .q-icon').click(); +}); +Cypress.Commands.add('closeLeftMenu', (element) => { + if (element) cy.waitForElement(element); + cy.get('.fullscreen').click(); +}); + +Cypress.Commands.add('clearSearchbar', (element) => { + if (element) cy.waitForElement(element); + cy.get('#searchbar > form > label > div:nth-child(1) input').clear(); +}); + +Cypress.Commands.add('writeSearchbar', (value) => { + cy.get('#searchbar > form > label > div:nth-child(1) input').type(value); +}); Cypress.Commands.add('validateContent', (selector, expectedValue) => { cy.get(selector).should('have.text', expectedValue); }); diff --git a/test/vitest/__tests__/components/common/VnSearchBar.spec.js b/test/vitest/__tests__/components/common/VnSearchBar.spec.js new file mode 100644 index 000000000..fd0a026ef --- /dev/null +++ b/test/vitest/__tests__/components/common/VnSearchBar.spec.js @@ -0,0 +1,53 @@ +import { vi, describe, expect, it, beforeAll, beforeEach, afterEach } from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; +import VnSearchbar from 'components/ui/VnSearchbar.vue'; + + +describe('VnSearchBar', () => { + let vm; + let wrapper; + + beforeAll(() => { + wrapper = createWrapper(VnSearchbar, { + propsData: { + dataKey: 'CustomerList', + label: 'Search customer', + info: 'Info customer', + }, + }); + vm = wrapper.vm; + vm.route.matched = [ + { + path: '/', + }, + { + path: '/customer', + }, + { + path: '/customer/:id', + }, + { + path: '/customer/:id/basic-data', + }, + ]; + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + it('should be defined', async () => { + expect(vm.searchText).toBeDefined(); + expect(vm.searchText).toEqual(''); + }); + it('should redirect', async () => { + vi.spyOn(vm.router,'push'); + vm.searchText = '1'; + await vm.search(); + expect(vm.router.push).toHaveBeenCalledWith('/customer/1/basic-data'); + vm.searchText = '1112'; + expect(vm.searchText).toEqual('1112'); + vi.spyOn(vm.router,'push'); + await vm.search(); + expect(vm.router.push).toHaveBeenCalledWith('/customer/1112/basic-data'); + }); +});