Change setTimeout by $timeout, add tests