I'm trying to test an Angular Component
that basically receives an Observable
and changes its template
based on the values emitted from that Observable. Here's a simplified version:
@Component({
selector: 'async-text',
template: `
<span>{{ text | async }}</span>
`,
})
export class AsyncTextComponent {
@Input() text: Observable<string>;
}
I'd like to test it out, and currently this is what I have, using rxjs-marbles
(though it's not a must).
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AsyncTextComponent } from './async-text.component';
describe('AsyncTextComponent', () => {
let component: BannerComponent;
let fixture: AsyncTextComponent<AsyncTextComponent>;
it('...',
marbles(m => {
fixture = TestBed.createComponent(AsyncTextComponent);
component = fixture.componentInstance;
component.text = m.cold('-a-b-c|', {
a: 'first',
b: 'second',
c: 'third',
});
fixture.detectChanges();
expect(component.nativeElement.innerHTML).toContain('first');
fixture.detectChanges();
expect(component.nativeElement.innerHTML).toContain('second');
fixture.detectChanges();
expect(component.nativeElement.innerHTML).toContain('third');
})
);
});
Obviously this doesn't work. My issue is that I didn't find a way to advance the TestScheduler by a given amount of 'frames' between each expect
.
How can I manually skip frames? Or alternatively, is there a better way to test the above component/scenario (Angular component that receives an Observable
and I want to test it's behaviour given the Observable's emittions).
I did see .flush()
, but as documented, it runs all of the Observable's emits, so I'd get to the final status, and can't test out different transitions between states.
Thanks