Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a test fixture for the MyComponent.
Angular
let fixture = TestBed.[1](MyComponent); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using compileComponents instead of createComponent.
Trying to inject the component directly.
✗ Incorrect
The
createComponent method creates a fixture for the component to test.2fill in blank
mediumComplete the code to access the native DOM element from the fixture.
Angular
const element = fixture.debugElement.[1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using componentInstance instead of nativeElement.
Trying to access children when you want the root element.
✗ Incorrect
The
nativeElement property gives direct access to the DOM element.3fill in blank
hardFix the error in the test by completing the code to detect changes after updating component properties.
Angular
fixture.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using autoDetectChanges which is a configuration, not a method.
Trying to call a non-existent method like refresh.
✗ Incorrect
Calling
detectChanges() tells Angular to update the view with the latest data.4fill in blank
hardFill both blanks to query a button element by CSS selector and trigger a click event.
Angular
const button = fixture.debugElement.[1](By.css('button')); button.[2]('click');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using queryAll when only one element is needed.
Trying to call click() directly on debugElement.
✗ Incorrect
Use
query to find the element and triggerEventHandler to simulate the click.5fill in blank
hardFill all three blanks to create a test that checks if a component's title is rendered correctly.
Angular
fixture.detectChanges(); const compiled = fixture.debugElement.[1](By.css('h1')).nativeElement; expect(compiled.[2]).toContain(component.[3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using queryAll instead of query when only one element is needed.
Checking innerHTML instead of textContent.
Comparing to a wrong component property.
✗ Incorrect
We query the
h1 element, check its textContent, and compare it to the component's title property.