Inspired by Crewmeister
The challenge here is
- to make the asynchronous code readable and
- NOT use any timeouts, make the tests fast and
- to handle all edge cases properly, which might get tricky ones async calls get parallelized and return errors.
What is the task?
A time tracking tool, that optionally also accepts GPS positions.
Implement the collecting of all GPS data (using promises) and sending to the server using some
asynchronous method, like XHR or fetch (using promises too).
clockIn() function that returns a promise and works as the diagrams below show.
Or in other words: the “boss” can determine
- if only times shall be tracked, without any GPS position recoding (one async action, the XHR) or
- GPS can be optionally added to the XHR data to be sent, getting GPS is async too,
- require GPS data to be sent with the XHR
Where to start?
- start with case 1), the simplest
- mock/stub the actual XHR and build all cases incl. error cases that might happen when sending an XHR
- mock/stub the GPS-data retreival, implement case 2), watch out there are multiple execution paths
- case 3) incl. retrying should be a piece of cake now
- prevent that any test times out, ensure that all Promise-paths are tested
If you want to get more challenging you can go the next steps like this:
- search for a library that makes testing with Promises easier
- use one or many library to find out which one suites best your needs
- mock that getting retreiving GPS data takes very long, trigger a time out and report it to the user
- write tests that verify the timing of the things happening, e.g. that the clock-in call NEVER starts before the gps-retreival had finished
For some code feel free to have a look at the original repo.