Angular Testing. Override component's service

component.ts

@Component({
  selector: 'my-component',
  template: '',
  providers: [ComponentService], // <=== component's service
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyComponent implements OnInit, OnDestroy { ... }

angular error during cleanup of component

component.spec.ts


  const windowMock = {
    location: {
      href: null
    }
  };

  const ComponentServiceSpy = {
    ...jasmine.createSpyObj<ComponentService>('ComponentService', [
      'init',
      'orderDomains',
      'destroy',
      'ngOnDestroy' // <=== angular error during cleanup of component
    ]),
    state$: of({
      ok: {
        preloader: false,
        disabled: true,
      },
      cancel: {
        disabled: false,
      },
    } as FormButtonsState),
  };  

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        ReactiveFormsModule,
        RouterTestingModule,
        MyTestModule,
      ],
      providers: [
        { provide: ModalService, useValue: modalServiceSpy },
        { provide: WINDOW, useValue: windowMock }
      ],
      declarations: [MyComponent, MyComponentChildrenMockComponent],
    })
      .overrideProvider(ComponentService, { // override
        useValue: ComponentServiceSpy,
      })
      .compileComponents();
  }));

Похожие записи

RxJS Pipeable Operators

Начиная с версии rxjs 5.5 операторы вместо цепочки вызовов применяются как параметры функции pipe.

ngx translate attribute

Используется конструкция

<img src="image.jpg" [alt]="'KEY' | translate"> 
19 августа 2018 г. в Angular

Upload файла в Angular по клику кнопки

Создано простейшее nestjs приложение, принимающее файлы. Выбор и отправка файла по клику подразумевает, что на форме отсутствует input для файла. Отправка запроса осуществляется с отслеживанием прогресса.

Angular environment service

Использование сервиса для окружения вместо прямой ссылки на environment.ts