File: twitter_js_spec.rb

package info (click to toggle)
tdiary-contrib 5.0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,660 kB
  • sloc: ruby: 17,024; lisp: 546; xml: 451; php: 61; sql: 40; makefile: 18
file content (116 lines) | stat: -rw-r--r-- 3,165 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
$:.unshift(File.dirname(__FILE__))
require 'spec_helper'
require 'time'

describe "twitter_js plugin" do
	def setup_twitter_js_plugin(mode, user_id)
		fake_plugin(:twitter_js) { |plugin|
			plugin.mode = mode
			plugin.conf['twitter.user'] = user_id
			plugin.date = Time.parse("20080124")
		}
	end

	describe "should render javascript and div tag in day" do
		before do
			@plugin = setup_twitter_js_plugin("day", "123456789")
		end

		it "for header" do
			snippet = @plugin.header_proc
			expect(snippet).to eq(expected_html_header_snippet("123456789"))
		end

		it "for body leave" do
			snippet = @plugin.body_leave_proc(Time.parse("20080124"))
			expect(snippet).to eq(expected_html_body_snippet)
		end
	end

	describe "should render javascript and div tag in latest" do
		before do
			@plugin = setup_twitter_js_plugin("latest", "123456789")
		end

		it "for header" do
			snippet = @plugin.header_proc
			expect(snippet).to eq(expected_html_header_snippet("123456789"))
		end

		it "for body leave" do
			snippet = @plugin.body_leave_proc(Time.parse("20080124"))
			expect(snippet).to eq(expected_html_body_snippet)
		end
	end

	describe "should not render in edit" do
		before do
			@plugin = setup_twitter_js_plugin("edit", "123456789")
		end

		it "for header" do
			snippet = @plugin.header_proc
			expect(snippet).to be_empty
		end

		it "for body leave" do
			snippet = @plugin.body_leave_proc(Time.parse("20080124"))
			expect(snippet).to be_empty
		end
	end

	describe "should not render when user_id is empty" do
		before do
			@plugin = setup_twitter_js_plugin("edit", "")
		end

		it "for header" do
			snippet = @plugin.header_proc
			expect(snippet).to be_empty
		end

		it "for body leave" do
			snippet = @plugin.body_leave_proc(Time.parse("20080124"))
			expect(snippet).to be_empty
		end
	end

	def expected_html_header_snippet(user_id)
		expected = <<-EXPECTED
		<script type="text/javascript"><!--
		function twitter_cb(a){
			var f=function(n){return (n<10?'0':'')+n};
			for(var i=0,l=a.length;i<l;i++){
				var d=new Date(a[i]['created_at'].replace('+0000','UTC'));
				var id='twitter_statuses_'+f(d.getFullYear())+f(d.getMonth()+1)+f(d.getDate());
				var e=document.getElementById(id);
				if(!e) continue;
				if(!e.innerHTML) e.innerHTML='<h3><a href="http://twitter.com/#{user_id}">Twitter statuses</a></h3>';
				e.innerHTML+='<p><strong>'+a[i]['text']+'</strong> ('+f(d.getHours())+':'+f(d.getMinutes())+':'+f(d.getSeconds())+')</p>';
			}
		}
		function twitter_js(){
			var e=document.createElement('script');
			e.type='text/javascript';
			e.src='http://twitter.com/statuses/user_timeline/#{user_id}.json?callback=twitter_cb&amp;count=20';
			document.documentElement.appendChild(e);
		}
		if(window.addEventListener){
			window.addEventListener('load',twitter_js,false);
		}else if(window.attachEvent){
			window.attachEvent('onload',twitter_js);
		}else{
			window.onload=twitter_js;
		}
		// --></script>
		EXPECTED
		expected.gsub(/^\t/, '').chomp
	end

	def expected_html_body_snippet
		expected = <<-HTML
		<div id="twitter_statuses_20080124" class="section"></div>
		HTML
		expected.gsub( /^\t/, '' ).chomp
	end
end