/
menu_item_spec.rb
137 lines (115 loc) · 3.93 KB
/
menu_item_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'spec_helper'
module ActiveAdmin
describe MenuItem do
it "should have a name" do
item = MenuItem.new("Dashboard", "/admin")
item.name.should == "Dashboard"
end
it "should have a url" do
item = MenuItem.new("Dashboard", "/admin")
item.url.should == "/admin"
end
it "should have a priority of 10 by default" do
item = MenuItem.new("Dashboard", "/admin")
item.priority.should == 10
end
it "should accept an optional options hash" do
item = MenuItem.new("Dashboard", "/admin", 10, :if => lambda{ logged_in? } )
end
it "should have a display if block" do
block = lambda{ logged_in? }
item = MenuItem.new("Dashboard", "/admin", 10, :if => block )
item.display_if_block.should == block
end
describe "url generation and caching" do
it "should generate a url if it is a symbol" do
MenuItem.new("Posts", :admin_posts_path).url.should == "/admin/posts"
end
it "should generate a url if it is a string" do
MenuItem.new("Posts", "/admin/posts").url.should == "/admin/posts"
end
end
context "with no children" do
it "should be empty" do
item = MenuItem.new("Blog", "/admin/blog")
item.children.should == []
end
it "should accept new children" do
item = MenuItem.new("Blog", "/admin/blog")
item.add "Dashboard", "/admin"
item.children.first.should be_an_instance_of(MenuItem)
item.children.first.name.should == "Dashboard"
end
end
context "with many children" do
let(:item) do
i = MenuItem.new("Dashboard", "/admin")
i.add "Blog", "/"
i.add "Cars", "/"
i.add "Users", "/", 1
i.add "Settings", "/", 2
i.add "Analytics", "/", 44
i
end
it "should give access to the menu item as an array" do
item['Blog'].name.should == 'Blog'
end
it "should sort items based on priority and name" do
item.children[0].name.should == 'Users'
item.children[1].name.should == 'Settings'
item.children[2].name.should == 'Blog'
item.children[3].name.should == 'Cars'
item.children[4].name.should == 'Analytics'
end
it "children should hold a reference to their parent" do
item["Blog"].parent.should == item
end
end
describe "building children using block syntax" do
let(:item) do
MenuItem.new("Blog", "/") do |blog|
blog.add "Create New", "/blog/new"
blog.add("Comments", "/blog/comments") do |comments|
comments.add "Approved", "/blog/comments?status=approved"
end
end
end
it "should have 2 children" do
item.children.size.should == 2
end
it "should have sub-sub items" do
item["Comments"]["Approved"].name.should == 'Approved'
end
end
describe "accessing ancestory" do
let(:item){ MenuItem.new "Blog", "/blog" }
context "with no parent" do
it "should return an empty array" do
item.ancestors.should == []
end
end
context "with one parent" do
let(:sub_item) do
item.add "Create New", "/blog/new"
item["Create New"]
end
it "should return an array with the parent" do
sub_item.ancestors.should == [item]
end
end
context "with many parents" do
before(:each) do
item.add "C1", "/c1" do |c1|
c1.add "C2", "/c2" do |c2|
c2.add "C3", "/c3"
end
end
end
let(:sub_item){ item["C1"]["C2"]["C3"] }
it "should return an array with the parents in reverse order" do
sub_item.ancestors.should == [item["C1"]["C2"], item["C1"], item]
end
end
end # accessing ancestory
end
end