| 1 | $:.unshift(File.dirname(__FILE__)) |
|---|
| 2 | require 'spec_helper' |
|---|
| 3 | require 'time' |
|---|
| 4 | |
|---|
| 5 | describe "twitter_js plugin" do |
|---|
| 6 | def setup_twitter_js_plugin(mode, user_id) |
|---|
| 7 | fake_plugin(:twitter_js) { |plugin| |
|---|
| 8 | plugin.mode = mode |
|---|
| 9 | plugin.conf['twitter.user'] = user_id |
|---|
| 10 | plugin.date = Time.parse("20080124") |
|---|
| 11 | } |
|---|
| 12 | end |
|---|
| 13 | |
|---|
| 14 | describe "should render javascript and div tag in day" do |
|---|
| 15 | before do |
|---|
| 16 | @plugin = setup_twitter_js_plugin("day", "123456789") |
|---|
| 17 | end |
|---|
| 18 | |
|---|
| 19 | it "for header" do |
|---|
| 20 | snippet = @plugin.header_proc |
|---|
| 21 | snippet.should == expected_html_header_snippet("123456789") |
|---|
| 22 | end |
|---|
| 23 | |
|---|
| 24 | it "for body leave" do |
|---|
| 25 | snippet = @plugin.body_leave_proc(Time.parse("20080124")) |
|---|
| 26 | snippet.should == expected_html_body_snippet |
|---|
| 27 | end |
|---|
| 28 | end |
|---|
| 29 | |
|---|
| 30 | describe "should render javascript and div tag in latest" do |
|---|
| 31 | before do |
|---|
| 32 | @plugin = setup_twitter_js_plugin("latest", "123456789") |
|---|
| 33 | end |
|---|
| 34 | |
|---|
| 35 | it "for header" do |
|---|
| 36 | snippet = @plugin.header_proc |
|---|
| 37 | snippet.should == expected_html_header_snippet("123456789") |
|---|
| 38 | end |
|---|
| 39 | |
|---|
| 40 | it "for body leave" do |
|---|
| 41 | snippet = @plugin.body_leave_proc(Time.parse("20080124")) |
|---|
| 42 | snippet.should == expected_html_body_snippet |
|---|
| 43 | end |
|---|
| 44 | end |
|---|
| 45 | |
|---|
| 46 | describe "should not render in edit" do |
|---|
| 47 | before do |
|---|
| 48 | @plugin = setup_twitter_js_plugin("edit", "123456789") |
|---|
| 49 | end |
|---|
| 50 | |
|---|
| 51 | it "for header" do |
|---|
| 52 | snippet = @plugin.header_proc |
|---|
| 53 | snippet.should be_empty |
|---|
| 54 | end |
|---|
| 55 | |
|---|
| 56 | it "for body leave" do |
|---|
| 57 | snippet = @plugin.body_leave_proc(Time.parse("20080124")) |
|---|
| 58 | snippet.should be_empty |
|---|
| 59 | end |
|---|
| 60 | end |
|---|
| 61 | |
|---|
| 62 | describe "should not render when user_id is empty" do |
|---|
| 63 | before do |
|---|
| 64 | @plugin = setup_twitter_js_plugin("edit", "") |
|---|
| 65 | end |
|---|
| 66 | |
|---|
| 67 | it "for header" do |
|---|
| 68 | snippet = @plugin.header_proc |
|---|
| 69 | snippet.should be_empty |
|---|
| 70 | end |
|---|
| 71 | |
|---|
| 72 | it "for body leave" do |
|---|
| 73 | snippet = @plugin.body_leave_proc(Time.parse("")) |
|---|
| 74 | snippet.should be_empty |
|---|
| 75 | end |
|---|
| 76 | end |
|---|
| 77 | |
|---|
| 78 | def expected_html_header_snippet(user_id) |
|---|
| 79 | expected = <<-EXPECTED |
|---|
| 80 | <script type="text/javascript"><!-- |
|---|
| 81 | function twitter_cb(a){ |
|---|
| 82 | var f=function(n){return (n<10?"0":"")+n}; |
|---|
| 83 | for(var i=0;i<a.length;i++){ |
|---|
| 84 | var d=new Date(a[i]['created_at'].replace('+0000','UTC')); |
|---|
| 85 | var id="twitter_statuses_"+f(d.getFullYear())+f(d.getMonth()+1)+f(d.getDate()); |
|---|
| 86 | var e=document.getElementById(id); |
|---|
| 87 | if(!e) continue; |
|---|
| 88 | if(!e.innerHTML) e.innerHTML='<h3><a href="http://twitter.com/#{user_id}">Twitter statuses</a></h3>'; |
|---|
| 89 | e.innerHTML+='<p><strong>'+a[i]['text']+'</strong> ('+f(d.getHours())+':'+f(d.getMinutes())+':'+f(d.getSeconds())+')</p>'; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | function twitter_js(){ |
|---|
| 93 | var e=document.createElement("script"); |
|---|
| 94 | e.type="text/javascript"; |
|---|
| 95 | e.src="http://twitter.com/statuses/user_timeline/#{user_id}.json?callback=twitter_cb&count=20"; |
|---|
| 96 | document.documentElement.appendChild(e); |
|---|
| 97 | } |
|---|
| 98 | if(window.addEventListener){ |
|---|
| 99 | window.addEventListener('load',twitter_js,false); |
|---|
| 100 | }else if(window.attachEvent){ |
|---|
| 101 | window.attachEvent('onload',twitter_js); |
|---|
| 102 | }else{ |
|---|
| 103 | window.onload=twitter_js; |
|---|
| 104 | } |
|---|
| 105 | // --></script> |
|---|
| 106 | EXPECTED |
|---|
| 107 | expected.gsub(/^\t/, '').chomp |
|---|
| 108 | end |
|---|
| 109 | |
|---|
| 110 | def expected_html_body_snippet |
|---|
| 111 | expected = <<-HTML |
|---|
| 112 | <div id="twitter_statuses_20080124" class="section"></div> |
|---|
| 113 | HTML |
|---|
| 114 | expected.gsub( /^\t/, '' ).chomp |
|---|
| 115 | end |
|---|
| 116 | end |
|---|