fron 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-gemset +1 -0
  3. data/.ruby-version +1 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +35 -0
  6. data/Rakefile +18 -0
  7. data/docs/configuration.md +8 -8
  8. data/lib/fron/version.rb +1 -1
  9. data/opal/fron/core.rb +1 -1
  10. data/opal/fron/core/adapters/{local-storage.rb → local.rb} +7 -6
  11. data/opal/fron/core/application.rb +13 -11
  12. data/opal/fron/core/configuration.rb +3 -4
  13. data/opal/fron/core/controller.rb +2 -2
  14. data/opal/fron/core/eventable.rb +5 -2
  15. data/opal/fron/core/logger.rb +7 -0
  16. data/opal/fron/core/model.rb +10 -8
  17. data/opal/fron/core/router.rb +1 -1
  18. data/opal/fron/dom.rb +1 -0
  19. data/opal/fron/dom/document.rb +4 -0
  20. data/opal/fron/dom/element.rb +22 -6
  21. data/opal/fron/dom/event.rb +71 -68
  22. data/opal/fron/dom/fragment.rb +1 -3
  23. data/opal/fron/dom/modules/classlist.rb +2 -2
  24. data/opal/fron/dom/modules/dimensions.rb +7 -8
  25. data/opal/fron/dom/modules/events.rb +57 -12
  26. data/opal/fron/dom/node.rb +45 -11
  27. data/opal/fron/dom/nodelist.rb +14 -0
  28. data/opal/fron/dom/style.rb +17 -15
  29. data/opal/fron/dom/text.rb +2 -4
  30. data/opal/fron/dom/window.rb +2 -3
  31. data/opal/fron/request/request.rb +17 -14
  32. data/opal/fron/storage/local-storage.rb +29 -13
  33. data/spec/core-ext/hash_spec.rb +18 -0
  34. data/spec/core/adapter/local_spec.rb +65 -0
  35. data/spec/core/adapter/rails_spec.rb +72 -0
  36. data/spec/core/application_spec.rb +35 -0
  37. data/spec/core/component_spec.rb +107 -0
  38. data/spec/core/configuration_spec.rb +20 -0
  39. data/spec/core/controlller_spec.rb +68 -0
  40. data/spec/core/eventable_spec.rb +74 -0
  41. data/spec/core/logger_spec.rb +28 -0
  42. data/spec/core/model_spec.rb +125 -0
  43. data/spec/core/router_spec.rb +127 -0
  44. data/spec/dom/document_spec.rb +41 -0
  45. data/spec/dom/element_spec.rb +164 -0
  46. data/spec/dom/event_spec.rb +121 -0
  47. data/spec/dom/fragment_spec.rb +13 -0
  48. data/spec/dom/modules/classlist_spec.rb +72 -0
  49. data/spec/dom/modules/dimensions_spec.rb +55 -0
  50. data/spec/dom/modules/events_spec.rb +73 -0
  51. data/spec/dom/node_spec.rb +189 -0
  52. data/spec/dom/nodelist_spec.rb +12 -0
  53. data/spec/dom/style_spec.rb +31 -0
  54. data/spec/dom/text_spec.rb +12 -0
  55. data/spec/dom/window_spec.rb +30 -0
  56. data/spec/request/request_spec.rb +71 -0
  57. data/spec/request/response_spec.rb +46 -0
  58. data/spec/storage/local-storage_spec.rb +58 -0
  59. metadata +36 -4
