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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
module SimpleNavigation
describe Item do
let!(:item_container) { ItemContainer.new }
let(:adapter) { double(:adapter) }
let(:item_args) { [item_container, :my_key, 'name', url, options] }
let(:item) { Item.new(*item_args) }
let(:options) { Hash.new }
let(:url) { 'url' }
before { allow(SimpleNavigation).to receive_messages(adapter: adapter) }
describe '#highlights_on' do
let(:options) {{ highlights_on: :test }}
it "returns the item's highlights_on option" do
expect(item.highlights_on).to eq :test
end
end
describe '#initialize' do
context 'when there is a sub_navigation' do
let(:subnav_container) { double(:subnav_container).as_null_object }
shared_examples 'creating sub navigation container' do
it 'creates a sub navigation container with a level+1' do
expect(item.sub_navigation.level).to eq 2
end
end
context 'when a block is given' do
it_behaves_like 'creating sub navigation container' do
let(:item) { Item.new(*item_args) {} }
end
it 'calls the block' do
allow(ItemContainer).to receive_messages(new: subnav_container)
expect{ |blk|
Item.new(*item_args, &blk)
}.to yield_with_args(subnav_container)
end
end
context 'when no block is given' do
context 'and items are given' do
let(:items) { [] }
let(:options) {{ items: items }}
it_behaves_like 'creating sub navigation container'
it "sets the items on the subnav_container" do
expect(item.sub_navigation.items).to eq items
end
end
context 'and no items are given' do
it "doesn't create a new ItemContainer" do
item = Item.new(*item_args)
expect(item.sub_navigation).to be_nil
end
end
end
end
context 'when a :method option is given' do
let(:options) {{ method: :delete }}
it "sets the item's method" do
expect(item.method).to eq :delete
end
end
context 'when no :method option is given' do
it "sets the item's method to nil" do
expect(item.method).to be_nil
end
end
context 'when an :highlights_on option is given' do
let(:highlights_on) { double(:highlights_on) }
let(:options) {{ highlights_on: highlights_on }}
it "sets the item's highlights_on" do
expect(item.highlights_on).to eq highlights_on
end
end
context 'when no :highlights_on option is given' do
it "sets the item's highlights_on to nil" do
expect(item.highlights_on).to be_nil
end
end
context 'when a url is given' do
context 'and it is a string' do
it "sets the item's url accordingly" do
expect(item.url).to eq 'url'
end
end
context 'and it is a proc' do
let(:url) { proc{ "my_" + "url" } }
it "sets the item's url accordingly" do
expect(item.url).to eq 'my_url'
end
end
context 'and it is nil' do
let(:url) { nil }
it "sets the item's url accordingly" do
expect(item.url).to be_nil
end
end
end
context 'when no url nor options is specified' do
let(:item_args) { [item_container, :my_key, 'name'] }
it "sets the item's url to nil" do
expect(item.url).to be_nil
end
end
context 'when only a url is given' do
let(:item_args) { [item_container, :my_key, 'name', 'url'] }
it "set the item's url accordingly" do
expect(item.url).to eq 'url'
end
end
context 'when url and options are given' do
let(:options) {{ html: { option: true } }}
before { allow(adapter).to receive_messages(current_page?: false) }
it "set the item's url accordingly" do
expect(item.url).to eq 'url'
end
it "sets the item's html_options accordingly" do
allow(item).to \
receive_messages(selected_by_subnav?: false,
selected_by_condition?: false)
expect(item.html_options).to include(option: true)
end
end
end
describe '#link_html_options' do
let(:options) {{ link_html: :test }}
it "returns the item's link_html option" do
expect(item.link_html_options).to eq :test
end
end
describe '#method' do
let(:options) {{ method: :test }}
it "returns the item's method option" do
expect(item.method).to eq :test
end
end
describe '#name' do
before do
allow(SimpleNavigation.config).to \
receive_messages(name_generator: proc{ |name| "<span>#{name}</span>" })
end
context 'when no option is given' do
context 'and the name_generator uses only the name' do
it 'uses the default name_generator' do
expect(item.name).to eq '<span>name</span>'
end
end
context 'and the name_generator uses only the item itself' do
before do
allow(SimpleNavigation.config).to \
receive_messages(name_generator: proc{ |name, item| "<span>#{item.key}</span>" })
end
it 'uses the default name_generator' do
expect(item.name).to eq '<span>my_key</span>'
end
end
end
context 'when the :apply_generator is false' do
it "returns the item's name" do
expect(item.name(apply_generator: false)).to eq 'name'
end
end
context 'when a block is given' do
let(:item_args) { [item_container, :my_key, -> { 'Name in block' }, url, options] }
it "returns the item's name that is defined in the block" do
expect(item.name).to include 'Name in block'
end
end
end
describe '#selected?' do
context 'when the item has no :highlights_on option' do
before { allow(SimpleNavigation).to receive_messages(config: config) }
context 'and auto highlighting is off' do
let(:config) { double(:config, auto_highlight: false) }
it 'returns false' do
expect(item.selected?).to be false
end
end
context 'and auto highlighting is on' do
let(:config) { double(:config, ignore_query_params_on_auto_highlight: true, ignore_anchors_on_auto_highlight: true, auto_highlight: true) }
context "and the current url matches the item's url" do
before { allow(adapter).to receive_messages(current_page?: true) }
it 'returns true' do
expect(item.selected?).to be true
end
end
context "and the current url does not match the item's url" do
let(:config) do
double(:config, auto_highlight: false, highlight_on_subpath: false)
end
before { allow(adapter).to receive_messages(current_page?: false) }
it 'returns false' do
expect(item.selected?).to be false
end
end
context 'and highlights_on_subpath is on' do
let(:config) do
double(:config, auto_highlight: true, highlight_on_subpath: true, ignore_query_params_on_auto_highlight: true, ignore_anchors_on_auto_highlight: true)
end
context "and the current url is a sub path of the item's url" do
before do
allow(adapter).to \
receive_messages(current_page?: false, request_uri: 'url/test')
end
it 'returns true' do
expect(item.selected?).to be true
end
end
context "and the current url is not a sub path of the item's url" do
before do
allow(adapter).to \
receive_messages(current_page?: false, request_uri: 'other/test')
end
it 'returns false' do
expect(item.selected?).to be false
end
end
end
end
end
context 'when the item has a :highlights_on option' do
context 'and it is a regular expression' do
before { allow(adapter).to receive_messages(request_uri: '/test') }
context 'and the current url matches the expression' do
let(:options) {{ highlights_on: /test/ }}
it 'returns true' do
expect(item.selected?).to be true
end
end
context 'and the current url does not match the expression' do
let(:options) {{ highlights_on: /other/ }}
it 'returns false' do
expect(item.selected?).to be false
end
end
end
context 'and it is a callable object' do
context 'and the call returns true' do
let(:options) {{ highlights_on: -> { true } }}
it 'returns true' do
expect(item.selected?).to be true
end
end
context 'and the call returns false' do
let(:options) {{ highlights_on: -> { false } }}
it 'returns false' do
expect(item.selected?).to be false
end
end
end
context 'and it is the :subpath symbol' do
let(:options) {{ highlights_on: :subpath }}
context "and the current url is a sub path of the item's url" do
before do
allow(adapter).to receive_messages(request_uri: 'url/test')
end
it 'returns true' do
expect(item.selected?).to be true
end
end
context "and the current url is not a sub path of the item's url" do
before do
allow(adapter).to receive_messages(request_uri: 'other/test')
end
it 'returns false' do
expect(item.selected?).to be false
end
end
end
context 'and it is non usable' do
let(:options) {{ highlights_on: :hello }}
it 'raises an exception' do
expect{ item.selected? }.to raise_error
end
end
end
end
describe '#selected_class' do
context 'when the item is selected' do
before { allow(item).to receive_messages(selected?: true) }
it 'returns the default selected_class' do
expect(item.selected_class).to eq 'selected'
end
context 'and selected_class is defined in the context' do
before { allow(item_container).to receive_messages(selected_class: 'defined') }
it "returns the context's selected_class" do
expect(item.selected_class).to eq 'defined'
end
end
end
context 'when the item is not selected' do
before { allow(item).to receive_messages(selected?: false) }
it 'returns nil' do
expect(item.selected_class).to be_nil
end
end
end
describe ':html_options argument' do
let(:selected_classes) { 'selected simple-navigation-active-leaf' }
context 'when the :class option is given' do
let(:options) {{ html: { class: 'my_class' } }}
context 'and the item is selected' do
before { allow(item).to receive_messages(selected?: true, selected_by_condition?: true) }
it "adds the specified class to the item's html classes" do
expect(item.html_options[:class]).to include('my_class')
end
it "doesn't replace the default html classes of a selected item" do
expect(item.html_options[:class]).to include(selected_classes)
end
end
context "and the item isn't selected" do
before { allow(item).to receive_messages(selected?: false, selected_by_condition?: false) }
it "sets the specified class as the item's html classes" do
expect(item.html_options[:class]).to include('my_class')
end
end
end
context "when the :class option isn't given" do
context 'and the item is selected' do
before { allow(item).to receive_messages(selected?: true, selected_by_condition?: true) }
it "sets the default html classes of a selected item" do
expect(item.html_options[:class]).to include(selected_classes)
end
end
context "and the item isn't selected" do
before { allow(item).to receive_messages(selected?: false, selected_by_condition?: false) }
it "doesn't set any html class on the item" do
expect(item.html_options[:class]).to be_blank
end
end
end
shared_examples 'generating id' do |id|
it "sets the item's html id to the specified id" do
expect(item.html_options[:id]).to eq id
end
end
describe 'when the :id option is given' do
let(:options) {{ html: { id: 'my_id' } }}
before do
allow(SimpleNavigation.config).to receive_messages(autogenerate_item_ids: generate_ids)
allow(item).to receive_messages(selected?: false, selected_by_condition?: false)
end
context 'and :autogenerate_item_ids is true' do
let(:generate_ids) { true }
it_behaves_like 'generating id', 'my_id'
end
context 'and :autogenerate_item_ids is false' do
let(:generate_ids) { false }
it_behaves_like 'generating id', 'my_id'
end
end
context "when the :id option isn't given" do
before do
allow(SimpleNavigation.config).to receive_messages(autogenerate_item_ids: generate_ids)
allow(item).to receive_messages(selected?: false, selected_by_condition?: false)
end
context 'and :autogenerate_item_ids is true' do
let(:generate_ids) { true }
it_behaves_like 'generating id', 'my_key'
end
context 'and :autogenerate_item_ids is false' do
let(:generate_ids) { false }
it "doesn't set any html id on the item" do
expect(item.html_options[:id]).to be_blank
end
end
end
end
end
end
|