Я написал модульный тест для проверки метода с измененным значением. Мне нужно оценить остальную часть IsNotNullOrUndefined (event.value). Я попытался переопределить возвращаемое значение службы, как показано в тесте, отмеченном как подходящий. Тест не пройден с ошибкой. Похоже, расширение не переопределяет его должным образом. Может кто-нибудь сказать мне, в чем может быть проблема
Expected [ Object({ name: 'EVP', version: '2.0.0.0', issued: Date(Wed Apr 22 2020 17:35:38 GMT+0100 (British Summer Time)), deferred: 'Yes (142)', acceptance: '3 d', updated:
Date(Wed Apr 22 2020 17:35:38 GMT+0100 (British Summer Time)), id: 23 }) ] to be null.
TestComponent
describe('MyAgreementsComponent', () => {
let component: MyAgreementsComponent;
let fixture: ComponentFixture<MyAgreementsComponent>;
let mockAgreementsService: Mock<AgreementsService>;
const agreementModelObject = AgreementIssuedModelObjectMock;
const agreementHistoryModel = AgreementHistoryModelMock;
const setupComponent = () => {
fixture = TestBed.createComponent(MyAgreementsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
};
beforeEach(() => {
mockAgreementsService = new Mock<AgreementsService>({
getAgreement: () => of(agreementModelObject),
getAgreementsHistory: () => of(agreementHistoryModel),
});
setupComponent();
});
configureTestSuite(() => {
TestBed.configureTestingModule({
imports: [ DevExpressModule,
DxDataGridModule,
DxButtonModule],
declarations: [MyAgreementsComponent, MockComponent(SpinnerComponent)],
providers: [
{ provide: AgreementsService, useFactory: () => mockAgreementsService.Object },
// { provide: Router, useValue: mockRouter },
{ provide: Router, useClass: MockHasRouteRouter },
{ provide: ActivatedRoute, useClass: MockActivatedRoute }
]
});
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('#onValueChanged', () => {
fit('should not filter agreements when onValueChanged is called with no argument', () => {
mockAgreementsService.extend({ getAgreementsHistory: () => of(null) });
fixture.detectChanges();
component.onValueChanged({ event: null , value: null });
expect(component.agreementsHistory).toBeNull();
});
it('should filter agreements that are complete when onValueChanged is called with a true event', () => {
component.agreementsHistoryData = _.cloneDeep(AgreementHistoryModelMock);
component.onValueChanged({ event: true , value: true });
expect(component.agreementsHistory[0].acceptance).toEqual('N/A');
});
it('should filter agreements that are not complete when onValueChanged is called with a true event', () => {
component.agreementsHistoryData = _.cloneDeep(AgreementHistoryModelMock);
component.onValueChanged({ event: false , value: false });
expect(component.agreementsHistory[0].acceptance).not.toEqual('N/A');
});
});
});
Код компонента
public onValueChanged(event) {
if (IsNotNullOrUndefined(event.value)) {
if (event.value) {
this.agreementsHistory = this.agreementsHistoryData.filter(a => a.acceptance === 'N/A');
} else {
this.getNonCompletedAgreements();
}
}
}