def test_render_node_template_tag_with_children_and_current(self): child1 = menu.Node('c1', 'c1', url='c1') child2 = menu.Node('c2', 'c2', url='c2') child3 = menu.Node('c3', 'c3', url='c3') node = menu.Node('test', 'Test', url='http://test.com', children=[ child1, child2, child3, ]) output = navutils_tags.render_node({'current_menu_item': 'test:c3'}, node=node, user=self.user) self.assertHTMLEqual( output, """ <li class="menu-item has-children has-current has-dropdown"> <a href="http://test.com">Test</a> <ul class="sub-menu dropdown"> <li class="menu-item"><a href="c1">c1</a></li> <li class="menu-item"><a href="c2">c2</a></li> <li class="menu-item current"><a href="c3">c3</a></li> </ul> </li> """)
def test_menu_node_sort_children_by_weight(self): child1 = menu.Node('c1', 'Child1', weight=3, url='http://test.com/child1') child2 = menu.Node('c2', 'Child2', weight=1, url='http://test.com/child2') child3 = menu.Node('c3', 'Child3', weight=2, url='http://test.com/child3') children = [child1, child2, child3] parent = menu.Node('test', 'Test', url='http://test.com', children=children) self.assertEqual(parent.children, [child1, child3, child2]) # we add another child, order should be updated child4 = menu.Node('c4', 'Child4', weight=999, url='http://test.com/child4') parent.add(child4) self.assertEqual(parent.children, [child4, child1, child3, child2])
def test_children_keep_reference_to_parent(self): child = menu.Node('c', 'Child', url='http://test.com/child') parent = menu.Node('test', 'Test', url='http://test.com', children=[child]) self.assertEqual(child.parent, parent)
def test_node_id_is_built_from_menu_and_parent(self): subchild = menu.Node('sc', 'SubChild', url='http://test.com/subchild') child = menu.Node('c', 'Child', url='http://test.com/child', children=[subchild]) parent = menu.Node('test', 'Test', url='http://test.com', children=[child]) self.assertEqual(parent.id, 'test') self.assertEqual(child.id, 'test:c') self.assertEqual(subchild.id, 'test:c:sc')
def test_node_depth(self): subchild = menu.Node('sc', 'SubChild', url='http://test.com/subchild') child = menu.Node('c', 'Child', url='http://test.com/child', children=[subchild]) parent = menu.Node('test', 'Test', url='http://test.com', children=[child]) self.assertEqual(parent.depth, 0) self.assertEqual(child.depth, 1) self.assertEqual(subchild.depth, 2)
def test_render_node_template_tag_with_embedded_template(self): node = menu.Node('test', '{{ 1|add:"2" }}', url='http://test.com') output = navutils_tags.render_node({}, node=node, user=self.user) self.assertHTMLEqual( output, '<li class="menu-item"><a href="http://test.com">3</a></li>')
def test_render_node_template_tag(self): node = menu.Node('test', 'Test', url='http://test.com') output = navutils_tags.render_node({}, node=node, user=self.user) self.assertHTMLEqual( output, '<li class="menu-item"><a href="http://test.com">Test</a></li>')
def test_render_node_template_tag_with_node_attrs(self): attrs = {'id': 'important'} node = menu.Node('test', 'Test', url='http://test.com', attrs=attrs) output = navutils_tags.render_node({}, node=node, user=self.user) self.assertHTMLEqual( output, """<li class="menu-item" id="important"> <a href="http://test.com">Test</a> </li>""")
def test_render_node_template_tag_with_children_and_depth(self): subchild = menu.Node('s1', 's1', url='s1') child1 = menu.Node('c1', 'c1', url='c1', children=[subchild]) child2 = menu.Node('c2', 'c2', url='c2') child3 = menu.Node('c3', 'c3', url='c3') node = menu.Node('test', 'Test', url='http://test.com', children=[ child1, child2, child3, ]) output = navutils_tags.render_node({}, node=node, user=self.user, max_depth=1) self.assertHTMLEqual( output, """ <li class="menu-item has-children has-dropdown"> <a href="http://test.com">Test</a> <ul class="sub-menu dropdown"> <li class="menu-item"><a href="c1">c1</a></li> <li class="menu-item"><a href="c2">c2</a></li> <li class="menu-item"><a href="c3">c3</a></li> </ul> </li> """) output = navutils_tags.render_node({}, node=node, user=self.user, max_depth=0) self.assertHTMLEqual( output, """ <li class="menu-item"> <a href="http://test.com">Test</a> </li> """)
def test_render_node_template_tag_with_embedded_template_and_context(self): node = menu.Node('test', '{{ foo }}', url='http://test.com', context={'foo': 'bar'}) output = navutils_tags.render_node({}, node=node, user=self.user) self.assertHTMLEqual( output, '<li class="menu-item"><a href="http://test.com">bar</a></li>')
def test_children_accept_a_callable(self): def generate_children(): return [menu.Node(i, i, url='#') for i in range(5)] parent = menu.Node('test', 'Test', url='http://test.com', children=generate_children) for i in range(5): self.assertEqual(parent.children[i].id, i)
def test_render_node_template_tag_with_extra_context(self): node = menu.Node('test', 'Test', url='http://test.com', template='test_app/test_node.html', context={'foo': 'bar'}) output = navutils_tags.render_node({}, node=node, user=self.user) self.assertHTMLEqual( output, '<li class="menu-item"><a href="http://test.com">Test bar</a></li>' )
def test_render_node_template_tag_with_link_attrs(self): attrs = {'target': '_blank', 'title': 'Click me !'} node = menu.Node('test', 'Test', url='http://test.com', link_attrs=attrs) output = navutils_tags.render_node({}, node=node, user=self.user) self.assertHTMLEqual( output, """<li class="menu-item"> <a href="http://test.com" target="_blank" title="Click me !">Test</a> </li>""")
def test_template_tag(self): main_menu = menu.Menu('main') node = menu.Node('test', 'Test', url='http://test.com') main_menu.register(node) output = navutils_tags.render_menu({}, menu=main_menu, user=self.user) self.assertHTMLEqual( output, """ <ul class="main-menu"> <li class="menu-item"><a href="http://test.com">Test</a></li> </ul> """)
def test_context_available_in_template(self): main_menu = menu.Menu('main') node = menu.Node('context', 'Context', url='http://test-context.com', template='test_app/test_node.html') main_menu.register(node) output = navutils_tags.render_menu({'foo': 'bar'}, menu=main_menu, user=self.user) self.assertHTMLEqual( output, """ <ul class="main-menu"> <li class="menu-item"><a href="http://test-context.com">Context bar</a></li> </ul> """)
def test_menu_node_allows_django_pattern_name(self): node = menu.Node('test', 'Test', pattern_name='index') self.assertEqual(node.get_url(), '/')
def test_menu_node_allows_arbitrary_url(self): node = menu.Node('test', 'Test', url='http://test.com') self.assertEqual(node.get_url(), 'http://test.com')
def test_menu_node_is_viewable_by_anobody(self): node = menu.Node('test', 'Test', url='http://test.com') self.assertTrue(node.is_viewable_by(self.user))
def generate_children(): return [menu.Node(i, i, url='#') for i in range(5)]
def test_menu_can_register_nodes(self): main_menu = menu.Menu('main') node = menu.Node('test', 'Test', url='http://test.com') main_menu.register(node) self.assertEqual(main_menu['test'], node)
from navutils import menu main_menu = menu.Menu('main') menu.register(main_menu) library = menu.Node(id='library', label='Library', pattern_name='library:media-list') main_menu.register(library)