def test_create_event(self): tag = WdomElement('tag') mock = MagicMock(_is_coroutine=False) tag.addEventListener('a', mock) e = self.doc.createEvent('a') self.assertEqual(type(e), Event) tag.dispatchEvent(e) mock.assert_called_once_with(e)
class TestEventMessage(TestCase): def setUp(self): self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock) self.elm = WdomElement('tag') set_app(self.elm) self.elm.js_exec = MagicMock() self.mock = MagicMock(_is_coroutine=False) self.elm.addEventListener('click', self.mock) msg = {'type': 'click', 'currentTarget': {'id': self.elm.wdom_id}} self.event = create_event(msg) def tearDown(self): _tornado.connections.remove(self.conn_mock) def test_handle_event(self): self.elm.js_exec.assert_called_once_with('addEventListener', 'click') self.elm.dispatchEvent(self.event) self.assertTrue(self.mock.called) def test_remove_event(self): self.elm.removeEventListener('click', self.mock) self.elm.js_exec.assert_called_with('removeEventListener', 'click') self.elm.dispatchEvent(self.event) self.mock.assert_not_called() def test_remove_multi_event(self): self.elm.addEventListener('click', self.mock) self.elm.removeEventListener('click', self.mock) with self.assertRaises(AssertionError): self.elm.js_exec.assert_has_calls( call('removeEventListener', 'click')) self.elm.removeEventListener('click', self.mock) self.elm.js_exec.assert_called_with('removeEventListener', 'click') self.elm.dispatchEvent(self.event) self.mock.assert_not_called()