diff --git a/src/cdk/listbox/listbox.spec.ts b/src/cdk/listbox/listbox.spec.ts index 82f41e22b1f7..c9ac18836a01 100644 --- a/src/cdk/listbox/listbox.spec.ts +++ b/src/cdk/listbox/listbox.spec.ts @@ -883,6 +883,18 @@ describe('CdkOption and CdkListbox', () => { fixture.detectChanges(); }).toThrowError('Listbox has selected values that do not match any of its options.'); }); + + it('should not throw on init with a preselected form control and a dynamic set of options', () => { + expect(() => { + setupComponent(ListboxWithPreselectedFormControl, [ReactiveFormsModule]); + }).not.toThrow(); + }); + + it('should throw on init if the preselected value is invalid', () => { + expect(() => { + setupComponent(ListboxWithInvalidPreselectedFormControl, [ReactiveFormsModule]); + }).toThrowError('Listbox has selected values that do not match any of its options.'); + }); }); }); @@ -955,6 +967,30 @@ class ListboxWithFormControl { isActiveDescendant = false; } +@Component({ + template: ` +
+
{{option}}
+
+ `, +}) +class ListboxWithPreselectedFormControl { + options = ['a', 'b', 'c']; + formControl = new FormControl('c'); +} + +@Component({ + template: ` +
+
{{option}}
+
+ `, +}) +class ListboxWithInvalidPreselectedFormControl { + options = ['a', 'b', 'c']; + formControl = new FormControl('d'); +} + @Component({ template: `