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
TestSchedulerin thebeforeEach()method. - In the only
test()use theTestSchdulerinstance and invoke therun()method, providing the callback function that is invoked with theRunHelpersobject. You'll need thecoldandexpectObservableproperties in theRunHelpersobject. - Define a
valuesobject 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
sourceObservable using thecold()function, providing the marble string and thevaluesobject. Use themap()operator to multiply next notification values by 10. - Define a new
expectedstring that represents the desired behavior of thesourceObservable. - Use the
expectObservable()function to create the assertion of thesourceObservable to be deeply equal to theexpectedbehavior. Don't forget to also provide thevaluesobject as the second argument to thetoBe()function.