Exercise - Subscription
Recap
Previously, we created a test using a cold Observable.
The cold Observable is immediately subscribed to, not by us, rather by the expectObservable() function.
And, further, the cold observable was never unsubscribed from.
In this exercise we are going to use the hot() function that is a property in the RunHelpers object.
This function enables us to create a hot Observable.
Using a hot Observable we can assert for the notifications that occur while an Observer is subscribed.
As a quick recap, there are two characters that we use to signal the subscribing and unsubscribing events of an Observer:
^represents the subscribing of an Observer.!represents the unsubscribing of an Observer.
Exercise
In this exercise we are going to mock a hot Observable. The behavior of the source hot Observable is:
- Next notification value of
0in frame 100. - Next notification value of
1in frame 200. - Next notification value of
1in frame 300. - The Observable completes in the next frame (301).
You can think of this Observable as an interval that emits an incrementing integer starting from 0 every 100 milliseconds, and completes after 3 next notifications.
It might look like this:
interval(100).pipe(take(3));
We want to test two subscription scenarios:
- An Observer subscribes after 200 milliseconds, and then unsubscribes after 300 milliseconds (or 100 milliseconds later).
- An Observer subscribes after 300 milliseconds, and never unsubscribes.
Here is your task:
- Open exercise on codesandbox.
I have already imported the necessary
TestSchedulerclass, and set it up in thebeforeEach()method. - Use the
run()method on theTestSchedulerinstance, providing the callback function that is invoked with theRunHelpersobject. You will need thehotandexpectObservableproperties. - Define a new
valuesobject that has three properties for each next notification and the associated values. In this test, we're asserting the timing of the subscriptions of each Observer and the next notifications that we expect. Therefore, the values are not important in this test. In fact, you could just skip setting up thevaluesobject if you prefer. - Create the
sourceObservable using thehot()function, specifying the marble string, and optionally thevaluesobject as noted previously. - Define a new
subscription1string that represents the first Observer and its subscribing and unsubscribing behaviors. - Define a new
subscription2string that represents the second Observer and its subscribing and unsubscribing behaviors. - Define the
expected1string that represents the next notifications that the first Observer receives. - Define the
expected2string that represents the next notifications that the second Observer receives. - Invoke the
expectObservable()function. The first argument is thesourceObservable, and the second argument is thesubscription1string. Chain thetoBe()function and provide theexpected1string, and optionally, thevaluesobject. - Repeat the previous step.
This time, use the
subscription2andexpected2strings.