Skip to content

Commit

Permalink
fix: async options resolve fix #266
Browse files Browse the repository at this point in the history
  • Loading branch information
adamberecz committed Sep 28, 2022
1 parent 0d42746 commit 5cb9ee4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/composables/useOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/composables/useOptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5cb9ee4

Please sign in to comment.