diff --git a/src/components/common/VnSelect.vue b/src/components/common/VnSelect.vue index ef3e9238c..3b93fcca4 100644 --- a/src/components/common/VnSelect.vue +++ b/src/components/common/VnSelect.vue @@ -71,7 +71,7 @@ const myOptionsOriginal = ref([]); const vnSelectRef = ref(); const dataRef = ref(); -const value = computed({ +const selectValue = computed({ get() { return $props.modelValue; }, @@ -212,7 +212,7 @@ async function onScroll(scrollEv) { { + let vm; + const options = [ + { id: 1, name: 'Option 1' }, + { id: 2, name: 'Option 2' }, + { id: 3, name: 'Option 3' }, + ]; + beforeAll(() => { + vm = createWrapper(VnSelect, { + propsData: { + options, + optionLabel: 'name', + optionValue: 'id', + }, + global: { + stubs: ['FetchData'], + mocks: {}, + }, + }).vm; + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should mounted correctly', () => { + expect(vm).toBeDefined(); + }); + it('should pass options and not CALL URL', async () => { + expect(vm.myOptions.length).toEqual(options.length); + expect(vm.useURL).toBeFalsy(); + vm.selectValue = 'Option 2'; + const optionsFiltered = vm.filter('Option 2', options); + // vm.filterHandler('Option 2', vi.fn()); + vm.filterHandler('Option 2', () => { + vm.myOptions = optionsFiltered; + }); + expect(vm.myOptions.length).toEqual(1); + }); +}); + +describe('VnSelect FETCH URL', () => { + let vm; + + beforeAll(async () => { + vi.spyOn(axios, 'get').mockResolvedValue({ + data: [ + { id: 1, name: 'Tony Stark' }, + { id: 2, name: 'Jessica Jones' }, + { id: 3, name: 'Bruce Wayne' }, + ], + }); + vm = createWrapper(VnSelect, { + propsData: { + optionLabel: 'name', + optionValue: 'id', + url: 'Suppliers', + }, + global: { + stubs: ['FetchData'], + mocks: { fetch: vi.fn() }, + }, + }).vm; + }); + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should CALL URL', async () => { + vm.selectValue = ''; + expect(vm.options.length).toEqual(0); + expect(vm.useURL).toBeTruthy(); + expect(vm.myOptions.length).toEqual(3); + + const canceller = new AbortController(); + expect(axios.get).toHaveBeenCalledWith('Suppliers', { + params: { + filter: '{"order":"","limit":"30","skip":0}', + }, + signal: canceller.signal, + }); + }); +});