1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Jest Unit test Using Angular (No Jasmine)

Discussão em 'Angular' iniciado por Shaleen Agarwal, Setembro 28, 2024 às 12:43.

  1. `Hi All, I am stuck in writing a unit test for the below function in Angular.

    Here is my unit test:
    describe('ProductAccountSetupComponent', () => {
    let urlsService: GBUrlsService;
    let componentRefInstanceSpy: any;
    let component: ProductAccountSetupComponent;
    let fixture: ComponentFixture<ProductAccountSetupComponent>;

    beforeEach(async () => {
    await TestBed.configureTestingModule({
    declarations: [ProductAccountSetupComponent],
    schemas: [CUSTOM_ELEMENTS_SCHEMA],
    providers: [
    { provide: GBDataStore, useValue: dataStore },
    { provide: GBRouterService, useValue: routerService },
    {
    provide: GBAccountService,
    useValue: new MockAccountService()
    },
    { provide: GBRoutes, useValue: {} },
    { provide: GBTridionService, useValue: {} },
    { provide: GBUrlsService, useValue: mockUrlsService },
    { provide: GBOfferService, useValue: mockOfferService }
    ],

    }).compileComponents();

    componentRefInstanceSpy = { cancel: { subscribe: jest.fn() } };

    fixture.detectChanges();

    });
    describe('#initAccountSetup', () => {
    it('should create ZeroUnlinkedGoalsComponent and subscribe to cancel', () => {
    jest.spyOn(component, 'create');
    const spy = jest.spyOn(component, 'reset');
    componentRefInstanceSpy.cancel.subscribe.mockImplementationOnce((callback) => {
    callback();
    });
    fixture.detectChanges();
    component.initZeroUnlinkedTemplate();
    expect(spy).toHaveBeenCalled();
    });
    });
    });


    I get error: Parameter 'callback' implicitly has an 'any' type.

    I tried to mock componentRef but get error. I am trying to accomplish fixing the above error.

    Continue reading...

Compartilhe esta Página