Tests for #766.

@3d0c Thanks for your awesome tests. This PR is just a small refactoring of what I meant in my previous comments.