Clean up widget JS + add tests. Closes #486.