Writing a Real-world Test
Get familiar with writing a real-world test.
We'll cover the following...
There’s no better way to understand how to bend time using virtual time than to write a test for a time-sensitive task in the real world. Let’s recover an Observable from the earthquake viewer we made earlier.
quakes.pluck('properties').map(makeRow).bufferWithTime(500).filter(function(rows){return rows.length > 0;}).map(function(rows){var fragment = document.createDocumentFragment();rows.forEach(function(row){fragment.appendChild(row);});return fragment;}).subscribe(function(fragment){table.appendChild(fragment);});
To make the code more testable, let’s encapsulate the Observable in a function that takes a Scheduler we use in the bufferWithTime
operator. It’s always a good idea to parameterize Schedulers in Observables that will be tested.
function quakeBatches(scheduler){return quakes.pluck('properties').bufferWithTime(500, null, scheduler || null).filter(function(rows){return rows.length > 0;});}
Let’s also simplify the code by taking some steps out, but retaining the essence of it. This code takes an Observable of JSON objects that contain a properties
property, buffers them into batches released every 500 milliseconds, and filters the batches that arrive empty.
We want to verify that this code works, but ...