diff --git a/src/components/ui/__tests__/VnSearchbar.spec.js b/src/components/ui/__tests__/VnSearchbar.spec.js new file mode 100644 index 000000000..25649194d --- /dev/null +++ b/src/components/ui/__tests__/VnSearchbar.spec.js @@ -0,0 +1,71 @@ +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, { + propsData: { + dataKey: 'testKey', + filter: null, + whereFilter: null, + searchRemoveParams: true, + }, + }); + wrapper = wrapper.wrapper; + vm = wrapper.vm; + + 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 resetPaginationSpy = vi.spyOn(vm.arrayData, 'resetPagination').mockImplementation(() => {}); + await vm.search(); + + expect(resetPaginationSpy).toHaveBeenCalled(); + expect(applyFilterSpy).toHaveBeenCalledWith({ + params: { search: searchText }, + }); + }); + + it('search includes static params if searchRemoveParams is false', async () => { + wrapper.setProps({ searchRemoveParams: false }); + await vm.$nextTick(); + await vm.search(); + + expect(applyFilterSpy).toHaveBeenCalledWith({ + params: { staticKey: 'staticValue', search: searchText }, + filter: {skip: 0}, + }); + }); + + it('updates store when dataKey changes', async () => { + expect(vm.store.userParams).toEqual(userParams); + wrapper.setProps({ dataKey: 'newTestKey' }); + await vm.$nextTick(); + expect(vm.store.userParams).toEqual({}); + }); + + it('computes the "to" property correctly for redirection', () => { + vm.arrayData.store.searchUrl = 'searchParam'; + vm.arrayData.store.currentFilter = { category: 'plants' }; + const expectedQuery = JSON.stringify({ + ...vm.arrayData.store.currentFilter, + search: searchText, + }); + expect(vm.to.query.searchParam).toBe(expectedQuery); + }); +}); \ No newline at end of file