From 06e4cd15289b913878889b54ac19bcc9caf36f3e Mon Sep 17 00:00:00 2001 From: Boris Diakur Date: Tue, 2 Nov 2021 12:46:07 +0100 Subject: [PATCH] fix(ld-select): always set ignore slot changes to false on next tick --- src/liquid/components/ld-select/ld-select.tsx | 8 +++--- .../ld-select/test/ld-select.spec.ts | 27 +++++++++++++++---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/liquid/components/ld-select/ld-select.tsx b/src/liquid/components/ld-select/ld-select.tsx index ff61882929..f8214495bd 100644 --- a/src/liquid/components/ld-select/ld-select.tsx +++ b/src/liquid/components/ld-select/ld-select.tsx @@ -336,6 +336,9 @@ export class LdSelect { private initOptions() { this.ignoreSlotChanges = true + setTimeout(() => { + this.ignoreSlotChanges = false + }) const initialized = this.initialized let children @@ -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( diff --git a/src/liquid/components/ld-select/test/ld-select.spec.ts b/src/liquid/components/ld-select/test/ld-select.spec.ts index 17ef0b4229..b32044f5e9 100644 --- a/src/liquid/components/ld-select/test/ld-select.spec.ts +++ b/src/liquid/components/ld-select/test/ld-select.spec.ts @@ -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 () => {