Exercise - First Test

Testing

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.

  1. 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 the beforeEach() method.
  2. In the only test() use the TestSchduler instance and invoke the run() method, providing the callback function that is invoked with the RunHelpers object. You'll need the cold and expectObservable properties in the RunHelpers object.
  3. 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.
  4. Define a new source Observable using the cold() function, providing the marble string and the values object. Use the map() operator to multiply next notification values by 10.
  5. Define a new expected string that represents the desired behavior of the source Observable.
  6. Use the expectObservable() function to create the assertion of the source Observable to be deeply equal to the expected behavior. Don't forget to also provide the values object as the second argument to the toBe() function.

Exercise - First Test

Testing

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.

  1. 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 the beforeEach() method.
  2. In the only test() use the TestSchduler instance and invoke the run() method, providing the callback function that is invoked with the RunHelpers object. You'll need the cold and expectObservable properties in the RunHelpers object.
  3. 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.
  4. Define a new source Observable using the cold() function, providing the marble string and the values object. Use the map() operator to multiply next notification values by 10.
  5. Define a new expected string that represents the desired behavior of the source Observable.
  6. Use the expectObservable() function to create the assertion of the source Observable to be deeply equal to the expected behavior. Don't forget to also provide the values object as the second argument to the toBe() function.

Copyright

Site © by LiveLoveApp, LLC

We Can Help!

Need help with RxJS in your project?

Hire Us