@@ -0,0 +1,127 @@
1
+ require 'fron/core'
2
+
3
+ module Fron
4
+ class Router
5
+ attr_reader :routes
6
+ end
7
+ end
8
+
9
+ class TestRouteController < Fron::Controller
10
+ route "show/:id", :show
11
+ route "*", :test
12
+
13
+ before :empty, [:show]
14
+
15
+ def empty
16
+ end
17
+ def show
18
+ end
19
+ def test
20
+ end
21
+ end
22
+
23
+ describe Fron::Router do
24
+
25
+ subject { described_class.new [], config }
26
+ let(:window) { DOM::Window }
27
+ let(:window_listeners) {window.instance_variable_get("@listeners")}
28
+ let(:controller) { TestRouteController.new }
29
+ let(:config) { double :config, {
30
+ logger: double(:logger,info: true),
31
+ main: double(:main, :"<<" => true, "empty" => true)
32
+ }
33
+ }
34
+
35
+ before { window.off }
36
+
37
+ describe "DSL" do
38
+ describe "#map" do
39
+ it "should retrun '*' path for action only" do
40
+ result = described_class.map :test
41
+ result[:path].should eq "*"
42
+ result[:action].should eq :test
43
+ end
44
+
45
+ it "should return regexp for path" do
46
+ result = described_class.map "/test", :test
47
+ result[:path][:regexp].should eq Regexp.new "^/test"
48
+ result[:path][:map].should eq nil
49
+ result[:action].should eq :test
50
+ end
51
+
52
+ it 'should parse string as action' do
53
+ result = described_class.map :test
54
+ result[:action].should eq :test
55
+ result[:controller].should be nil
56
+ end
57
+
58
+ it 'should parse controllers as controller' do
59
+ result = described_class.map Fron::Controller
60
+ result[:controller].should_not be nil
61
+ result[:controller].class.should eq Fron::Controller
62
+ result[:action].should eq nil
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#route" do
68
+ it "should deletage if controller is given" do
69
+ subject.routes << {path: "*", controller: controller}
70
+ subject.should receive(:route).twice.and_call_original
71
+ subject.route
72
+ end
73
+
74
+ it "should call controller action" do
75
+ subject.routes << {path: "*", controller: controller}
76
+ controller.should receive(:test).once
77
+ subject.route
78
+ end
79
+
80
+ it "should call controller action with params" do
81
+ controller.should receive(:show).once.with({id: '10'})
82
+ subject.routes << {path: "*", controller: controller}
83
+ subject.route 'show/10'
84
+ end
85
+
86
+ it "should remove the matched portion of the hash" do
87
+ controller.should receive(:show).once.with({id: '10'})
88
+ subject.routes << {path: {regexp: Regexp.new("test/")}, controller: controller}
89
+ subject.route 'test/show/10'
90
+ end
91
+ end
92
+
93
+ describe "#applyRoute" do
94
+ it "should call before methods" do
95
+ controller.should receive(:empty).once
96
+ subject.applyRoute controller, {action: 'show'}
97
+ end
98
+
99
+ it "should call action method" do
100
+ controller.should receive(:show).once
101
+ subject.applyRoute controller, {action: 'show'}
102
+ end
103
+
104
+ it 'should log the route' do
105
+ config.logger.should receive(:info).once
106
+ subject.applyRoute controller, {action: 'show'}
107
+ end
108
+
109
+ it "should append it the the main" do
110
+ config.main.should receive(:<<).once
111
+ subject.applyRoute controller, {action: 'show'}
112
+ end
113
+ end
114
+
115
+ describe "#initalize" do
116
+ it 'should listen on window load' do
117
+ subject
118
+ window_listeners['load'].should_not be nil
119
+ end
120
+
121
+ it 'should listen on window hashchange' do
122
+ subject
123
+ window_listeners['hashchange'].should_not be nil
124
+ end
125
+ end
126
+
127
+ end
@@ -0,0 +1,41 @@
1
+ require 'fron/dom'
2
+
3
+ describe DOM::Document do
4
+
5
+ subject { DOM::Document }
6
+
7
+ describe "head" do
8
+ it 'should return the head element' do
9
+ DOM::Element.new(`document.head`).should eq subject.head
10
+ end
11
+ end
12
+
13
+ describe "body" do
14
+ it 'should return the body element' do
15
+ DOM::Element.new(`document.body`).should eq subject.body
16
+ end
17
+ end
18
+
19
+ describe 'title' do
20
+ it 'should return the title of the document' do
21
+ subject.title.should eq "Opal Server"
22
+ end
23
+ end
24
+
25
+ describe 'title=' do
26
+ it 'should set the title of the document' do
27
+ subject.title = 'test'
28
+ `document.title`.should eq 'test'
29
+ end
30
+ end
31
+
32
+ describe 'find' do
33
+ it 'should return nil if no element is found' do
34
+ subject.find('list').should eq nil
35
+ end
36
+
37
+ it 'should return element' do
38
+ subject.find('body').class.should eq DOM::Element
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,164 @@
1
+ require 'fron/dom'
2
+
3
+ describe DOM::Element do
4
+
5
+ subject { described_class.new 'div' }
6
+ let(:el) { subject.instance_variable_get("@el") }
7
+ let(:a) { described_class.new 'a' }
8
+
9
+ describe "#initailize" do
10
+ context "string argument" do
11
+ it 'should create an element with tagname' do
12
+ described_class.new('div').tag.should eq 'div'
13
+ end
14
+
15
+ it 'should add classes' do
16
+ el = described_class.new('div.test.help')
17
+ el.hasClass('test').should be true
18
+ el.hasClass('help').should be true
19
+ end
20
+
21
+ it 'should set the id' do
22
+ el = described_class.new('div#test')
23
+ el.id.should eq 'test'
24
+ end
25
+
26
+ it 'should set attributes' do
27
+ el = described_class.new('div[href=test][link=rel]')
28
+ el['href'].should eq 'test'
29
+ el['link'].should eq 'rel'
30
+ end
31
+ end
32
+
33
+ context "node argument" do
34
+ it 'should link given node' do
35
+ el = described_class.new `document.body`
36
+ el.tag.should eq 'body'
37
+ end
38
+ end
39
+
40
+ context "other argument" do
41
+ it "should throw error" do
42
+ expect(Proc.new { described_class.new({}) } ).to raise_error
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#matches' do
48
+ it 'should return if matches selector' do
49
+ subject.matches('div').should be true
50
+ end
51
+
52
+ it 'should return flase unless matches selector' do
53
+ subject.matches('body').should be false
54
+ end
55
+ end
56
+
57
+ describe '#hide' do
58
+ it 'should hide the element' do
59
+ subject.hide
60
+ subject.style.display.should eq 'none'
61
+ end
62
+ end
63
+
64
+ describe '#show' do
65
+ it 'should show the element' do
66
+ subject.show
67
+ subject.style.display.should eq 'block'
68
+ end
69
+ end
70
+
71
+ describe '#[]' do
72
+ it 'should return the given attribute value' do
73
+ `#{el}.setAttribute('test','data')`
74
+ subject['test'].should eq 'data'
75
+ end
76
+ end
77
+
78
+ describe '#[]=' do
79
+ it 'should set the given attribute with the given value' do
80
+ subject['test'] = 'data'
81
+ `#{el}.getAttribute('test')`.should eq 'data'
82
+ end
83
+ end
84
+
85
+ describe '#find' do
86
+ it 'should find a descendant element' do
87
+ a >> subject
88
+ subject.find('a').should eq a
89
+ end
90
+
91
+ it 'should return nil if no element is found' do
92
+ subject.find('p').should eq nil
93
+ end
94
+ end
95
+
96
+ describe "#html" do
97
+ it 'should get the html of the element' do
98
+ `#{el}.innerHTML = 'test'`
99
+ subject.html.should eq 'test'
100
+ end
101
+ end
102
+
103
+ describe '#html=' do
104
+ it 'should set the html of the element' do
105
+ subject.html = 'test'
106
+ `#{el}.innerHTML`.should eq 'test'
107
+ end
108
+ end
109
+
110
+ describe '#empty' do
111
+ it 'should empty the element' do
112
+ subject << a
113
+ subject.empty
114
+ subject.children.length.should eq 0
115
+ subject.html.should eq ""
116
+ end
117
+ end
118
+
119
+ describe "#value" do
120
+ it 'should get the value of the element' do
121
+ `#{el}.value = 'test'`
122
+ subject.value.should eq 'test'
123
+ end
124
+ end
125
+
126
+ describe '#value=' do
127
+ it 'should set the value of the element' do
128
+ subject.value = 'test'
129
+ `#{el}.value`.should eq 'test'
130
+ end
131
+ end
132
+
133
+ describe '#checked' do
134
+ it 'should return false unless the element is checked' do
135
+ subject.checked.should be false
136
+ end
137
+
138
+ it 'should return true if the element is checked' do
139
+ `#{el}.checked = true`
140
+ subject.checked.should be true
141
+ end
142
+ end
143
+
144
+ describe '#checked=' do
145
+ it 'should set the checked property' do
146
+ subject.checked = true
147
+ `#{el}.checked == true`
148
+ end
149
+ end
150
+
151
+ describe '#tag' do
152
+ it 'should return the elements tagName' do
153
+ subject.tag.should eq 'div'
154
+ a.tag.should eq 'a'
155
+ end
156
+ end
157
+
158
+ describe '#id' do
159
+ it 'should return the id of the element' do
160
+ `#{el}.id = 'test'`
161
+ subject.id.should eq 'test'
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,121 @@
1
+ describe DOM::Event do
2
+
3
+ subject { described_class.new data, Hash }
4
+
5
+ let(:data) {%x{
6
+ return { charCode: 1,
7
+ keyCode: 2,
8
+ pageX: 3,
9
+ pageY: 4,
10
+ screenX: 5,
11
+ screenY: 6,
12
+ clientX: 7,
13
+ clientY: 8,
14
+ altKey: true,
15
+ shiftKey: false,
16
+ ctrlKey: true,
17
+ metaKey: false,
18
+ preventDefault: function(){return 1},
19
+ stopPropagation: function(){return 2},
20
+ target: {test: 'data'} }
21
+ }}
22
+
23
+ describe '#stop' do
24
+ it "should call stopPropagation and preventDefault" do
25
+ expect(subject).to receive(:stopPropagation).once
26
+ expect(subject).to receive(:preventDefault).once
27
+ subject.stop
28
+ end
29
+ end
30
+
31
+ describe "#stopPropagation" do
32
+ it 'should call stopPropagation of the event' do
33
+ subject.stopPropagation.should eq 2
34
+ end
35
+ end
36
+
37
+ describe "#preventDefault" do
38
+ it 'should call preventDefault of the event' do
39
+ subject.preventDefault.should eq 1
40
+ end
41
+ end
42
+
43
+ describe "#target" do
44
+ it 'should return the target' do
45
+ subject.target['test'].should eq 'data'
46
+ subject.target.class.should eq Hash
47
+ end
48
+ end
49
+
50
+ describe "#charCode" do
51
+ it 'should return the charCode' do
52
+ subject.charCode.should eq 1
53
+ end
54
+ end
55
+
56
+ describe "#keyCode" do
57
+ it 'should return the keyCode' do
58
+ subject.keyCode.should eq 2
59
+ end
60
+ end
61
+
62
+ describe "#pageX" do
63
+ it 'should return the pageX' do
64
+ subject.pageX.should eq 3
65
+ end
66
+ end
67
+
68
+ describe "#pageY" do
69
+ it 'should return the pageY' do
70
+ subject.pageY.should eq 4
71
+ end
72
+ end
73
+
74
+ describe "#screenX" do
75
+ it 'should return the screenX' do
76
+ subject.screenX.should eq 5
77
+ end
78
+ end
79
+
80
+ describe "#screenY" do
81
+ it 'should return the screenY' do
82
+ subject.screenY.should eq 6
83
+ end
84
+ end
85
+
86
+ describe "#clientX" do
87
+ it 'should return the clientX' do
88
+ subject.clientX.should eq 7
89
+ end
90
+ end
91
+
92
+ describe "#clientY" do
93
+ it 'should return the clientY' do
94
+ subject.clientY.should eq 8
95
+ end
96
+ end
97
+
98
+ describe "#alt?" do
99
+ it 'should return the alt?' do
100
+ subject.alt?.should eq true
101
+ end
102
+ end
103
+
104
+ describe "#shift?" do
105
+ it 'should return the shift?' do
106
+ subject.shift?.should eq false
107
+ end
108
+ end
109
+
110
+ describe "#ctrl?" do
111
+ it 'should return the ctrl?' do
112
+ subject.ctrl?.should eq true
113
+ end
114
+ end
115
+
116
+ describe "#meta?" do
117
+ it 'should return the meta?' do
118
+ subject.meta?.should eq false
119
+ end
120
+ end
121
+ end