Exercise - First Test
Exercise
Write a unit test for a cold Observable that after 1000 milliseconds emits a number, which is then mapped to a new value that is 10 times the source next notification.
For example, here is what the Observable might look like:
from([1]).pipe(
delay(1000),
map((value) => value * 10)
);
Assert that the source Observable behavior of multiplying next notification values by 10.
- Open exercise on codesandbox.
I have already imported the necessary functions and set up the test.
Take note that we are creating a new
TestScheduler
in thebeforeEach()
method. - In the only
test()
use theTestSchduler
instance and invoke therun()
method, providing the callback function that is invoked with theRunHelpers
object. You'll need thecold
andexpectObservable
properties in theRunHelpers
object. - Define a
values
object that contains properties that are any alphabetic or numeric character, along with properties for each value. In this test we'll have a starting value, and an expected value that is 10 times the starting value. - Define a new
source
Observable using thecold()
function, providing the marble string and thevalues
object. Use themap()
operator to multiply next notification values by 10. - Define a new
expected
string that represents the desired behavior of thesource
Observable. - Use the
expectObservable()
function to create the assertion of thesource
Observable to be deeply equal to theexpected
behavior. Don't forget to also provide thevalues
object as the second argument to thetoBe()
function.