Bird
0
0

You want to test a form that disables the submit button until the form is valid. How do you write a test to verify the button is disabled initially and enabled after valid input?

hard📝 component behavior Q8 of 15
Angular - Testing
You want to test a form that disables the submit button until the form is valid. How do you write a test to verify the button is disabled initially and enabled after valid input?
ASimulate a click on the button and expect the form to be valid.
BOnly check the form's valid property without inspecting the button state.
CCall updateValueAndValidity() on the button element.
DCheck button disabled property before and after setting valid form values and calling fixture.detectChanges().
Step-by-Step Solution
Solution:
  1. Step 1: Verify initial button state

    Initially, the form is invalid, so the button should have disabled=true.
  2. Step 2: Set valid form values and update view

    Set valid values on form controls, call updateValueAndValidity(), then fixture.detectChanges() to update the button state.
  3. Step 3: Check button disabled property again

    After valid input, the button's disabled property should be false.
  4. Final Answer:

    Check button disabled property before and after setting valid form values and calling fixture.detectChanges(). -> Option D
  5. Quick Check:

    Test button disabled state by checking property before/after input [OK]
Quick Trick: Check button disabled before and after valid input with detectChanges [OK]
Common Mistakes:
  • Ignoring button disabled property in tests
  • Calling updateValueAndValidity on button element
  • Only checking form validity without UI state

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes