From e8cee99b787072ef76c1f382eac4c088b09a5024 Mon Sep 17 00:00:00 2001 From: provira Date: Tue, 21 Jan 2025 09:45:47 +0100 Subject: [PATCH 1/4] feat: refs #7103 created test for VnSearchbar --- .../ui/__tests__/VnSearchbar.spec.js | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/components/ui/__tests__/VnSearchbar.spec.js diff --git a/src/components/ui/__tests__/VnSearchbar.spec.js b/src/components/ui/__tests__/VnSearchbar.spec.js new file mode 100644 index 000000000..66d6b1ad9 --- /dev/null +++ b/src/components/ui/__tests__/VnSearchbar.spec.js @@ -0,0 +1,67 @@ +import { vi, describe, expect, it, beforeEach } from 'vitest'; +import { createWrapper } from 'app/test/vitest/helper'; +import VnSearchbar from 'src/components/ui/VnSearchbar.vue'; + +describe('VnSearchbar - Functions', () => { + let vm; + let wrapper; + + beforeEach(async () => { + wrapper = createWrapper(VnSearchbar, { + propsData: { + dataKey: 'testKey', + filter: null, + whereFilter: null, + searchRemoveParams: true, + }, + }); + wrapper = wrapper.wrapper; + vm = wrapper.vm; + }); + + it('search resets pagination and applies filter', async () => { + const resetPaginationSpy = vi.spyOn(vm.arrayData, 'resetPagination').mockImplementation(() => {}); + const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => Promise.resolve()); + + vm.searchText = 'Bolas de madera'; + await vm.search(); + + expect(resetPaginationSpy).toHaveBeenCalled(); + expect(applyFilterSpy).toHaveBeenCalledWith({ + params: { search: 'Bolas de madera' }, + filter: null, + }); + }); + + it('search includes static params if searchRemoveParams is false', async () => { + wrapper.setProps({ searchRemoveParams: false }); + await vm.$nextTick(); + const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => Promise.resolve()); + vm.arrayData.store.userParams = { staticKey: 'staticValue' }; + vm.searchText = 'Bolas de madera'; + await vm.search(); + + expect(applyFilterSpy).toHaveBeenCalledWith({ + params: { staticKey: 'staticValue', search: 'Bolas de madera' }, + filter: null, + }); + }); + + it('updates store when dataKey changes', async () => { + expect(vm.store.userParams).toEqual({ staticKey: 'staticValue' }); + wrapper.setProps({ dataKey: 'newTestKey' }); + await vm.$nextTick(); + expect(vm.store.userParams).toEqual({}); + }); + + it('computes the "to" property correctly for redirection', () => { + vm.searchText = 'testRedirect'; + vm.arrayData.store.searchUrl = 'searchParam'; + vm.arrayData.store.currentFilter = { category: 'plants' }; + const expectedQuery = JSON.stringify({ + ...vm.arrayData.store.currentFilter, + search: 'testRedirect', + }); + expect(vm.to.query.searchParam).toBe(expectedQuery); + }); +}); \ No newline at end of file -- 2.40.1 From 5e6a83c001a860e8d05410c04035eaa5638c6ab7 Mon Sep 17 00:00:00 2001 From: provira Date: Tue, 21 Jan 2025 14:41:28 +0100 Subject: [PATCH 2/4] fix: refs #7103 removed unused code on spies --- src/components/ui/__tests__/VnSearchbar.spec.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/components/ui/__tests__/VnSearchbar.spec.js b/src/components/ui/__tests__/VnSearchbar.spec.js index 66d6b1ad9..834fa2faf 100644 --- a/src/components/ui/__tests__/VnSearchbar.spec.js +++ b/src/components/ui/__tests__/VnSearchbar.spec.js @@ -2,9 +2,10 @@ import { vi, describe, expect, it, beforeEach } from 'vitest'; import { createWrapper } from 'app/test/vitest/helper'; import VnSearchbar from 'src/components/ui/VnSearchbar.vue'; -describe('VnSearchbar - Functions', () => { +describe('VnSearchbar', () => { let vm; let wrapper; + let applyFilterSpy; beforeEach(async () => { wrapper = createWrapper(VnSearchbar, { @@ -17,13 +18,14 @@ describe('VnSearchbar - Functions', () => { }); wrapper = wrapper.wrapper; vm = wrapper.vm; + + vm.searchText = 'Bolas de madera'; + vm.arrayData.store.userParams = { staticKey: 'staticValue' }; + applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); }); it('search resets pagination and applies filter', async () => { const resetPaginationSpy = vi.spyOn(vm.arrayData, 'resetPagination').mockImplementation(() => {}); - const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => Promise.resolve()); - - vm.searchText = 'Bolas de madera'; await vm.search(); expect(resetPaginationSpy).toHaveBeenCalled(); @@ -36,9 +38,6 @@ describe('VnSearchbar - Functions', () => { it('search includes static params if searchRemoveParams is false', async () => { wrapper.setProps({ searchRemoveParams: false }); await vm.$nextTick(); - const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => Promise.resolve()); - vm.arrayData.store.userParams = { staticKey: 'staticValue' }; - vm.searchText = 'Bolas de madera'; await vm.search(); expect(applyFilterSpy).toHaveBeenCalledWith({ @@ -55,12 +54,11 @@ describe('VnSearchbar - Functions', () => { }); it('computes the "to" property correctly for redirection', () => { - vm.searchText = 'testRedirect'; vm.arrayData.store.searchUrl = 'searchParam'; vm.arrayData.store.currentFilter = { category: 'plants' }; const expectedQuery = JSON.stringify({ ...vm.arrayData.store.currentFilter, - search: 'testRedirect', + search: 'Bolas de madera', }); expect(vm.to.query.searchParam).toBe(expectedQuery); }); -- 2.40.1 From 2aa7177449e289326cbfefa3277e41d80157b81e Mon Sep 17 00:00:00 2001 From: provira Date: Wed, 22 Jan 2025 07:30:28 +0100 Subject: [PATCH 3/4] fix: refs #7103 updated tests for new changes --- src/components/ui/__tests__/VnSearchbar.spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/ui/__tests__/VnSearchbar.spec.js b/src/components/ui/__tests__/VnSearchbar.spec.js index 834fa2faf..6ba8a6aeb 100644 --- a/src/components/ui/__tests__/VnSearchbar.spec.js +++ b/src/components/ui/__tests__/VnSearchbar.spec.js @@ -5,7 +5,6 @@ import VnSearchbar from 'src/components/ui/VnSearchbar.vue'; describe('VnSearchbar', () => { let vm; let wrapper; - let applyFilterSpy; beforeEach(async () => { wrapper = createWrapper(VnSearchbar, { @@ -21,28 +20,29 @@ describe('VnSearchbar', () => { vm.searchText = 'Bolas de madera'; vm.arrayData.store.userParams = { staticKey: 'staticValue' }; - applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); + }); it('search resets pagination and applies filter', async () => { + const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); const resetPaginationSpy = vi.spyOn(vm.arrayData, 'resetPagination').mockImplementation(() => {}); await vm.search(); expect(resetPaginationSpy).toHaveBeenCalled(); expect(applyFilterSpy).toHaveBeenCalledWith({ params: { search: 'Bolas de madera' }, - filter: null, }); }); it('search includes static params if searchRemoveParams is false', async () => { + const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); wrapper.setProps({ searchRemoveParams: false }); await vm.$nextTick(); await vm.search(); expect(applyFilterSpy).toHaveBeenCalledWith({ params: { staticKey: 'staticValue', search: 'Bolas de madera' }, - filter: null, + filter: {skip: 0}, }); }); -- 2.40.1 From ea4a46d9c7000590109ef817841765fdcf34dc79 Mon Sep 17 00:00:00 2001 From: provira Date: Wed, 22 Jan 2025 11:40:25 +0100 Subject: [PATCH 4/4] fix: refs #7103 used consts for repeated variables --- .../ui/__tests__/VnSearchbar.spec.js | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/components/ui/__tests__/VnSearchbar.spec.js b/src/components/ui/__tests__/VnSearchbar.spec.js index 6ba8a6aeb..25649194d 100644 --- a/src/components/ui/__tests__/VnSearchbar.spec.js +++ b/src/components/ui/__tests__/VnSearchbar.spec.js @@ -1,10 +1,13 @@ -import { vi, describe, expect, it, beforeEach } from 'vitest'; +import { vi, describe, expect, it, beforeEach, afterEach } from 'vitest'; import { createWrapper } from 'app/test/vitest/helper'; import VnSearchbar from 'src/components/ui/VnSearchbar.vue'; describe('VnSearchbar', () => { let vm; let wrapper; + let applyFilterSpy; + const searchText = 'Bolas de madera'; + const userParams = {staticKey: 'staticValue'}; beforeEach(async () => { wrapper = createWrapper(VnSearchbar, { @@ -18,36 +21,39 @@ describe('VnSearchbar', () => { wrapper = wrapper.wrapper; vm = wrapper.vm; - vm.searchText = 'Bolas de madera'; - vm.arrayData.store.userParams = { staticKey: 'staticValue' }; + vm.searchText = searchText; + vm.arrayData.store.userParams = userParams; + applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); }); + afterEach(() => { + vi.clearAllMocks(); + }); + it('search resets pagination and applies filter', async () => { - const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); const resetPaginationSpy = vi.spyOn(vm.arrayData, 'resetPagination').mockImplementation(() => {}); await vm.search(); expect(resetPaginationSpy).toHaveBeenCalled(); expect(applyFilterSpy).toHaveBeenCalledWith({ - params: { search: 'Bolas de madera' }, + params: { search: searchText }, }); }); it('search includes static params if searchRemoveParams is false', async () => { - const applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); wrapper.setProps({ searchRemoveParams: false }); await vm.$nextTick(); await vm.search(); expect(applyFilterSpy).toHaveBeenCalledWith({ - params: { staticKey: 'staticValue', search: 'Bolas de madera' }, + params: { staticKey: 'staticValue', search: searchText }, filter: {skip: 0}, }); }); it('updates store when dataKey changes', async () => { - expect(vm.store.userParams).toEqual({ staticKey: 'staticValue' }); + expect(vm.store.userParams).toEqual(userParams); wrapper.setProps({ dataKey: 'newTestKey' }); await vm.$nextTick(); expect(vm.store.userParams).toEqual({}); @@ -58,7 +64,7 @@ describe('VnSearchbar', () => { vm.arrayData.store.currentFilter = { category: 'plants' }; const expectedQuery = JSON.stringify({ ...vm.arrayData.store.currentFilter, - search: 'Bolas de madera', + search: searchText, }); expect(vm.to.query.searchParam).toBe(expectedQuery); }); -- 2.40.1