I have been watching videos and reading articles, but so far they are all either over my head, or are too simplistic. (Looking for a good goldilocks-type practical explanation here.)
I have an angular service that has an observable property, like so, where the property I want to test is message$
:
@Injectable()
export class MyMessageService {
private messageSubj = new BehaviorSubject("");
public message$ = this.messageSubj.pipe(filter(msg => !!msg));
public someFunction(msg: string): void {
this.messageSubj.next(msg);
}
}
message$
should not emit at all, until the subject gets next'd in some way.
All the tests that I've found (at least, that make sense to me), are defining 3 observables: a source$
, expected$
, and result$
, then comparing the last 2, thusly:
it("should be easier to test than this", () => {
scheduler.run((helpers: RunHelpers) => {
const source$ = helpers.cold("-a-b|", { a: "", b: "hello" });
const expected$ = helpers.cold("---b|", { b: "hello" });
const result$ = source$.pipe(filter(x => !!x));
helpers.expectObservable(result$).toEqual(expected$);
});
});
Now, if I want to test my service's message$
observable, how would I alter the above test? Would I replace source$
with const source$ = myService.message$
? Is that correct?
If I do that, I can test that it DOESN'T emit (WOOHOO!). But how can I test it such that, at "a" the source does not emit, but either before or at "b", I call myService.someFunction("hello")
so that at "b", the value is "hello"?
Thanks!