File: amp_spec.rb

package info (click to toggle)
tdiary 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,088 kB
  • sloc: ruby: 23,031; javascript: 1,029; xml: 325; makefile: 26; sh: 4
file content (127 lines) | stat: -rw-r--r-- 3,811 bytes parent folder | download | duplicates (4)
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
117
118
119
120
121
122
123
124
125
126
127
# coding: utf-8
require File.expand_path('../plugin_helper', __FILE__)
require 'tdiary/plugin'

describe "amp plugin w/" do
	before do
		# stub of TDiary::IO::Diary
		@diary = Object.new.tap {|diary|
			allow(diary).to receive(:title) { 'Example Diary' }
			allow(diary).to receive(:date) { Time.parse('2017/3/2') }
		}
		@conf = PluginFake::Config.new.tap {|conf|
			conf.plugin_path = 'spec/fixtures/plugin'
			conf.lang = 'ja'
			allow(conf).to receive(:base_url) { 'http://example.com/index.rb' }
		}
		@plugin = TDiary::Plugin.new(
			debug: true,
			conf: @conf,
			date: Time.parse('2017/3/2'),
			diaries: { '20170302' => @diary }
		).tap {|plugin|
			plugin.load_plugin('misc/plugin/amp.rb')
		}
	end

	describe "#amp_canonical_url" do
		subject { @plugin.amp_canonical_url(@diary).to_s }
		it { expect(subject).to eq('http://example.com/index.rb?date=20170302') }
	end

	describe "#header_proc" do
		subject { @plugin.__send__(:header_proc) }

		describe "when @mode == 'day'" do
			before { @plugin.instance_variable_set(:@mode, 'day') }
			it { expect(subject).to eq('<link rel="amphtml" href="http://example.com/index.rb?date=20170302&plugin=amp">') }
		end

		describe "when @mode != 'day'" do
			before { @plugin.instance_variable_set(:@mode, 'latest') }
			it { expect(subject).to eq('') }
		end
	end

	describe "#amp_theme_css" do
		subject { @plugin.amp_theme_css }

		describe "when local theme" do
			before {
				@conf.theme = 'local/default'
				allow(@plugin).to receive(:theme_paths_local) { ['theme/*'] }
			}
			it { expect(subject).to include('Title: tDiary3 default') }
		end

		describe "when online theme" do
			before {
				begin
					require 'open-uri'
					URI.open('https://tdiary.github.io/tdiary-theme/default/default.css')
				rescue
					skip("Debian buildd might not have internet access")
				end
				@conf.theme = 'online/default'
				allow(@plugin).to receive(:theme_url_online) { '//tdiary.github.io/tdiary-theme/default/default.css' }
			}
			it { expect(subject).to include('Title: tDiary3 default') }
		end
	end

	describe "#amp_day_title" do
		subject { @plugin.amp_day_title(@diary) }
		it { expect(subject).to eq('Example Diary') }
	end

	describe "#amp_body" do
		subject { @plugin.amp_body(@diary) }

		describe "when contain img element" do
			before { allow(@diary).to receive(:to_html) {
				<<-HTML
				<h3>subsection</h3>
				<p>This is a test diary.</p>
				<img src="image.jpg" weight="640" height="480">
				HTML
			} }
			it { expect(subject).to include('<h3>subsection</h3>') }
			it { expect(subject).to include('<amp-img layout="responsive" src="image.jpg" weight="640" height="480">') }
		end

		describe "when contain script element" do
			before { allow(@diary).to receive(:to_html) {
				<<-HTML
				<h3>subsection</h3>
				<p>This is a test diary.</p>
				<script>
					console.log("test");
				</script>
				<noscript>noscript</noscript>
				HTML
			} }
			it { expect(subject).to include('<h3>subsection</h3>') }
			it { expect(subject).not_to include('<script>') }
			it { expect(subject).to include('<noscript>noscript</noscript>') }
		end
	end

	describe "#content_proc" do
		subject { @plugin.__send__(:content_proc, 'amp', '20170302') }
		before {
			@conf.theme = 'local/default'
			allow(@plugin).to receive(:theme_paths_local) { ['theme/*'] }
			allow(@plugin).to receive(:navi_user) { '' }
			allow(@diary).to receive(:to_html) { '<h3>sample</h3>' }
		}
		it { expect(subject).to include('<html ⚡ lang="ja">') }
		it { expect(subject).to include('<h3>sample</h3>') }
	end

	describe "AMP module" do
		subject { @plugin }
		it { expect(subject.singleton_class.constants).to include(:AMP) }
		it { expect(subject.methods).to include(:add_amp_header_proc) }
		it { expect(subject.methods).to include(:amp_body_enter_proc) }
	end
end