Skip to content
This repository has been archived by the owner on Nov 17, 2018. It is now read-only.

Commit

Permalink
Add tests for ajax lib
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Dec 20, 2008
1 parent 88fb201 commit 1e30462
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
6 changes: 4 additions & 2 deletions build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
code.scan(/(^\/\/.+?)(mini\.[a-z0-9_]+\.[a-z0-9_]+)=function\(([a-z0-9_,]*)\)/m).each do |doc, name, args|
content << "@#{name}(#{args})@\n"
content << doc.gsub(/^\/\/\s*/, '').sub(/\n+$/, '') + "\n\n"
end
File.open(File.join('pkg', lib + '.js'), 'w') { |f| f.write(code.gsub(/^\/\/.+\n/, '').gsub(/^\n/, '').gsub(/\n$/, '')) }
end
code = code.gsub(/^\/\/.+\n/, '').gsub(/^\n/, '').sub(/\n$/, '')
File.open(File.join('pkg', lib + '.js'), 'w') { |f| f.write(code) }
File.open(File.join('test/public/js', lib + '.js'), 'w') { |f| f.write(code) }
end

File.open(README, 'w') { |f| f.write(content) }
64 changes: 64 additions & 0 deletions test/public/html/ajax_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<html>
<head>
<title>mini.ajax.js test</title>
<script type="text/javascript" src="/js/ajax.js"></script>
<style type="text/css">
code {
background-color: #eee;
}
</style>
</head>
<body>
<h1>mini.ajax.js test</h1>
<p>This HTML page serves to test the mini.ajax.js library in its basic functionality.</p>

<h2>collect()</h2>
<p>
<a href="#" onclick="$('collect_results').innerHTML=collect(['a','b','c','notme'],function(i){if(i!='notme')return i+' changed'});return false;">test collect()</a>
</p>
<div id="collect_results">a,b,c,notme</div>

<h2>mini.ajax.serialize()</h2>
<p>Submit this form to test the <code>mini.ajax.serialize()</code> function.</p>
<form onsubmit="$('serialize_results').innerHTML=mini.form.serialize(this);return false;">
<input type="hidden" name="hideme" value="i am hidden"/>
<input name="text_input" value="some text"/>
<input type="checkbox" name="checkbox_input1" value="yes"/>
<input type="checkbox" name="checkbox_input2" value="yes" checked="checked"/>
<input type="radio" name="radio_input" value="value 1"/>
<input type="radio" name="radio_input" value="value 2" checked="checked"/>
<input type="radio" name="radio_input" value="value 2"/>
<select name="select_input">
<option value="value 1">value 1</option>
<option value="value 2">value 2</option>
<option value="value 3">value 3</option>
</select>
<textarea name="textarea_input">here is some text
with

a line break or two</textarea>
<input type="submit" value="test serialize"/>
</form>
<div id="serialize_results"></div>

<h2>mini.ajax.get()</h2>
<p><a href="#" onclick="mini.ajax.get('/echo?foo=bar', function(r){$('get_results').innerHTML=r});return false;">test mini.ajax.get()</a></p>
<div id="get_results"></div>

<h2>mini.ajax.post()</h2>
<p><a href="#" onclick="mini.ajax.post('/echo', function(r){$('post_results').innerHTML=r}, 'bar=baz');return false;">test mini.ajax.post()</a></p>
<div id="post_results"></div>

<h2>mini.ajax.update()</h2>
<p><a href="#" onclick="mini.ajax.update('/echo', 'update_results', 'POST', 'baz=foo');return false;">test mini.ajax.update()</a></p>
<div id="update_results"></div>

<h2>mini.ajax.submit()</h2>
<form onsubmit="mini.ajax.submit('/echo', this, 'submit_results');return false;">
<input name="foo" value="bar"/>
<input type="submit" value="test submit"/>
</form>
<div id="submit_results"></div>
</body>
</html>

11 changes: 11 additions & 0 deletions test/public/js/ajax.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/test_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rubygems'
require 'sinatra'

get '/' do
"test_server.rb is running"
end

get '/echo' do
"you sent a GET with args #{params.inspect}"
end

post '/echo' do
"you sent a POST with args #{params.inspect}"
end
57 changes: 57 additions & 0 deletions test/tests/ajax_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'rubygems'
require 'firewatir'
require 'test/unit'
require 'cgi'
require 'open-uri'

open('http://localhost:4567/') rescue raise('test_server.rb must be running for this set of tests')

TEST_DIR = File.expand_path(File.dirname(__FILE__) + '/..')
FF = FireWatir::Firefox.start("http://localhost:4567/html/ajax_test.html")

class AjaxTest < Test::Unit::TestCase

def setup
@ff = FF
end

def test_collect
@ff.link(:text, 'test collect()').click
assert_equal 'a changed,b changed,c changed', @ff.div(:id, 'collect_results').text
end

def test_serialize
@ff.button(:value, 'test serialize').click
results = @ff.div(:id, 'serialize_results').text
unescaped = CGI.unescape(results)
assert_match /hideme=i am hidden/, unescaped
assert_match /text_input=some text/, unescaped
assert_no_match /checkbox_input1=yes/, unescaped
assert_match /checkbox_input2=yes/, unescaped
assert_match /radio_input=value 2/, unescaped
assert_no_match /radio_input=value 1/, unescaped
assert_match /select_input=value 1/, unescaped
assert_no_match /select_input=value 2/, unescaped
assert_match /textarea_input=here is some text\s*with\s*a line break or two/, unescaped
end

def test_get
@ff.link(:text, 'test mini.ajax.get()').click
assert_equal 'you sent a GET with args {"foo"=>"bar"}', @ff.div(:id, 'get_results').text
end

def test_post
@ff.link(:text, 'test mini.ajax.post()').click
assert_equal 'you sent a POST with args {"bar"=>"baz"}', @ff.div(:id, 'post_results').text
end

def test_update
@ff.link(:text, 'test mini.ajax.update()').click
assert_equal 'you sent a POST with args {"baz"=>"foo"}', @ff.div(:id, 'update_results').text
end

def test_submit
@ff.button(:value, 'test submit').click
assert_equal 'you sent a POST with args {"foo"=>"bar"}', @ff.div(:id, 'submit_results').text
end
end

0 comments on commit 1e30462

Please sign in to comment.