def test_create_element_defclass(self): from wdom import element doc = WdomDocument(default_class=element.HTMLElement) elm = doc.createElement('a') self.assertEqual(type(elm), A) self.assertRegex(elm.html, '<a wdom_id="\d+"></a>')
def test_create_element_defclass_unknown(self): from wdom import element doc = WdomDocument(default_class=element.HTMLElement) elm = doc.createElement('aa') self.assertEqual(type(elm), element.HTMLElement) self.assertRegex(elm.html, '<aa></aa>')
class TestWdomDocument(TestCase): def setUp(self) -> None: super().setUp() self.doc = WdomDocument() self.doc.defaultView.customElements.reset() server._tornado.connections = [1] def test_blankpage(self) -> None: _re = re.compile( '\s*<!DOCTYPE html>' '\s*<html>' '\s*<head>' '\s*<meta charset="utf-8">' '\s*<title>' '\s*W-DOM' '\s*</title>' '(\s*<script type="text/javascript">.*?</script>)?' '\s*</head>' '\s*<body>' '\s*<script type="text/javascript">' '.*?</script>' '\s*</body>' '.*</html>', re.S ) html = self.doc.build() self.assertIsNotNone(_re.match(remove_wdom_id(html))) def test_get_element_by_id(self): elm = WdomElement(tag='a', id='a', wdom_id='b') self.assertIs(getElementById('a'), elm) self.assertIs(getElementByWdomId('b'), elm) self.assertIsNone(self.doc.getElementById('a')) self.assertIsNone(self.doc.getElementByWdomId('b'), elm) self.doc.appendChild(elm) self.assertIs(getElementById('a'), elm) self.assertIs(getElementByWdomId('b'), elm) self.assertIs(self.doc.getElementById('a'), elm) self.assertIs(self.doc.getElementByWdomId('b'), elm) def test_add_jsfile(self) -> None: self.doc.add_jsfile('jsfile') _re = re.compile( '<body.*' '<script( src="jsfile"| type="text/javascript"| wdom_id="\d+"){3}' '>\s*</script>' '.*</body', re.S ) self.assertIsNotNone(_re.search(self.doc.build())) def test_add_cssfile(self) -> None: self.doc.add_cssfile('cssfile') _re = re.compile( '<head wdom_id="\d+">.*' '<link( href="cssfile"| rel="stylesheet"| wdom_id="\d+"){3}>' '.*</head>' '', re.S ) self.assertIsNotNone(_re.search(self.doc.build())) def test_add_header_link(self) -> None: self.doc.add_header('<link href="cssfile" rel="stylesheet">') self.assertIn( '<link href="cssfile" rel="stylesheet">', self.doc.build(), ) def test_add_header_script(self) -> None: self.doc.add_header( '<script src="jsfile" type="text/javascript"></script>') self.assertIn( '<script src="jsfile" type="text/javascript"></script>', self.doc.build(), ) def test_title(self) -> None: doc = WdomDocument(title='TEST') _re = re.compile( '<title wdom_id="\d+">\s*TEST\s*</title>', re.S ) html = doc.build() self.assertIsNotNone(_re.search(html)) self.assertNotIn('W-DOM', html) self.assertEqual('TEST', doc.title) def test_charset(self) -> None: doc = WdomDocument(charset='TEST') _re = re.compile( '<meta( charset="TEST"| wdom_id="\d+"){2}>', re.S ) html = doc.build() self.assertIsNotNone(_re.search(html)) self.assertNotIn('utf', html) self.assertEqual('TEST', doc.characterSet) def test_set_body(self) -> None: self.doc.body.prepend(Tag()) html = self.doc.build() _re = re.compile( '<tag wdom_id="\d+">\s*</tag>', re.S ) self.assertIsNotNone(_re.search(html)) def test_set_body_string(self) -> None: string = 'testing' self.doc.body.prepend(string) html = self.doc.build() self.assertIn(string, html) def test_set_body_error(self) -> None: with self.assertRaises(TypeError): self.doc.body.prepend(1) def test_create_element(self): elm = self.doc.createElement('a') self.assertEqual(type(elm), A) self.assertRegex(elm.html, r'<a wdom_id="\d+"></a>') def test_create_element_unknown(self): elm = self.doc.createElement('aa') self.assertEqual(type(elm), WdomElement) self.assertRegex(elm.html, r'<aa wdom_id="\d+"></aa>') def test_create_element_defclass(self): from wdom import element doc = WdomDocument(default_class=element.HTMLElement) elm = doc.createElement('a') self.assertEqual(type(elm), A) self.assertRegex(elm.html, '<a wdom_id="\d+"></a>') def test_create_element_defclass_unknown(self): from wdom import element doc = WdomDocument(default_class=element.HTMLElement) elm = doc.createElement('aa') self.assertEqual(type(elm), element.HTMLElement) self.assertRegex(elm.html, '<aa></aa>') def test_create_custom_element(self): class A(WdomElement): pass self.doc.defaultView.customElements.define('a', A) elm = self.doc.createElement('a') self.assertEqual(type(elm), A) self.assertRegex(elm.html, '<a wdom_id="\d+"></a>') def test_create_custom_element_tag(self): class A(Tag): tag = 'a' self.doc.defaultView.customElements.define('a', A) elm = self.doc.createElement('a') self.assertEqual(type(elm), A) self.assertRegex(elm.html, '<a wdom_id="\d+"></a>') def test_custom_tag_theme_tag(self): from wdom import themes self.doc.register_theme(themes) elm = Tag(parent=self.doc.body) elm.innerHTML = '<div is="container"></div>' self.assertTrue(isinstance(elm.firstChild, themes.Container)) def test_custom_tag_theme_default(self): from wdom.util import suppress_logging suppress_logging() from wdom.themes import default from wdom import themes self.doc.register_theme(default) elm = Tag(parent=self.doc.body) elm.innerHTML = '<div is="container"></div>' self.assertTrue(isinstance(elm.firstChild, default.Container)) self.assertTrue(isinstance(elm.firstChild, themes.Container)) def test_custom_tag_theme(self): from wdom.themes import bootstrap3 from wdom import themes self.doc.register_theme(bootstrap3) elm = Tag(parent=self.doc.body) elm.innerHTML = '<div is="container"></div>' self.assertTrue(isinstance(elm.firstChild, bootstrap3.Container)) self.assertTrue(isinstance(elm.firstChild, themes.Container)) self.assertIn('maxcdn.bootstrapcdn.com', self.doc.build()) elm.innerHTML = '<button is="default-button"></button>' self.assertTrue(isinstance(elm.firstChild, bootstrap3.DefaultButton)) self.assertTrue(isinstance(elm.firstChild, bootstrap3.Button)) self.assertFalse(isinstance(elm.firstChild, themes.DefaultButton)) self.assertTrue(isinstance(elm.firstChild, themes.Button)) def test_tempdir(self): doc = WdomDocument() self.assertIsNotNone(doc.tempdir) self.assertTrue(os.path.exists(doc.tempdir)) self.assertTrue(os.path.isabs(doc.tempdir)) self.assertTrue(os.path.isdir(doc.tempdir)) tempdir = doc.tempdir testfile = os.path.join(tempdir, 'test_file') with open(testfile, 'w') as f: f.write('test') self.assertTrue(os.path.exists(testfile)) self.assertTrue(os.path.isfile(testfile)) with open(testfile) as f: self.assertEqual('test', f.read().strip()) del doc import gc gc.collect() self.assertFalse(os.path.exists(testfile)) self.assertFalse(os.path.exists(tempdir)) def test_add_eventlistener(self): mock = MagicMock(_is_coroutine=False) js_mock = MagicMock() doc = get_document() doc.js_exec = js_mock doc.addEventListener('click', mock) js_mock.assert_called_once_with('addEventListener', 'click') msg = { 'type': 'click', 'currentTarget': {'id': 'document'}, 'target': {'id': 'document'}, } e = event_handler(msg) mock.assert_called_once_with(e) def test_wdom_id(self): self.assertEqual(self.doc.wdom_id, 'document')