Skip to content

Commit c7d0aaa

Browse files
committed
added some basic tests, refactorings, version 0.5
1 parent 267e111 commit c7d0aaa

File tree

10 files changed

+393
-89
lines changed

10 files changed

+393
-89
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.config
55
.yardoc
66
.idea
7+
.sass-cache
78
Gemfile.lock
89
InstalledFiles
910
_yardoc
@@ -16,3 +17,6 @@ spec/reports
1617
test/tmp
1718
test/version_tmp
1819
tmp
20+
spec/ajaxify_test/log
21+
spec/ajaxify_test/tmp
22+
.DS_Store

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format progress

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
rvm:
2+
- 1.9.3
3+
before_script:
4+
- "export DISPLAY=:99.0"
5+
- "sh -e /etc/init.d/xvfb start"

Rakefile

+13
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
#!/usr/bin/env rake
22
require "bundler/gem_tasks"
3+
# require "rake/testtask"
4+
5+
# Rake::TestTask.new(:test) do |t|
6+
# t.libs << 'test'
7+
# t.test_files = FileList['test/**/*_test.rb']
8+
# t.verbose = true
9+
# end
10+
11+
12+
require 'rspec/core/rake_task'
13+
14+
RSpec::Core::RakeTask.new(:spec)
15+
task default: :spec

ajaxify_rails.gemspec

+11
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,15 @@ Gem::Specification.new do |gem|
2020
gem.version = AjaxifyRails::VERSION
2121

2222
gem.add_dependency 'rails', '>= 3.1.0'
23+
24+
gem.add_development_dependency 'jquery-rails'
25+
gem.add_development_dependency 'rspec-rails', '2.9'
26+
gem.add_development_dependency 'rspec-steps'
27+
gem.add_development_dependency 'capybara'
28+
gem.add_development_dependency 'selenium-webdriver', '>= 2.30.0'
29+
gem.add_development_dependency 'sqlite3'
30+
gem.add_development_dependency 'haml', '>= 3.1.5'
31+
gem.add_development_dependency 'sass-rails', '~> 3.2.3'
32+
gem.add_development_dependency 'coffee-rails', '~> 3.2.1'
33+
#gem.add_development_dependency 'poltergeist'
2334
end

lib/ajaxify_rails/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module AjaxifyRails
2-
VERSION = "0.2.1"
2+
VERSION = "0.5"
33
end

spec/ajaxify_test

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 66dddc2c992a8ab6a46cddc7471c239cb5aa711b
+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
require 'spec_helper'
2+
3+
4+
steps 'Test basic behavior', :js => true do
5+
6+
before(:each) do # only executed once before all "it" blocks (see https://github.com/LRDesign/rspec-steps)
7+
visit root_path(ajaxify_on: true)
8+
@layout_id = find('body')['data-id']
9+
end
10+
11+
it "should display loader" do
12+
click_on 'Page 1'
13+
page.should have_css(".ajaxify_loader")
14+
end
15+
16+
it "should load page's content" do
17+
should_have_content 'Page 1 Content'
18+
push_state_path_should_be '/home/page1'
19+
ensure_layout_not_reloaded
20+
end
21+
22+
it "should redirect" do
23+
click_on 'Page 3 (redirects to page 1)'
24+
should_have_content 'Page 1 Content'
25+
push_state_path_should_be '/home/page1'
26+
ensure_layout_not_reloaded
27+
end
28+
29+
it "should display flash" do
30+
should_have_notice 'Redirected to Page 1'
31+
end
32+
33+
it "should submit a form" do
34+
click_on 'Page 2'
35+
fill_in 'input1', with: 'test'
36+
check 'check_me'
37+
push_state_path_should_be '/home/page2'
38+
click_button 'Submit me'
39+
should_have_notice 'Form submitted (test, , 1)'
40+
ensure_layout_not_reloaded
41+
end
42+
43+
it "should load previous content when clicking the browser back button" do
44+
click_on 'Home'
45+
wait_for_ajaxify_loaded
46+
click_on 'Page 1'
47+
wait_for_ajaxify_loaded
48+
click_browser_back_button
49+
should_have_content 'Home Content'
50+
push_state_path_should_be '/'
51+
ensure_layout_not_reloaded
52+
end
53+
54+
it "should load next content when clicking the browser forward button" do
55+
click_browser_forward_button
56+
should_have_content 'Page 1 Content'
57+
push_state_path_should_be '/home/page1'
58+
ensure_layout_not_reloaded
59+
end
60+
61+
it "should not repost form clicking the browser back button" do
62+
click_on 'Page 2'
63+
click_button 'Submit me'
64+
wait_for_ajaxify_loaded
65+
click_on 'Page 1'
66+
wait_for_ajaxify_loaded
67+
click_browser_back_button
68+
wait_for_ajaxify_loaded
69+
page.should_not have_css("#notice", visible: true)
70+
should_have_content /A Form/
71+
push_state_path_should_be '/home/page2'
72+
end
73+
74+
end
75+
76+
77+
steps 'Test hash based url scheme for browsers without pushState', :js => true do
78+
79+
before(:each) do # only executed once before all "it" blocks (see https://github.com/LRDesign/rspec-steps)
80+
visit root_path(ajaxify_on: true, push_state_enabled: false)
81+
@layout_id = find('body')['data-id']
82+
#sleep 2 # poltergeist needs this
83+
hash_path_should_be '/?ajaxify_on=true&push_state_enabled=false'
84+
end
85+
86+
it "should display loader" do
87+
click_on 'Page 1'
88+
page.should have_css(".ajaxify_loader")
89+
end
90+
91+
it "should load page's content" do
92+
should_have_content 'Page 1 Content'
93+
hash_path_should_be '/home/page1'
94+
ensure_layout_not_reloaded
95+
end
96+
97+
it "should redirect" do
98+
click_on 'Page 3 (redirects to page 1)'
99+
should_have_content 'Page 1 Content'
100+
hash_path_should_be '/home/page1'
101+
ensure_layout_not_reloaded
102+
end
103+
104+
it "should display flash" do
105+
should_have_notice 'Redirected to Page 1'
106+
end
107+
108+
it "should submit a form" do
109+
click_on 'Page 2'
110+
fill_in 'input1', with: 'test'
111+
check 'check_me'
112+
hash_path_should_be '/home/page2'
113+
click_button 'Submit me'
114+
should_have_notice 'Form submitted (test, , 1)'
115+
ensure_layout_not_reloaded
116+
end
117+
118+
it "should load previous content when clicking the browser back button" do
119+
click_on 'Home'
120+
wait_for_ajaxify_loaded
121+
click_on 'Page 1'
122+
wait_for_ajaxify_loaded
123+
click_browser_back_button
124+
should_have_content 'Home Content'
125+
hash_path_should_be '/'
126+
ensure_layout_not_reloaded
127+
end
128+
129+
it "should load next content when clicking the browser forward button" do
130+
click_browser_forward_button
131+
should_have_content 'Page 1 Content'
132+
hash_path_should_be '/home/page1'
133+
ensure_layout_not_reloaded
134+
end
135+
136+
it "should not repost form clicking the browser back button" do
137+
click_on 'Page 2'
138+
click_button 'Submit me'
139+
wait_for_ajaxify_loaded
140+
click_on 'Page 1'
141+
wait_for_ajaxify_loaded
142+
click_browser_back_button
143+
wait_for_ajaxify_loaded
144+
page.should_not have_css("#notice", visible: true)
145+
should_have_content /A Form/
146+
hash_path_should_be '/home/page2'
147+
end
148+
149+
end
150+
151+
152+
describe 'Test convert urls', :js => true do
153+
154+
it 'should convert a hash url to a proper one in a browser supporting pushState' do
155+
visit root_path(ajaxify_on: true, anchor: '/home/page1')
156+
should_have_content 'Page 1 Content'
157+
push_state_path_should_be '/home/page1'
158+
end
159+
160+
it 'should convert a proper url to a hash url in a browser not supporting pushState' do
161+
visit '/home/page1?ajaxify_on=true&push_state_enabled=false'
162+
should_have_content 'Page 1 Content'
163+
hash_path_should_be '/home/page1?ajaxify_on=true&push_state_enabled=false'
164+
end
165+
166+
end

