import { ComponentFixture } from '@angular/core/testing'; import { MyComponent } from './my-component'; describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture; beforeEach(() => { TestBed.configureTestingModule({ declarations: [ MyComponent ] }) .compileComponents(); fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; }); it('should update the view when a property is updated', () => { // Set the component's property component.myProperty = 'new value'; // Trigger a change detection cycle fixture.detectChanges(); // Verify that the view has been updated expect(fixture.nativeElement.querySelector('.property-value').innerText).toEqual('new value'); }); });
import { ComponentFixture } from '@angular/core/testing'; import { MyComponent } from './my-component'; describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixtureIn this example, we create a fixture for the `MyComponent` component, set up a spy on one of the component's methods, then simulate a button click in the view. We then verify that the spy was called, indicating that the method was executed.; beforeEach(() => { // Use the TestBed to create a fixture for the MyComponent component TestBed.configureTestingModule({ declarations: [ MyComponent ] }); fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; }); it('should call a method when a button is clicked', () => { // Create a spy on the component's method we want to test const spy = spyOn(component, 'onButtonClick'); // Click the button in the view fixture.nativeElement.querySelector('button').click(); // Verify that the method was called expect(spy).toHaveBeenCalled(); }); });