How to initialise Angular components with signal inputs from test?

Discussão em 'Angular' iniciado por Sergey Tihon, Novembro 15, 2024.

  1. Sergey Tihon

    Sergey Tihon Guest

    Let's say I have a simple component with signal input introduced in Angular 17.1

    @Component({
    selector: 'greet',
    template: `
    <span class="greet-text">{{firstName()}}</span>`,
    standalone: true,
    changeDetection: ChangeDetectionStrategy.OnPush,
    })
    export class GreetComponent {
    firstName = input.required<string>();
    }


    How can I instantiate this component from TestBed and assign signal inputs?

    The only solution I found in Angular repo, it to wrap it into another component without signal input.

    Is it the only way to test new inputs? Doubling number of components...

    describe('greet component', () => {
    it('should allow binding to an input', () => {
    const fixture = TestBed.createComponent(TestCmp);
    fixture.detectChanges();
    });
    });

    @Component({
    standalone: true,
    template: `<greet [firstName]="firstName" />`,
    imports: [GreetComponent],
    })
    class TestCmp {
    firstName = 'Initial';
    }

    Continue reading...

Compartilhe esta Página