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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
|
module SimpleNavigation
describe ItemContainer do
subject(:item_container) { ItemContainer.new }
shared_examples 'adding the item to the list' do
it 'adds the item to the list' do
allow(Item).to receive_messages(new: item)
item_container.item(*args)
expect(item_container.items).to include(item)
end
end
shared_examples 'not adding the item to the list' do
it "doesn't add the item to the list" do
allow(Item).to receive_messages(new: item)
item_container.item(*args)
expect(item_container.items).not_to include(item)
end
end
describe '#initialize' do
it 'sets an empty items array' do
expect(item_container.items).to be_empty
end
end
describe '#dom_attributes' do
let(:dom_attributes) {{ id: 'test_id', class: 'test_class' }}
before { item_container.dom_attributes = dom_attributes }
it "returns the container's dom_attributes" do
expect(item_container.dom_attributes).to eq dom_attributes
end
context 'when the dom_attributes do not contain any id or class' do
let(:dom_attributes) {{ test: 'test' }}
context "and the container hasn't any dom_id" do
it "returns the contaier's dom_attributes without any id" do
expect(item_container.dom_attributes).not_to include(:id)
end
end
context 'and the container has a dom_id' do
before { item_container.dom_id = 'test_id' }
it "returns the contaier's dom_attributes including the #dom_id" do
expect(item_container.dom_attributes).to include(id: 'test_id')
end
end
context "and the container hasn't any dom_class" do
it "returns the contaier's dom_attributes without any class" do
expect(item_container.dom_attributes).not_to include(:class)
end
end
context 'and the container has a dom_class' do
before { item_container.dom_class = 'test_class' }
it "returns the contaier's dom_attributes including the #dom_class" do
expect(item_container.dom_attributes).to include(class: 'test_class')
end
end
end
end
describe '#items=' do
let(:item) {{ key: :my_key, name: 'test', url: '/' }}
let(:items) { [item] }
let(:item_adapter) { double(:item_adapter).as_null_object }
let(:real_item) { double(:real_item) }
before do
allow(ItemAdapter).to receive_messages(new: item_adapter)
allow(item_adapter).to receive(:to_simple_navigation_item)
.with(item_container)
.and_return(real_item)
end
context 'when the item should be added' do
before { allow(item_container).to receive_messages(should_add_item?: true) }
it 'converts it to an Item and adds it to the items collection' do
item_container.items = items
expect(item_container.items).to include(real_item)
end
end
context 'when the item should not be added' do
before { allow(item_container).to receive_messages(should_add_item?: false) }
it "doesn't add it to the items collection" do
item_container.items = items
expect(item_container.items).not_to include(real_item)
end
end
end
describe '#selected?' do
let(:item_1) { double(:item, selected?: false) }
let(:item_2) { double(:item, selected?: false) }
before do
item_container.instance_variable_set(:@items, [item_1, item_2])
end
context 'when no item is selected' do
it 'returns nil' do
expect(item_container).not_to be_selected
end
end
context 'when an item is selected' do
it 'returns true' do
allow(item_1).to receive_messages(selected?: true)
expect(item_container).to be_selected
end
end
end
describe '#selected_item' do
let(:item_1) { double(:item, selected?: false) }
let(:item_2) { double(:item, selected?: false) }
before(:each) do
allow(SimpleNavigation).to receive_messages(current_navigation_for: :nav)
allow(item_container).to receive_messages(:[] => nil)
item_container.instance_variable_set(:@items, [item_1, item_2])
end
context "when navigation isn't explicitely set" do
context 'and no item is selected' do
it 'returns nil' do
expect(item_container.selected_item).to be_nil
end
end
context 'and an item selected' do
before { allow(item_1).to receive_messages(selected?: true) }
it 'returns the selected item' do
expect(item_container.selected_item).to be item_1
end
end
end
end
describe '#active_item_container_for' do
context "when the desired level is the same as the container's" do
it 'returns the container itself' do
expect(item_container.active_item_container_for(1)).to be item_container
end
end
context "when the desired level is different than the container's" do
context 'and no subnavigation is selected' do
before { allow(item_container).to receive_messages(selected_sub_navigation?: false) }
it 'returns nil' do
expect(item_container.active_item_container_for(2)).to be_nil
end
end
context 'and a subnavigation is selected' do
let(:sub_navigation) { double(:sub_navigation) }
let(:selected_item) { double(:selected_item) }
before do
allow(item_container).to \
receive_messages(selected_sub_navigation?: true, selected_item: selected_item)
allow(selected_item).to receive_messages(sub_navigation: sub_navigation)
end
it 'calls recursively on the sub_navigation' do
expect(sub_navigation).to receive(:active_item_container_for)
.with(2)
item_container.active_item_container_for(2)
end
end
end
end
describe '#active_leaf_container' do
context 'when the current container has a selected subnavigation' do
let(:sub_navigation) { double(:sub_navigation) }
let(:selected_item) { double(:selected_item) }
before do
allow(item_container).to receive_messages(selected_sub_navigation?: true,
selected_item: selected_item)
allow(selected_item).to receive_messages(sub_navigation: sub_navigation)
end
it 'calls recursively on the sub_navigation' do
expect(sub_navigation).to receive(:active_leaf_container)
item_container.active_leaf_container
end
end
context 'when the current container is the leaf already' do
before { allow(item_container).to receive_messages(selected_sub_navigation?: false) }
it 'returns itsself' do
expect(item_container.active_leaf_container).to be item_container
end
end
end
describe '#item' do
let(:options) { Hash.new }
let(:item) { double(:item) }
context 'when a block is given' do
let(:block) { proc{} }
let(:sub_container) { double(:sub_container) }
it 'yields a new ItemContainer' do
allow_any_instance_of(Item).to \
receive_messages(sub_navigation: sub_container)
expect{ |blk|
item_container.item('key', 'name', 'url', options, &blk)
}.to yield_with_args(sub_container)
end
it "creates a new Item with the given params and block" do
allow(Item).to receive(:new)
.with(item_container, 'key', 'name', 'url', options, &block)
.and_return(item)
item_container.item('key', 'name', 'url', options, &block)
expect(item_container.items).to include(item)
end
it 'adds the created item to the list of items' do
item_container.item('key', 'name', 'url', options) {}
expect(item_container.items).not_to include(item)
end
end
context 'when no block is given' do
it 'creates a new Item with the given params and no sub navigation' do
allow(Item).to receive(:new)
.with(item_container, 'key', 'name', 'url', options)
.and_return(item)
item_container.item('key', 'name', 'url', options)
expect(item_container.items).to include(item)
end
it 'adds the created item to the list of items' do
allow(Item).to receive_messages(new: item)
item_container.item('key', 'name', 'url', options) {}
expect(item_container.items).to include(item)
end
end
describe 'Optional url and optional options' do
context 'when item specifed without url or options' do
it_behaves_like 'adding the item to the list' do
let(:args) { ['key', 'name'] }
end
end
context 'when item is specified with only a url' do
it_behaves_like 'adding the item to the list' do
let(:args) { ['key', 'name', 'url'] }
end
end
context 'when item is specified with only options' do
context 'and options do not contain any condition' do
it_behaves_like 'adding the item to the list' do
let(:args) { ['key', 'name', { option: true }] }
end
end
context 'and options contains a negative condition' do
it_behaves_like 'not adding the item to the list' do
let(:args) { ['key', 'name', nil, { if: ->{ false }, option: true }] }
end
end
context 'and options contains a positive condition' do
it_behaves_like 'adding the item to the list' do
let(:args) { ['key', 'name', nil, { if: ->{ true }, option: true }] }
end
end
end
context 'when item is specified with a url and options' do
context 'and options do not contain any condition' do
it_behaves_like 'adding the item to the list' do
let(:args) { ['key', 'name', 'url', { option: true }] }
end
end
context 'and options contains a negative condition' do
it_behaves_like 'not adding the item to the list' do
let(:args) { ['key', 'name', 'url', { if: ->{ false }, option: true }] }
end
end
context 'and options contains a positive condition' do
it_behaves_like 'adding the item to the list' do
let(:args) { ['key', 'name', 'url', { if: ->{ true }, option: true }] }
end
end
end
context 'when a frozen options hash is given' do
let(:options) do
{ html: { id: 'test' } }.freeze
end
it 'does not raise an exception' do
expect{
item_container.item('key', 'name', 'url', options)
}.not_to raise_error
end
end
describe "container options" do
before do
allow(item_container).to receive_messages(should_add_item?: add_item)
item_container.item :key, 'name', 'url', options
end
context 'when the container :id option is specified' do
let(:options) {{ container: { id: 'c_id' } }}
context 'and the item should be added' do
let(:add_item) { true }
it 'changes its dom_id' do
expect(item_container.dom_id).to eq 'c_id'
end
end
context "and the item shouldn't be added" do
let(:add_item) { false }
it "doesn't change its dom_id" do
expect(item_container.dom_id).to be_nil
end
end
end
context 'when the container :class option is specified' do
let(:options) {{ container: { class: 'c_class' } }}
context 'and the item should be added' do
let(:add_item) { true }
it 'changes its dom_class' do
expect(item_container.dom_class).to eq 'c_class'
end
end
context "and the item shouldn't be added" do
let(:add_item) { false }
it "doesn't change its dom_class" do
expect(item_container.dom_class).to be_nil
end
end
end
context 'when the container :attributes option is specified' do
let(:options) {{ container: { attributes: { option: true } } }}
context 'and the item should be added' do
let(:add_item) { true }
it 'changes its dom_attributes' do
expect(item_container.dom_attributes).to eq(option: true)
end
end
context "and the item shouldn't be added" do
let(:add_item) { false }
it "doesn't change its dom_attributes" do
expect(item_container.dom_attributes).to eq({})
end
end
end
context 'when the container :selected_class option is specified' do
let(:options) {{ container: { selected_class: 'sel_class' } }}
context 'and the item should be added' do
let(:add_item) { true }
it 'changes its selected_class' do
expect(item_container.selected_class).to eq 'sel_class'
end
end
context "and the item shouldn't be added" do
let(:add_item) { false }
it "doesn't change its selected_class" do
expect(item_container.selected_class).to be_nil
end
end
end
end
end
describe 'Conditions' do
context 'when an :if option is given' do
let(:options) {{ if: proc{condition} }}
let(:condition) { nil }
context 'and it evals to true' do
let(:condition) { true }
it 'creates a new Item' do
expect(Item).to receive(:new)
item_container.item('key', 'name', 'url', options)
end
end
context 'and it evals to false' do
let(:condition) { false }
it "doesn't create a new Item" do
expect(Item).not_to receive(:new)
item_container.item('key', 'name', 'url', options)
end
end
context 'and it is not a proc or a method' do
it 'raises an error' do
expect{
item_container.item('key', 'name', 'url', { if: 'text' })
}.to raise_error
end
end
end
context 'when an :unless option is given' do
let(:options) {{ unless: proc{condition} }}
let(:condition) { nil }
context 'and it evals to false' do
let(:condition) { false }
it 'creates a new Navigation-Item' do
expect(Item).to receive(:new)
item_container.item('key', 'name', 'url', options)
end
end
context 'and it evals to true' do
let(:condition) { true }
it "doesn't create a new Navigation-Item" do
expect(Item).not_to receive(:new)
item_container.item('key', 'name', 'url', options)
end
end
end
end
end
describe '#[]' do
before do
item_container.item(:first, 'first', 'bla')
item_container.item(:second, 'second', 'bla')
item_container.item(:third, 'third', 'bla')
end
it 'returns the item with the specified navi_key' do
expect(item_container[:second].name).to eq 'second'
end
context 'when no item exists for the specified navi_key' do
it 'returns nil' do
expect(item_container[:invalid]).to be_nil
end
end
end
describe '#render' do
# TODO
let(:renderer_instance) { double(:renderer).as_null_object }
let(:renderer_class) { double(:renderer_class, new: renderer_instance) }
context 'when renderer is specified as an option' do
context 'and is specified as a class' do
it 'instantiates the passed renderer_class with the options' do
expect(renderer_class).to receive(:new)
.with(renderer: renderer_class)
item_container.render(renderer: renderer_class)
end
it 'calls render on the renderer and passes self' do
expect(renderer_instance).to receive(:render).with(item_container)
item_container.render(renderer: renderer_class)
end
end
context 'and is specified as a symbol' do
before do
SimpleNavigation.registered_renderers = {
my_renderer: renderer_class
}
end
it "instantiates the passed renderer_class with the options" do
expect(renderer_class).to receive(:new).with(renderer: :my_renderer)
item_container.render(renderer: :my_renderer)
end
it 'calls render on the renderer and passes self' do
expect(renderer_instance).to receive(:render).with(item_container)
item_container.render(renderer: :my_renderer)
end
end
end
context 'when no renderer is specified' do
let(:options) { Hash.new }
before { allow(item_container).to receive_messages(renderer: renderer_class) }
it "instantiates the container's renderer with the options" do
expect(renderer_class).to receive(:new).with(options)
item_container.render(options)
end
it 'calls render on the renderer and passes self' do
expect(renderer_instance).to receive(:render).with(item_container)
item_container.render(options)
end
end
end
describe '#renderer' do
context 'when no renderer is set explicitly' do
it 'returns globally-configured renderer' do
expect(item_container.renderer).to be Configuration.instance.renderer
end
end
context 'when a renderer is set explicitly' do
let(:renderer) { double(:renderer) }
before { item_container.renderer = renderer }
it 'returns the specified renderer' do
expect(item_container.renderer).to be renderer
end
end
end
describe '#level_for_item' do
before(:each) do
item_container.item(:p1, 'p1', 'p1')
item_container.item(:p2, 'p2', 'p2') do |p2|
p2.item(:s1, 's1', 's1')
p2.item(:s2, 's2', 's2') do |s2|
s2.item(:ss1, 'ss1', 'ss1')
s2.item(:ss2, 'ss2', 'ss2')
end
p2.item(:s3, 's3', 's3')
end
item_container.item(:p3, 'p3', 'p3')
end
shared_examples 'returning the level of an item' do |item, level|
specify{ expect(item_container.level_for_item(item)).to eq level }
end
it_behaves_like 'returning the level of an item', :p1, 1
it_behaves_like 'returning the level of an item', :p3, 1
it_behaves_like 'returning the level of an item', :s1, 2
it_behaves_like 'returning the level of an item', :ss1, 3
it_behaves_like 'returning the level of an item', :x, nil
end
describe '#empty?' do
context 'when there are no items' do
it 'returns true' do
item_container.instance_variable_set(:@items, [])
expect(item_container).to be_empty
end
end
context 'when there are some items' do
it 'returns false' do
item_container.instance_variable_set(:@items, [double(:item)])
expect(item_container).not_to be_empty
end
end
end
end
end
|