DEV Community

Fran Saoco
Fran Saoco

Posted on • Edited on

Testing Angular Components with Children using Jest

parent-component.ts

@Component({
  selector: 'app-parent',
  template: `
    <div>
      <h1>Parent Component</h1>
      <app-child [value]="parentValue"></app-child>
    </div>
  `,
})
export class ParentComponent {
  parentValue = 'Parent Value';
}
Enter fullscreen mode Exit fullscreen mode

parent-component.spec.ts

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ParentComponent } from './parent.component';
import { Component, Input } from '@angular/core';
import { By } from '@angular/platform-browser';

// Child component mock
@Component({
  selector: 'app-child',
  template: '<div>{{ value }}</div>'
})
class MockChildComponent {
  @Input() value: string = '';
}

describe('ParentComponent', () => {
  let parentComponent: ParentComponent;
  let fixture: ComponentFixture<ParentComponent>;
  let childComponent: MockChildComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ParentComponent, MockChildComponent] // Use mock instead of real child
    });
    fixture = TestBed.createComponent(ParentComponent);
    parentComponent = fixture.componentInstance;
    childComponent = fixture.debugElement.query(By.directive(MockChildComponent)).componentInstance as MockChildComponent;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(parentComponent).toBeTruthy();
  });

  it('should pass the correct value to child component', () => {
    expect(childComponent.value).toBe('Parent Value');
  });

  it('should update child component when parent value changes', () => {
    parentComponent.parentValue = 'New Value';
    fixture.detectChanges();

    expect(childComponent.value).toEqual('New Value');
  });

});
Enter fullscreen mode Exit fullscreen mode

Tip: The mock child component (MockChildComponent) ensures you're only testing the parent component's behavior.

Top comments (1)

Collapse
 
assetkad profile image
assetkad

`import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ParentComponent } from './parent.component';
import { By } from '@angular/platform-browser';
import { Component, Input } from '@angular/core';

@Component({
selector: 'app-child',
standalone: true,
template: '

{{ value }}',
})
class MockChildComponent {
@Input() value: string = '';
}

describe('ParentComponent', () => {
let parentComponent: ParentComponent;
let fixture: ComponentFixture;
let childComponent: MockChildComponent;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ParentComponent],
})
.overrideComponent(ParentComponent, {
set: {
imports: [MockChildComponent],
},
})
.compileComponents();

fixture = TestBed.createComponent(ParentComponent);
parentComponent = fixture.componentInstance;
fixture.detectChanges();

childComponent = fixture.debugElement.query(
  By.directive(MockChildComponent)
).componentInstance as MockChildComponent;
fixture.detectChanges();
Enter fullscreen mode Exit fullscreen mode

});

it('should create', () => {
expect(parentComponent).toBeTruthy();
});

it('should pass the correct value to child component', () => {
expect(childComponent.value).toBe('Parent Value');
});

it('should update child component when parent value changes', () => {
parentComponent.parentValue = 'New Value';
fixture.detectChanges();

expect(childComponent.value).toEqual('New Value');
Enter fullscreen mode Exit fullscreen mode

});
});
`