Я пишу тесты для проверки, вызывается ли родительский компонент, когда дочерний компонент генерирует некоторые данные, вот как я настроил свои компоненты для тестирования:
describe('OperationComponent', () => {
let component: OperationComponent;
let fixture: ComponentFixture<OperationComponent>;
let store: Store;
let de: DebugElement;
let data: any;
let cmp: any;
beforeEach(async(() => {
const {
imports,
providers
} = configureDefaultTestingMouduleForSecureComponent(OperationComponent);
TestBed.resetTestingModule();
TestBed.configureTestingModule({
declarations: [OperationComponent],
imports: [...imports, NgxsModule.forFeature([RoutesOperationState])],
providers: [...providers, RoutesOperationEntity]
}).compileComponents();
store = TestBed.get(Store);
}));
beforeEach(() => {
fixture = TestBed.createComponent(OperationComponent);
component = fixture.componentInstance;
de = fixture.debugElement;
data = de.query(By.directive(RouteOperationsComponent));
cmp = data.componentInstance;
component.routeId = mockRoutes[0].id;
});
describe('cancel action', () => {
it('should be called on cancelOperation when it emits data', () => {
jest.spyOn(component, 'handleCancelRouteOperation');
cmp.cancelRouteOperation.emit(true);
expect(component.handleCancelRouteOperation).toHaveBeenCalledWith(true);
});
});
configureDefaultTestingMouduleForSecureComponent()
имеет общий доступ модули и другие angular модули. Я хочу использовать beforeAll()
вместо beforeEach()
, потому что использование beforeEach()
замедляет мой набор тестов. Однако, когда я использую beforeAll()
, мой дочерний компонент RoureOperationsComponent
не создается правильно и выдает ошибку.
expect(spy).toHaveBeenCalledWith(...expected)
Expected: true
Number of calls: 0