Skip to content

Commit 5cb9ee4

Browse files
committed
fix: async options resolve fix #266
1 parent 0d42746 commit 5cb9ee4

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/composables/useOptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ export default function useOptions (props, context, dep)
723723

724724
watch(options, (n, o) => {
725725
if (typeof props.options === 'function') {
726-
if (resolveOnLoad.value) {
726+
if (resolveOnLoad.value && (!o || (n && n.toString() !== o.toString()))) {
727727
resolveOptions()
728728
}
729729
} else {

tests/unit/composables/useOptions.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,6 +3055,39 @@ describe('useOptions', () => {
30553055
expect(asyncOptionsMock2).toHaveBeenCalledTimes(1)
30563056
})
30573057

3058+
it('should not resolve options when async options does not change & resolveOnLoad=true', async () => {
3059+
let asyncOptionsMock = jest.fn()
3060+
3061+
const fn1 = async () => {
3062+
return await new Promise((resolve, reject) => {
3063+
asyncOptionsMock()
3064+
resolve([1,2,3])
3065+
})
3066+
}
3067+
3068+
const fn2 = async () => {
3069+
return await new Promise((resolve, reject) => {
3070+
asyncOptionsMock()
3071+
resolve([1,2,3])
3072+
})
3073+
}
3074+
3075+
let select = createSelect({
3076+
resolveOnLoad: true,
3077+
options: fn1,
3078+
})
3079+
3080+
await flushPromises()
3081+
3082+
expect(asyncOptionsMock).toHaveBeenCalledTimes(1)
3083+
3084+
select.vm.$parent.props.options = fn2
3085+
3086+
await flushPromises()
3087+
3088+
expect(asyncOptionsMock).toHaveBeenCalledTimes(1)
3089+
})
3090+
30583091
it('should not resolve options when async options change & resolveOnLoad=false', async () => {
30593092
let asyncOptionsMock = jest.fn()
30603093
let asyncOptionsMock2 = jest.fn()

0 commit comments

Comments
 (0)