From 8cb72d9716d0139ee1fa76e84e66a569e1f461c9 Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Fri, 20 Aug 2021 10:24:49 -0600 Subject: [PATCH] [fix] set selectedIndex to -1 when no option matches bound + + + + + +

selected: null

+ `, + + async test({ assert, component, target }) { + const select = target.querySelector('select'); + const options = [...target.querySelectorAll('option')]; + + assert.equal(component.selected, null); + + // no option should be selected since none of the options matches the bound value + assert.equal(select.value, ''); + assert.equal(select.selectedIndex, -1); + assert.ok(!options[0].selected); + + component.selected = 'a'; // first option should now be selected + assert.equal(select.value, 'a'); + assert.ok(options[0].selected); + + assert.htmlEqual(target.innerHTML, ` +

selected: a

+ + + +

selected: a

+ `); + + component.selected = 'd'; // doesn't match an option + + // now no option should be selected again + assert.equal(select.value, ''); + assert.equal(select.selectedIndex, -1); + assert.ok(!options[0].selected); + + assert.htmlEqual(target.innerHTML, ` +

selected: d

+ + + +

selected: d

+ `); + } +}; diff --git a/test/runtime/samples/binding-select-unmatched/main.svelte b/test/runtime/samples/binding-select-unmatched/main.svelte new file mode 100644 index 0000000000..20e13f4765 --- /dev/null +++ b/test/runtime/samples/binding-select-unmatched/main.svelte @@ -0,0 +1,14 @@ + + +

selected: {selected}

+ + + +

selected: {selected}

\ No newline at end of file