I need helps about test in Angular. I have a method which in I use a subscribe, after lot of researchs I don’t found a solution thats corresponding to my situation,
my component
In this component I want to test ngOnInit
port { Component, OnInit } from '@angular/core';
import { ProjectEditionService } from '../../../project-view/services/project-edition.service';
@Component({
selector: 'ngx-brush-width-popover',
templateUrl: './brush-width-popover.component.html',
styleUrls: ['./brush-width-popover.component.scss'],
})
export class BrushWidthPopoverComponent implements OnInit {
private canvas: fabric.Canvas;
public brushWidth: number = 1;
public readonly MIN_WIDTH: number = 1;
public readonly MAX_WIDTH: number = 100;
public readonly STEP: number = 0.1;
constructor(private projectEditionService: ProjectEditionService) {}
ngOnInit(): void {
this.projectEditionService.currentCanvas$.subscribe((canvas) => {
this.canvas = canvas;
this.brushWidth = this.canvas.freeDrawingBrush.width;
});
}
My spec file
import { ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing';
import { ProjectEditionService } from 'app/feature/project-view/services/project-edition.service';
import { fabric } from 'fabric';
import { of } from 'rxjs';
import { compileFunction } from 'vm';
import { BrushWidthPopoverComponent } from './brush-width-popover.component';
let canvas = new fabric.Canvas("canvas", {
width: 1280,
});
fdescribe('BrushWidthPopoverComponent', () => {
let component: BrushWidthPopoverComponent;
let fixture: ComponentFixture<BrushWidthPopoverComponent>;
let projectEditionService: ProjectEditionService;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [BrushWidthPopoverComponent],
providers: [
ProjectEditionService,
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(BrushWidthPopoverComponent);
component = fixture.componentInstance;
fixture.detectChanges();
projectEditionService = TestBed.inject(ProjectEditionService);
component['canvas'] = { freeDrawingBrush: {} } as fabric.Canvas;
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('ngOnInit', () => {
it('onInit', () => {
component.ngOnInit();
expect(canvas).toBeDefined();
expect(component.brushWidth).toEqual(1);
expect(canvas.freeDrawingBrush.width).toEqual(1);
expect(component.brushWidth).toEqual(canvas.freeDrawingBrush.width);
});
});
My test pass but am not sure if it’s test correctly my method ngOnInit because I don’t use projectEditionservice in my test.
thnks if you can help me to find a good solution