Bird
0
0

Given this test code snippet, what will be the value of component.count after the click event?

medium📝 component behavior Q13 of 15
Angular - Testing
Given this test code snippet, what will be the value of component.count after the click event?
const fixture = TestBed.createComponent(CounterComponent);
const component = fixture.componentInstance;
fixture.detectChanges();
const button = fixture.debugElement.query(By.css('button'));
button.triggerEventHandler('click', null);
fixture.detectChanges();

Assuming the button click increments count by 1 starting from 0.
ANaN
B0
C1
Dundefined
Step-by-Step Solution
Solution:
  1. Step 1: Trace code execution step-by-step

    Initial count=0. First detectChanges() renders template. triggerEventHandler('click', null) increments to 1. Second detectChanges() updates view.
  2. Final Answer:

    1 -> Option C
  3. Quick Check:

    Click increments count from 0 to 1 [OK]
Quick Trick: triggerEventHandler + detectChanges updates component state [OK]
Common Mistakes:
  • Forgetting to call detectChanges() after event
  • Assuming count stays 0 without event
  • Confusing nativeElement click with triggerEventHandler

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes