Skip to content

Commit

Permalink
fix(ld-select): always set ignore slot changes to false on next tick
Browse files Browse the repository at this point in the history
  • Loading branch information
borisdiakur committed Nov 2, 2021
1 parent 6ea5348 commit 06e4cd1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/liquid/components/ld-select/ld-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ export class LdSelect {

private initOptions() {
this.ignoreSlotChanges = true
setTimeout(() => {
this.ignoreSlotChanges = false
})

const initialized = this.initialized
let children
Expand Down Expand Up @@ -429,10 +432,7 @@ export class LdSelect {
}

private handleSlotChange(mutationsList: MutationRecord[]) {
if (this.ignoreSlotChanges) {
this.ignoreSlotChanges = false
return
}
if (this.ignoreSlotChanges) return

if (
mutationsList.some(
Expand Down
27 changes: 22 additions & 5 deletions src/liquid/components/ld-select/test/ld-select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,29 @@ describe('ld-select', () => {
})

it('ignores slot changes if options are being initialized', async () => {
expect.assertions(1)
const ldSelect = new LdSelect()
ldSelect.ignoreSlotChanges = true
;((ldSelect as unknown) as {
handleSlotChange: () => void
}).handleSlotChange()
expect(ldSelect.ignoreSlotChanges).toBeFalsy()

try {
ldSelect.ignoreSlotChanges = true
;((ldSelect as unknown) as {
handleSlotChange: () => void
}).handleSlotChange()
} catch (err) {
// the following assertion should be skipped.
expect(err).toStrictEqual(Error('should be skipped.'))
}

try {
ldSelect.ignoreSlotChanges = false
;((ldSelect as unknown) as {
handleSlotChange: () => void
}).handleSlotChange()
} catch (err) {
expect(err).toStrictEqual(
TypeError("Cannot read property 'some' of undefined")
)
}
})

it('deselects a selected option if another option is selected in single select mode', async () => {
Expand Down

0 comments on commit 06e4cd1

Please sign in to comment.