|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 | 3 | describe Vienna::Router do
|
4 |
| - let(:router) { Vienna::Router.new } |
5 |
| - |
6 | 4 | describe "#update" do
|
7 | 5 | it "should update Router.path" do
|
8 | 6 | $global.location.hash = "#/hello/world"
|
9 |
| - router.update |
| 7 | + subject.update |
10 | 8 |
|
11 |
| - router.path.should eq('/hello/world') |
| 9 | + subject.path.should eq('/hello/world') |
12 | 10 | end
|
13 | 11 |
|
14 | 12 | it "calls #match with the new @path" do
|
15 | 13 | $global.location.hash = "#/foo/bar"
|
16 | 14 | called = nil
|
17 | 15 |
|
18 |
| - router.define_singleton_method(:match) { |m| called = m } |
| 16 | + subject.define_singleton_method(:match) { |m| called = m } |
19 | 17 | called.should be_nil
|
20 | 18 |
|
21 |
| - router.update |
| 19 | + subject.update |
22 | 20 | called.should eq("/foo/bar")
|
23 | 21 | end
|
24 | 22 | end
|
25 | 23 |
|
26 | 24 | describe "#route" do
|
27 | 25 | it "should add a route" do
|
28 |
| - router.route('/users') {} |
29 |
| - router.routes.size.should eq(1) |
| 26 | + subject.route('/users') {} |
| 27 | + subject.routes.size.should eq(1) |
30 | 28 |
|
31 |
| - router.route('/hosts') {} |
32 |
| - router.routes.size.should eq(2) |
| 29 | + subject.route('/hosts') {} |
| 30 | + subject.routes.size.should eq(2) |
33 | 31 | end
|
34 | 32 | end
|
35 | 33 |
|
36 | 34 | describe "#match" do
|
37 | 35 | it "returns nil when no routes on router" do
|
38 |
| - router.match('/foo').should be_nil |
| 36 | + subject.match('/foo').should be_nil |
39 | 37 | end
|
40 | 38 |
|
41 | 39 | it "returns a matching route for the path" do
|
42 |
| - a = router.route('/foo') {} |
43 |
| - b = router.route('/bar') {} |
| 40 | + a = subject.route('/foo') {} |
| 41 | + b = subject.route('/bar') {} |
44 | 42 |
|
45 |
| - router.match('/foo').should eq(a) |
46 |
| - router.match('/bar').should eq(b) |
| 43 | + subject.match('/foo').should eq(a) |
| 44 | + subject.match('/bar').should eq(b) |
47 | 45 | end
|
48 | 46 |
|
49 | 47 | it "returns nil when there are no matching routes" do
|
50 |
| - router.route('/woosh') {} |
51 |
| - router.route('/kapow') {} |
| 48 | + subject.route('/woosh') {} |
| 49 | + subject.route('/kapow') {} |
52 | 50 |
|
53 |
| - router.match('/ping').should be_nil |
| 51 | + subject.match('/ping').should be_nil |
54 | 52 | end
|
55 | 53 |
|
56 | 54 | it "calls handler of matching route" do
|
57 | 55 | out = []
|
58 |
| - router.route('/foo') { out << :foo } |
59 |
| - router.route('/bar') { out << :bar } |
| 56 | + subject.route('/foo') { out << :foo } |
| 57 | + subject.route('/bar') { out << :bar } |
60 | 58 |
|
61 |
| - router.match('/foo') |
| 59 | + subject.match('/foo') |
62 | 60 | out.should eq([:foo])
|
63 | 61 |
|
64 |
| - router.match('/bar') |
| 62 | + subject.match('/bar') |
65 | 63 | out.should eq([:foo, :bar])
|
66 | 64 |
|
67 |
| - router.match('/eek') |
| 65 | + subject.match('/eek') |
68 | 66 | out.should eq([:foo, :bar])
|
69 | 67 | end
|
70 | 68 |
|
71 | 69 | it "works with / too" do
|
72 | 70 | out = []
|
73 |
| - router.route('/') { out << :index } |
| 71 | + subject.route('/') { out << :index } |
74 | 72 |
|
75 | 73 | $global.location.hash = ""
|
76 |
| - router.update |
| 74 | + subject.update |
77 | 75 |
|
78 | 76 | $global.location.hash = "#/"
|
79 |
| - router.update |
| 77 | + subject.update |
80 | 78 |
|
81 | 79 | out.should == [:index, :index]
|
82 | 80 | end
|
83 | 81 | end
|
84 | 82 |
|
85 | 83 | describe "#navigate" do
|
86 | 84 | it "should update location.hash" do
|
87 |
| - router.navigate "foo" |
| 85 | + subject.navigate "foo" |
88 | 86 | $global.location.hash.should eq("#foo")
|
89 | 87 | end
|
90 | 88 |
|
91 | 89 | it "triggers the route matchers" do
|
92 | 90 | called = false
|
93 |
| - router.route("/foo") { called = true } |
| 91 | + subject.route("/foo") { called = true } |
94 | 92 |
|
95 |
| - router.navigate("/bar") |
96 |
| - router.update |
97 |
| - called.should be_false |
| 93 | + subject.navigate("/bar") |
| 94 | + subject.update |
| 95 | + called.should eq(false) |
98 | 96 |
|
99 |
| - router.navigate("/foo") |
100 |
| - router.update |
101 |
| - called.should be_true |
| 97 | + subject.navigate("/foo") |
| 98 | + subject.update |
| 99 | + called.should eq(true) |
102 | 100 | end
|
103 | 101 | end
|
104 | 102 | end
|
|
0 commit comments