require 'test/unit' require File.dirname(__FILE__) + '/../lib/navigator' class NavigatorTest < Test::Unit::TestCase def test_draw RHNH::Magellan::Navigator.draw do |map| map.add_page :home end nav = RHNH::Magellan::Navigator.instance assert_equal 1, nav.pages.length # Each call should reset the nav map RHNH::Magellan::Navigator.draw do |map| map.add_page :home2 map.add_page :home3 end assert_equal 1, nav.pages.length # Can still access old one assert_equal 2, RHNH::Magellan::Navigator.instance.pages.length end def test_add_page home_url = { :controller => 'welcome' } page1_url = { :controller => 'main', :action => 'page1' } page2_url = { :controller => 'main', :action => 'page2' } RHNH::Magellan::Navigator.draw do |map| map.add_page :home, home_url map.add_page 'page1', page1_url # 'page1' gets converted to symbol # Block initialization map.add_page :page2 do |p| p.url = page2_url end end nav = RHNH::Magellan::Navigator.instance assert_equal home_url, nav.pages[:home].url assert_equal page1_url, nav.pages[:page1].url assert_equal page2_url, nav.pages[:page2].url end def test_add_link logic = lambda { |pages,state| :page1 } RHNH::Magellan::Navigator.draw do |map| map.add_link :back, :home map.add_link 'back2', :home map.add_link :evalled, logic map.add_page :home do |p| p.add_link :back, :elsewhere p.add_link 'back2', :elsewhere p.add_link :evalled, logic end end nav = RHNH::Magellan::Navigator.instance assert_equal :home, nav.links[:back] assert_equal :home, nav.links[:back2] assert_equal logic, nav.links[:evalled] assert_equal :elsewhere, nav.pages[:home][:back] assert_equal :elsewhere, nav.pages[:home][:back2] assert_equal logic, nav.pages[:home][:evalled] end def test_get_url home_url = { :controller => 'welcome' } noback_url = { :controller => 'noback' } page1_url = { :controller => 'main', :action => 'page1' } page2_url = { :controller => 'main', :action => 'page2' } RHNH::Magellan::Navigator.draw do |map| map.add_link :back, :noback map.add_page :home, home_url map.add_page :noback, noback_url map.add_page :page1 do |p| p.url = page1_url p.add_link :back, :home end map.add_page :page2 do |p| p.url = page2_url p.add_link :back, lambda { |pages,state| pages[:page1] } end nav = RHNH::Magellan::Navigator.instance assert_equal home_url, nav.get_url(:page1, :back) # Normal assert_equal noback_url, nav.get_url(:home, :back) # Default link assert_equal page1_url, nav.get_url(:page2, :back) # Evalled link assert_equal home_url, nav.get_url(:home) # Page as link end end def test_functional_state home_url = { :controller => 'welcome' } page1_url = { :controller => 'main', :action => 'page1' } page2_url = { :controller => 'main', :action => 'page2' } RHNH::Magellan::Navigator.draw do |map| map.add_page :home, home_url map.add_page :page1, page1_url map.add_page :page2 do |p| p.add_link :back, lambda { |pages,state| if state[:testkey] pages[:page1] else pages[:home] end } end end nav = RHNH::Magellan::Navigator.instance state = nil assert_equal home_url, nav.get_url(:page2, :back, state) state = { :testkey => true } assert_equal page1_url, nav.get_url(:page2, :back, state) state = { :testkey => false } assert_equal home_url, nav.get_url(:page2, :back, state) end def test_functional_with_params home_url = { :controller => 'welcome'} home2_url = { :controller => 'welcome', :params => {:p => 'Hello'}} page1_url = { :controller => 'main', :action => 'page1' } RHNH::Magellan::Navigator.draw do |map| map.add_page :home, home_url map.add_page :home2, home2_url map.add_page :page1 do |p| p.add_link :back, lambda { |pages,state| [pages[:home], {:msg => 'success'}] } p.add_link :back2, lambda { |pages,state| [pages[:home2], {:msg => 'success'}] } end end nav = RHNH::Magellan::Navigator.instance b1_expected = home_url.merge(:params => {:msg => 'success'}) b2_expected = home2_url b2_expected[:params][:msg] = 'success' assert_equal b1_expected, nav.get_url(:page1, :back, nil) # Normal assert_equal b2_expected, nav.get_url(:page1, :back2, nil) # Merging parameters end end