How to write unit test case for angular code

Discussão em 'Angular' iniciado por Goutm, Dezembro 10, 2024.

  1. Goutm

    Goutm Guest

    I am trying to to write test case for my angular project. I am using angular version 16. Below is my code. In my code I am not getting component.appRuleList and component.appRuleOptions values in my test case. Getting like undefined both. How to get these values inside spec.ts file

    app.component.ts:

    public appRuleOptions:string[] = [];
    public appRuleList:any[] = [];
    public selectedName = 'test';

    public ngOnInit(): void {
    this._store$
    .pipe(takeUntil(this.destroy), select(applicationRows))
    .subscribe((userIdentificationPart: IApplicationRule) => {
    if (userIdentificationPart && userIdentificationPart.applicationRule) {
    this.appRuleList = userIdentificationPart.applicationRule || [];
    this.appRuleOptions = userIdentificationPart.applicationRule.map(
    (x: IApplicationMainRules) => this.selectedName + '/' + x.name,
    );

    }
    });
    }



    app.component.spec.ts:

    it('should subscribe to the store and update appRuleList and appRuleOptions on ngOnInit', () => {
    const applicationRule = [

    {
    "name": "ui",
    "place": "usa"
    }
    ];

    component.selectedNamespace = 'test';
    (component['_store$'].pipe as jasmine.Spy).and.returnValue(of(applicationRule));

    component.ngOnInit();

    fixture.detectChanges();


    expect(component.appRuleList).toEqual(applicationRule); //Getting error
    expect(component.appRuleOptions).toEqual([
    "power/testing"
    ]);


    });


    I am expecting component.appRuleList and component.appRuleOptions values in the app.component.spec.ts file.

    Continue reading...

Compartilhe esta Página