forked from indirect/rails-footnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7243773
commit 702a050
Showing
6 changed files
with
328 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
require "spec_helper" | ||
require 'action_controller' | ||
require 'action_controller/test_case' | ||
|
||
class FootnotesController < ActionController::Base | ||
attr_accessor :template, :performed_render | ||
end | ||
|
||
module Footnotes::Notes | ||
class TestNote < AbstractNote | ||
def self.to_sym; :test; end | ||
def valid?; true; end | ||
end | ||
|
||
class NoteXNote < TestNote; end | ||
class NoteYNote < TestNote; end | ||
class NoteZNote < TestNote; end | ||
end | ||
|
||
describe "Footnotes" do | ||
before do | ||
@controller = FootnotesController.new | ||
@controller.template = Object.new | ||
@controller.request = ActionController::TestRequest.new | ||
@controller.response = ActionController::TestResponse.new | ||
@controller.response_body = $html.dup | ||
@controller.params = {} | ||
|
||
Footnotes::Filter.notes = [ :test ] | ||
Footnotes::Filter.multiple_notes = false | ||
@footnotes = Footnotes::Filter.new(@controller) | ||
end | ||
|
||
it "footnotes_controller" do | ||
index = @controller.response.body.index(/This is the HTML page/) | ||
index.should eql 334 | ||
end | ||
|
||
it "foonotes_included" do | ||
footnotes_perform! | ||
@controller.response_body.should_not eql $html | ||
end | ||
|
||
specify "footnotes_not_included_when_request_is_xhr" do | ||
@controller.request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' | ||
@controller.request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*' | ||
footnotes_perform! | ||
@controller.response.body.should eql $html | ||
end | ||
|
||
specify "footnotes_not_included_when_content_type_is_javascript" do | ||
@controller.response.headers['Content-Type'] = 'text/javascript' | ||
footnotes_perform! | ||
@controller.response.body.should eql $html | ||
end | ||
|
||
specify "footnotes_included_when_content_type_is_html" do | ||
@controller.response.headers['Content-Type'] = 'text/html' | ||
footnotes_perform! | ||
@controller.response.body.should_not eql $html | ||
end | ||
|
||
specify "footnotes_included_when_content_type_is_nil" do | ||
footnotes_perform! | ||
@controller.response.body.should_not eql $html | ||
end | ||
|
||
specify "not_included_when_body_is_not_a_string" do | ||
@controller.response.body = Proc.new { Time.now } | ||
expect { footnotes_perform! }.should_not raise_exception | ||
end | ||
|
||
specify "notes_are_initialized" do | ||
footnotes_perform! | ||
test_note = @footnotes.instance_variable_get('@notes').first | ||
test_note.class.name.should eql 'Footnotes::Notes::TestNote' | ||
test_note.to_sym.should eql :test | ||
end | ||
|
||
specify "notes_links" do | ||
note = Footnotes::Notes::TestNote.new | ||
note.should_receive(:row).twice | ||
@footnotes.instance_variable_set(:@notes, [note]) | ||
footnotes_perform! | ||
end | ||
|
||
specify "notes_fieldset" do | ||
note = Footnotes::Notes::TestNote.new | ||
note.should_receive(:has_fieldset?).exactly(3).times | ||
@footnotes.instance_variable_set(:@notes, [note]) | ||
footnotes_perform! | ||
end | ||
|
||
specify "multiple_notes" do | ||
Footnotes::Filter.multiple_notes = true | ||
note = Footnotes::Notes::TestNote.new | ||
note.should_receive(:has_fieldset?).twice | ||
@footnotes.instance_variable_set(:@notes, [note]) | ||
footnotes_perform! | ||
end | ||
|
||
specify "notes_are_reset" do | ||
note = Footnotes::Notes::TestNote.new | ||
note.class.should_receive(:close!) | ||
@footnotes.instance_variable_set(:@notes, [note]) | ||
@footnotes.send(:close!, @controller) | ||
end | ||
|
||
specify "links_helper" do | ||
note = Footnotes::Notes::TestNote.new | ||
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="">Test</a>' | ||
|
||
note.should_receive(:link).once.and_return(:link) | ||
@footnotes.send(:link_helper, note).should eql '<a href="link" onclick="">Test</a>' | ||
end | ||
|
||
specify "links_helper_has_fieldset?" do | ||
note = Footnotes::Notes::TestNote.new | ||
note.should_receive(:has_fieldset?).once.and_return(true) | ||
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="Footnotes.hideAllAndToggle(\'test_debug_info\');return false;">Test</a>' | ||
end | ||
|
||
specify "links_helper_onclick" do | ||
note = Footnotes::Notes::TestNote.new | ||
note.should_receive(:onclick).twice.and_return(:onclick) | ||
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="onclick">Test</a>' | ||
|
||
note.should_receive(:has_fieldset?).once.and_return(true) | ||
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="onclick">Test</a>' | ||
end | ||
|
||
specify "insert_style" do | ||
@controller.response.body = "<head></head><split><body></body>" | ||
@footnotes = Footnotes::Filter.new(@controller) | ||
footnotes_perform! | ||
@controller.response.body.split('<split>').first.include?('<!-- Footnotes Style -->').should be | ||
end | ||
|
||
specify "insert_footnotes_inside_body" do | ||
@controller.response.body = "<head></head><split><body></body>" | ||
@footnotes = Footnotes::Filter.new(@controller) | ||
footnotes_perform! | ||
@controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->').should be | ||
end | ||
|
||
specify "insert_footnotes_inside_holder" do | ||
@controller.response.body = "<head></head><split><div id='footnotes_holder'></div>" | ||
@footnotes = Footnotes::Filter.new(@controller) | ||
footnotes_perform! | ||
@controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->').should be | ||
end | ||
|
||
specify "insert_text" do | ||
@footnotes.send(:insert_text, :after, /<head>/, "Graffiti") | ||
after = " <head>Graffiti" | ||
@controller.response.body.split("\n")[2].should eql after | ||
|
||
@footnotes.send(:insert_text, :before, /<\/body>/, "Notes") | ||
after = " Notes</body>" | ||
@controller.response.body.split("\n")[12].should eql after | ||
end | ||
|
||
describe 'Hooks' do | ||
before {Footnotes::Filter.notes = [:note_x, :note_y, :note_z]} | ||
context 'before' do | ||
specify do | ||
Footnotes.setup {|config| config.before {|controller, filter| filter.notes -= [:note_y] }} | ||
Footnotes::Filter.start!(@controller) | ||
Footnotes::Filter.notes.should eql [:note_x, :note_z] | ||
end | ||
end | ||
context "after" do | ||
specify do | ||
Footnotes.setup {|config| config.after {|controller, filter| filter.notes -= [:note_y] }} | ||
Footnotes::Filter.start!(@controller) | ||
Footnotes::Filter.notes.should eql [:note_x, :note_z] | ||
end | ||
end | ||
end | ||
|
||
protected | ||
def footnotes_perform! | ||
template_expects('html') | ||
@controller.performed_render = true | ||
|
||
Footnotes::Filter.start!(@controller) | ||
@footnotes.add_footnotes! | ||
end | ||
|
||
def template_expects(format) | ||
if @controller.template.respond_to?(:template_format) | ||
@controller.template.stub(:template_format).and_return(format) | ||
else | ||
@controller.template.stub(:format).and_return(format) | ||
end | ||
end | ||
|
||
$html = <<HTML | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | ||
<html> | ||
<head> | ||
<title>HTML to XHTML Example: HTML page</title> | ||
<link rel="Stylesheet" href="htmltohxhtml.css" type="text/css" media="screen"> | ||
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | ||
</head> | ||
<body> | ||
<p>This is the HTML page. It works and is encoded just like any HTML page you | ||
have previously done. View <a href="htmltoxhtml2.htm">the XHTML version</a> of | ||
this page to view the difference between HTML and XHTML.</p> | ||
<p>You will be glad to know that no changes need to be made to any of your CSS files.</p> | ||
</body> | ||
</html> | ||
HTML | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require "spec_helper" | ||
|
||
describe Footnotes::Notes::AbstractNote do | ||
before do | ||
@note = Footnotes::Notes::AbstractNote.new | ||
Footnotes::Filter.notes = [:abstract] | ||
end | ||
|
||
it {described_class.should respond_to :start!} | ||
it {described_class.should respond_to :close!} | ||
it {described_class.should respond_to :title} | ||
|
||
it {should respond_to :to_sym} | ||
its(:to_sym) {should eql :abstract} | ||
|
||
it { described_class.should be_included } | ||
specify do | ||
Footnotes::Filter.notes = [] | ||
described_class.should_not be_included | ||
end | ||
|
||
it { should respond_to :row } | ||
it { should respond_to :legend } | ||
it { should respond_to :link } | ||
it { should respond_to :onclick } | ||
it { should respond_to :stylesheet } | ||
it { should respond_to :javascript } | ||
|
||
it { should respond_to :valid? } | ||
it { should be_valid } | ||
|
||
it { should respond_to :has_fieldset? } | ||
it { should_not have_fieldset } | ||
|
||
specify { Footnotes::Filter.prefix = ''; should_not be_prefix } | ||
specify do | ||
Footnotes::Filter.prefix = 'txmt://open?url=file://%s&line=%d&column=%d' | ||
should be_prefix | ||
end | ||
|
||
#TODO should be moved to builder | ||
#helpers | ||
specify { subject.escape('<').should eql '<' } | ||
specify { subject.escape('&').should eql '&' } | ||
specify { subject.escape('>').should eql '>' } | ||
|
||
specify { subject.mount_table([]).should be_blank } | ||
specify { subject.mount_table([['h1', 'h2', 'h3']], :class => 'table').should be_blank } | ||
|
||
specify { | ||
tab = <<-TABLE | ||
<table class="table" > | ||
<thead><tr><th>H1</th></tr></thead> | ||
<tbody><tr><td>r1c1</td></tr></tbody> | ||
</table> | ||
TABLE | ||
|
||
subject.mount_table([['h1'],['r1c1']], :class => 'table').should eql tab | ||
} | ||
|
||
specify { | ||
tab = <<-TABLE | ||
<table > | ||
<thead><tr><th>H1</th><th>H2</th><th>H3</th></tr></thead> | ||
<tbody><tr><td>r1c1</td><td>r1c2</td><td>r1c3</td></tr></tbody> | ||
</table> | ||
TABLE | ||
subject.mount_table([['h1', 'h2', 'h3'],['r1c1', 'r1c2', 'r1c3']]).should eql tab | ||
} | ||
|
||
specify { | ||
tab = <<-TABLE | ||
<table > | ||
<thead><tr><th>H1</th><th>H2</th><th>H3</th></tr></thead> | ||
<tbody><tr><td>r1c1</td><td>r1c2</td><td>r1c3</td></tr><tr><td>r2c1</td><td>r2c2</td><td>r2c3</td></tr></tbody> | ||
</table> | ||
TABLE | ||
subject.mount_table([['h1', 'h2', 'h3'], ['r1c1', 'r1c2', 'r1c3'], ['r2c1', 'r2c2', 'r2c3']]) | ||
} | ||
end |
Oops, something went wrong.