From 5cb9ee45c8d1448407bc8646deb14bfc048dbaf7 Mon Sep 17 00:00:00 2001 From: Adam Berecz Date: Wed, 28 Sep 2022 14:32:02 +0200 Subject: [PATCH] fix: async options resolve fix #266 --- src/composables/useOptions.js | 2 +- tests/unit/composables/useOptions.spec.js | 33 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/composables/useOptions.js b/src/composables/useOptions.js index 9cd6375..e8eac13 100644 --- a/src/composables/useOptions.js +++ b/src/composables/useOptions.js @@ -723,7 +723,7 @@ export default function useOptions (props, context, dep) watch(options, (n, o) => { if (typeof props.options === 'function') { - if (resolveOnLoad.value) { + if (resolveOnLoad.value && (!o || (n && n.toString() !== o.toString()))) { resolveOptions() } } else { diff --git a/tests/unit/composables/useOptions.spec.js b/tests/unit/composables/useOptions.spec.js index 3c4b3d5..688483d 100644 --- a/tests/unit/composables/useOptions.spec.js +++ b/tests/unit/composables/useOptions.spec.js @@ -3055,6 +3055,39 @@ describe('useOptions', () => { expect(asyncOptionsMock2).toHaveBeenCalledTimes(1) }) + it('should not resolve options when async options does not change & resolveOnLoad=true', async () => { + let asyncOptionsMock = jest.fn() + + const fn1 = async () => { + return await new Promise((resolve, reject) => { + asyncOptionsMock() + resolve([1,2,3]) + }) + } + + const fn2 = async () => { + return await new Promise((resolve, reject) => { + asyncOptionsMock() + resolve([1,2,3]) + }) + } + + let select = createSelect({ + resolveOnLoad: true, + options: fn1, + }) + + await flushPromises() + + expect(asyncOptionsMock).toHaveBeenCalledTimes(1) + + select.vm.$parent.props.options = fn2 + + await flushPromises() + + expect(asyncOptionsMock).toHaveBeenCalledTimes(1) + }) + it('should not resolve options when async options change & resolveOnLoad=false', async () => { let asyncOptionsMock = jest.fn() let asyncOptionsMock2 = jest.fn()