fix handleEvents bug and add test