spec/spec_helper.rb

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
4+
# loaded once.
5+
#
6+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7+
8+
ENV["RAILS_ENV"] = 'test'
9+
require_relative "ajaxify_test/config/environment"
10+
11+
require 'rspec/rails'
12+
require 'rspec-steps'
13+
require 'sqlite3'
14+
15+
require 'haml'
16+
17+
# Add this to load Capybara integration:
18+
require 'capybara/rspec'
19+
require 'capybara/rails'
20+
21+
# require 'capybara/poltergeist'
22+
23+
# Capybara.register_driver :poltergeist do |app|
24+
# # Specifiying the phantomjs path only for RubyMine which fails to pick it up otherwise
25+
# Capybara::Poltergeist::Driver.new(app, phantomjs: '/usr/local/bin/phantomjs') #, inspector: true
26+
# end
27+
28+
# Capybara.javascript_driver = :poltergeist
29+
# Capybara.current_driver = :poltergeist
30+
31+
Capybara.default_selector = :css
32+
Capybara.default_wait_time = 3
33+
34+
RSpec.configure do |config|
35+
config.treat_symbols_as_metadata_keys_with_true_values = true
36+
#config.run_all_when_everything_filtered = true
37+
config.use_transactional_fixtures = false
38+
config.include Capybara::DSL
39+
end
40+
41+
42+
def wait_until_gone css
43+
page.should have_no_css(css)
44+
end
45+
46+
def wait_for_ajaxify_loaded
47+
wait_until_gone ".ajaxify_loader"
48+
end
49+
50+
def click_browser_back_button
51+
page.execute_script("window.history.back()")
52+
end
53+
54+
def click_browser_forward_button
55+
page.execute_script("window.history.forward()")
56+
end
57+
58+
def ensure_layout_not_reloaded
59+
find('body')['data-id'].to_s.should eq(@layout_id), "Layout has been reloaded but shouldn't"
60+
end
61+
62+
def should_have_content content
63+
page.should have_css('#main', text: content)
64+
end
65+
66+
def should_have_notice notice
67+
page.should have_css('#notice', text: notice)
68+
end
69+
70+
def disable_push_state
71+
page.execute_script("window.history.pushState = null;")
72+
end
73+
74+
def current_hash
75+
URI.parse(current_url).fragment.to_s
76+
end
77+
78+
def hash_path_should_be path
79+
current_hash.should == path
80+
current_path.should == '/'
81+
end
82+
83+
def push_state_path_should_be path
84+
current_hash.should == ''
85+
current_path.should == path
86+
end
87+

0 commit comments

Comments
 (0)