opal-jquery 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ describe "Element#before" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="before-spec">
5
+ <div id="some-header" class="kapow"></div>
6
+ <div id="foo" class="before-spec-first"></div>
7
+ <div id="bar" class="before-spec-first"></div>
8
+ <div id="baz"></div>
9
+ </div>
10
+ HTML
11
+
12
+ @div.append_to_body
13
+ end
14
+
15
+ after do
16
+ @div.remove
17
+ end
18
+
19
+ it "should insert the given html string before each element" do
20
+ el = Document['.before-spec-first']
21
+ el.size.should == 2
22
+
23
+ el.before '<p class="woosh"></p>'
24
+
25
+ Document['#foo'].prev.class_name.should == "woosh"
26
+ Document['#bar'].prev.class_name.should == "woosh"
27
+ end
28
+
29
+ it "should insert the given DOM element before this element" do
30
+ Document['#baz'].before Document['#some-header']
31
+ Document['#baz'].prev.id.should == "some-header"
32
+ end
33
+ end
@@ -0,0 +1,82 @@
1
+ describe "Element#class_name" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="class-name-spec">
5
+ <div id="foo" class="whiskey"></div>
6
+ <div id="bar" class="scotch brandy"></div>
7
+ <div id="baz" class=""></div>
8
+ <div id="buz"></div>
9
+
10
+ <div class="red dark"></div>
11
+ <div class="red light"></div>
12
+ </div>
13
+ HTML
14
+
15
+ @div.append_to_body
16
+ end
17
+
18
+ after do
19
+ @div.remove
20
+ end
21
+
22
+ it "should return the elements' class name" do
23
+ Document.id('foo').class_name.should == "whiskey"
24
+ Document.id('bar').class_name.should == "scotch brandy"
25
+ end
26
+
27
+ it "should return an empty string for element with no class name" do
28
+ Document.id('baz').class_name.should == ""
29
+ Document.id('buz').class_name.should == ""
30
+ end
31
+
32
+ it "should return class name for first element if more than 1 in set" do
33
+ Document.find('.red').class_name.should == "red dark"
34
+ end
35
+
36
+ it "should return an empty string for instances with no elements" do
37
+ Document.find('.no-elements').class_name.should == ""
38
+ end
39
+ end
40
+
41
+ describe "Element#class_name=" do
42
+ before do
43
+ @div = Document.parse <<-HTML
44
+ <div id="class-name-set-spec">
45
+ <div id="foo" class=""></div>
46
+ <div id="bar" class="oranges"></div>
47
+
48
+ <div id="baz" class="banana"></div>
49
+ <div id="buz" class="banana"></div>
50
+ </div>
51
+ HTML
52
+
53
+ @div.append_to_body
54
+ end
55
+
56
+ after do
57
+ @div.remove
58
+ end
59
+
60
+ it "should set the given class name on the element" do
61
+ Document.id('foo').class_name = "apples"
62
+ Document.id('foo').class_name.should == "apples"
63
+ end
64
+
65
+ it "should replace any existing class name" do
66
+ bar = Document.id('bar')
67
+ bar.class_name.should == "oranges"
68
+
69
+ bar.class_name = "lemons"
70
+ bar.class_name.should == "lemons"
71
+ end
72
+
73
+ it "should set the class name on all elements in instance" do
74
+ el = Document.find '.banana'
75
+ el.length.should == 2
76
+
77
+ el.class_name = "pop"
78
+
79
+ Document.id('baz').class_name.should == "pop"
80
+ Document.id('buz').class_name.should == "pop"
81
+ end
82
+ end
@@ -0,0 +1,52 @@
1
+ describe "Element#css" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="css-spec">
5
+ <div id="foo" style="background-color:rgb(15,99,30); color:;"></div>
6
+ <div id="bar"></div>
7
+ <div id="hash"></div>
8
+ </div>
9
+ HTML
10
+
11
+ @div.append_to_body
12
+ end
13
+
14
+ after do
15
+ @div.remove
16
+ end
17
+
18
+ describe "with a given name" do
19
+ it "returns the value of the CSS property for the given name" do
20
+ Document.id('foo').css('backgroundColor').should be_kind_of(String)
21
+ end
22
+
23
+ it "should return an empty string when no style property is defined for name" do
24
+ Document.id('foo').css('color').should be_kind_of(String)
25
+ end
26
+ end
27
+
28
+ describe "with a name and value" do
29
+ it "should set the CSS property to the given value" do
30
+ Document.id('bar').css('backgroundColor', 'blue')
31
+ end
32
+
33
+ it "returns self" do
34
+ bar = Document.id('bar')
35
+ bar.css("background", "green").should equal(bar)
36
+ end
37
+ end
38
+
39
+ describe "with a set of names and values" do
40
+ it "should set the properties" do
41
+ hash = Document.id("hash")
42
+ hash.css(:width => "100px", :height => "200px")
43
+ hash.css("width").should be_kind_of(String)
44
+ hash.css("height").should be_kind_of(String)
45
+ end
46
+
47
+ it "should return self" do
48
+ hash = Document.id("hash")
49
+ hash.css(:border => "1px solid #000").should equal(hash)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,45 @@
1
+ describe "Element display methods" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="css-spec">
5
+ <div id="shown"></div>
6
+ <div id="hidden" style="display: none"></div>
7
+ </div>
8
+ HTML
9
+
10
+ @div.append_to_body
11
+ end
12
+
13
+ after do
14
+ @div.remove
15
+ end
16
+
17
+ it "hides an element" do
18
+ element = Document.id('shown')
19
+ element.css('display').should == 'block'
20
+ element.hide
21
+ element.css('display').should == 'none'
22
+ end
23
+
24
+ it "shows an element" do
25
+ element = Document.id('hidden')
26
+ element.css('display').should == 'none'
27
+ element.show
28
+ element.css('display').should == 'block'
29
+ end
30
+
31
+ it "toggles on a hidden element" do
32
+ element = Document.id('hidden')
33
+ element.css('display').should == 'none'
34
+ element.toggle
35
+ element.css('display').should == 'block'
36
+ end
37
+
38
+ it "toggles off a displayed element" do
39
+ element = Document.id('shown')
40
+ element.css('display').should == 'block'
41
+ element.toggle
42
+ element.css('display').should == 'none'
43
+ end
44
+
45
+ end
@@ -0,0 +1,26 @@
1
+ describe "Element#inspect" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="insert-spec">
5
+ <div id="foo"></div>
6
+ <div class="bar"></div>
7
+ <p id="lol" class="bar"></div>
8
+ </div>
9
+ HTML
10
+
11
+ @div.append_to_body
12
+ end
13
+
14
+ after do
15
+ @div.remove
16
+ end
17
+
18
+ it "should return a string representation of the elements" do
19
+ Document.id('foo').inspect.should == '[<div id="foo">]'
20
+ Document.find('.bar').inspect.should == '[<div class="bar">, <p id="lol" class="bar">]'
21
+ end
22
+
23
+ it "should return '[]' when called on empty element set" do
24
+ Document['.inspect-spec-none'].inspect.should == '[]'
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ describe "Element#length" do
2
+ it "should report the number of elements in the instance" do
3
+ Element.new.length.should == 1
4
+ end
5
+ end
@@ -0,0 +1,150 @@
1
+ describe Element do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="traversing-spec">
5
+ <div id="foo" class="traversing-class"></div>
6
+ <div id="bar" class="traversing-class">
7
+ <p>Hey</p>
8
+ <p>There</p>
9
+ </div>
10
+ <div id="baz" class="main-content-wrapper">
11
+ <div><p></p></div>
12
+ </div>
13
+ </div>
14
+ HTML
15
+
16
+ @div.append_to_body
17
+ end
18
+
19
+ after do
20
+ @div.remove
21
+ end
22
+
23
+ describe '#children' do
24
+ it "should return a new collection of all direct children of element" do
25
+ Document.id('foo').children.size.should == 0
26
+ Document.id('bar').children.size.should == 2
27
+ end
28
+
29
+ it "should only return direct children" do
30
+ c = Document.id('baz').children
31
+ c.size.should == 1
32
+ end
33
+ end
34
+
35
+ describe '#each' do
36
+ it "should loop over each element passing element to block" do
37
+ result = []
38
+ Document.find('.traversing-class').each do |e|
39
+ result << e.id
40
+ end
41
+
42
+ result.should == ['foo', 'bar']
43
+ end
44
+
45
+ it "should not call the block with an empty element set" do
46
+ Document.find('.bad-each-class').each do
47
+ raise "shouldn't get here"
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#find' do
53
+ it "should match all elements within scope of receiver" do
54
+ foo = Document['#traversing-spec']
55
+ foo.find('.traversing-class').size.should == 2
56
+ foo.find('.main-content-wrapper').size.should == 1
57
+ end
58
+
59
+ it "should return an empty collection if there are no matching elements" do
60
+ bar = Document['#bar']
61
+ bar.find('.some-non-existant-class').size.should == 0
62
+ end
63
+ end
64
+
65
+ describe '#first' do
66
+ it "should return the first element in the receiver" do
67
+ Document.find('.traversing-class').first.id.should == 'foo'
68
+ Document.id('baz').first.id.should == 'baz'
69
+ end
70
+
71
+ it "should return nil when receiver has no elements" do
72
+ Document.find('.some-random-class').first.should == nil
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "Element#next" do
78
+ before do
79
+ @div = Document.parse <<-HTML
80
+ <div id="next-spec">
81
+ <div id="foo"></div>
82
+ <div id="bar"></div>
83
+ </div>
84
+ HTML
85
+
86
+ @div.append_to_body
87
+ end
88
+
89
+ after do
90
+ @div.remove
91
+ end
92
+
93
+ it "should return the next sibling" do
94
+ Document.id('foo').next.id.should == "bar"
95
+ end
96
+
97
+ it "should return an empty instance when no next element" do
98
+ Document.id('bar').next.size.should == 0
99
+ end
100
+ end
101
+
102
+ describe "Element#parent" do
103
+ before do
104
+ @div = Document.parse <<-HTML
105
+ <div id="foo">
106
+ <div id="bar">
107
+ <div id="baz"></div>
108
+ <div id="buz"></div>
109
+ </div>
110
+ </div>
111
+ HTML
112
+
113
+ @div.append_to_body
114
+ end
115
+
116
+ after do
117
+ @div.remove
118
+ end
119
+
120
+ it "should return the parent of the element" do
121
+ Document.id('bar').parent.id.should == "foo"
122
+ Document.id('baz').parent.id.should == "bar"
123
+ Document.id('buz').parent.id.should == "bar"
124
+ end
125
+ end
126
+
127
+ describe "Element#succ" do
128
+ before do
129
+ @div = Document.parse <<-HTML
130
+ <div id="succ-spec">
131
+ <div id="foo"></div>
132
+ <div id="bar"></div>
133
+ </div>
134
+ HTML
135
+
136
+ @div.append_to_body
137
+ end
138
+
139
+ after do
140
+ @div.remove
141
+ end
142
+
143
+ it "should return the next sibling" do
144
+ Document.id('foo').succ.id.should == "bar"
145
+ end
146
+
147
+ it "should return an empty instance when no next element" do
148
+ Document.id('bar').succ.size.should == 0
149
+ end
150
+ end
@@ -0,0 +1,107 @@
1
+ describe Element do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="on-spec">
5
+ <div id="foo">
6
+ <div id="bar" class="apples"></div>
7
+ </div>
8
+ <div id="baz"></div>
9
+ </div>
10
+ HTML
11
+
12
+ @div.append_to_body
13
+ end
14
+
15
+ after do
16
+ @div.remove
17
+ end
18
+
19
+ describe '#on' do
20
+ it 'adds an event listener onto the elements' do
21
+ count = 0
22
+ foo = Document['#foo']
23
+
24
+ foo.on(:click) { count += 1 }
25
+ count.should == 0
26
+ foo.trigger(:click)
27
+ count.should == 1
28
+ foo.trigger(:click)
29
+ count.should == 2
30
+ foo.trigger(:mousedown)
31
+ count.should == 2
32
+ end
33
+
34
+ it 'takes an optional second parameter to delegate events' do
35
+ count = 0
36
+ foo = Document['#foo']
37
+ bar = Document['#bar']
38
+
39
+ foo.on(:click, '#bar') { count += 1 }
40
+ count.should == 0
41
+ foo.trigger(:click)
42
+ count.should == 0
43
+ bar.trigger(:click)
44
+ count.should == 1
45
+ bar.trigger(:click)
46
+ count.should == 2
47
+ bar.trigger(:mousedown)
48
+ count.should == 2
49
+ end
50
+
51
+ it 'can listen for non-browser events' do
52
+ count = 0
53
+ foo = Document['#foo']
54
+
55
+ foo.on('opal-is-mega-lolz') { count += 1 }
56
+ count.should == 0
57
+ foo.trigger('opal-is-mega-lolz')
58
+ count.should == 1
59
+ foo.trigger('opal-is-mega-lolz')
60
+ count.should == 2
61
+ end
62
+
63
+ it 'returns the given handler' do
64
+ handler = proc {}
65
+ Document['#foo'].on(:click, &handler).should == handler
66
+ end
67
+
68
+ it 'has an Event instance passed to the handler' do
69
+ foo = Document['#foo']
70
+ foo.on :click do |evt|
71
+ evt.should be_kind_of(Event)
72
+ end
73
+ foo.trigger(:click)
74
+ end
75
+ end
76
+
77
+ describe '#off' do
78
+ it 'removes event handlers that were added using #on' do
79
+ count = 0
80
+ foo = Document['#foo']
81
+
82
+ handler = foo.on(:click) { count += 1 }
83
+ count.should == 0
84
+ foo.trigger(:click)
85
+ count.should == 1
86
+ foo.off(:click, handler)
87
+ count.should == 1
88
+ foo.trigger(:click)
89
+ count.should == 1
90
+ end
91
+
92
+ it 'removes event handlers added with a selector' do
93
+ count = 0
94
+ foo = Document['#foo']
95
+ bar = Document['#bar']
96
+
97
+ handler = foo.on(:click, '#bar') { count += 1 }
98
+ count.should == 0
99
+ bar.trigger(:click)
100
+ count.should == 1
101
+ foo.off(:click, '#bar', handler)
102
+ count.should == 1
103
+ bar.trigger(:click)
104
+ count.should == 1
105
+ end
106
+ end
107
+ end