File: gist_tag_spec.rb

package info (click to toggle)
ruby-jekyll-gist 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: ruby: 298; sh: 14; makefile: 4
file content (175 lines) | stat: -rw-r--r-- 6,888 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require "spec_helper"

describe(Jekyll::Gist::GistTag) do
  let(:http_output) { "<test>true</test>" }
  let(:doc) { doc_with_content(content) }
  let(:content) { "{% gist #{gist} %}" }
  let(:output) do
    doc.content = content
    doc.output  = Jekyll::Renderer.new(doc.site, doc).run
  end

  before(:each) { ENV["JEKYLL_GITHUB_TOKEN"] = nil }

  context "valid gist" do
    context "with user prefix" do
      before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) }
      let(:gist) { "mattr-/24081a1d93d2898ecf0f" }

      it "produces the correct script tag" do
        expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!)
      end
      it "produces the correct noscript tag" do
        expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
      end
    end

    context "without user prefix" do
      before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) }
      let(:gist) { "28949e1d5ee2273f9fd3" }

      it "produces the correct script tag" do
        expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!)
      end
      it "produces the correct noscript tag" do
        expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
      end
    end

    context "classic Gist id style" do
      before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) }
      let(:gist) { "1234321" }

      it "produces the correct script tag" do
        expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!)
      end
      it "produces the correct noscript tag" do
        expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
      end
    end

    context "with file specified" do
      before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw/#{filename}").to_return(:body => http_output) }
      let(:gist)     { "mattr-/24081a1d93d2898ecf0f" }
      let(:filename) { "myfile.ext" }
      let(:content)  { "{% gist #{gist} #{filename} %}" }

      it "produces the correct script tag" do
        expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>!)
      end
      it "produces the correct noscript tag" do
        expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
      end
    end

    context "with variable gist id" do
      before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(:body => http_output) }
      let(:gist_id)  { "1342013" }
      let(:gist)     { "page.gist_id" }
      let(:output) do
        doc.data["gist_id"] = gist_id
        doc.content = content
        doc.output  = Jekyll::Renderer.new(doc.site, doc).run
      end

      it "produces the correct script tag" do
        expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js">\s<\/script>!)
      end
      it "produces the correct noscript tag" do
        expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
      end
    end

    context "with variable gist id and filename" do
      before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(:body => http_output) }
      let(:gist_id)       { "1342013" }
      let(:gist_filename) { "atom.xml" }
      let(:gist)          { "page.gist_id" }
      let(:filename)      { "page.gist_filename" }
      let(:content)       { "{% gist #{gist} #{filename} %}" }
      let(:output) do
        doc.data["gist_id"] = "1342013"
        doc.data["gist_filename"] = "atom.xml"
        doc.content = content
        doc.output  = Jekyll::Renderer.new(doc.site, doc).run
      end

      it "produces the correct script tag" do
        expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>!)
      end

      it "produces the correct noscript tag" do
        expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
      end
    end

    context "with valid gist id and invalid filename" do
      before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(:status => 404) }
      let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" }
      let(:gist_filename) { "myfile.ext" }
      let(:content) { "{% gist #{gist_id} #{gist_filename} %}" }

      it "produces the correct script tag" do
        expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>!)
      end

      it "does not produce the noscript tag" do
        expect(output).to_not match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
      end
    end

    context "with token" do
      before { ENV["JEKYLL_GITHUB_TOKEN"] = "1234" }
      before do
        stub_request(:get, "https://api.github.com/gists/1342013")
          .to_return(:status => 200, :body => fixture("single-file"), :headers => { "Content-Type" => "application/json" })
      end
      let(:gist_id)  { "1342013" }
      let(:gist)     { "page.gist_id" }
      let(:output) do
        doc.data["gist_id"] = gist_id
        doc.content = content
        doc.output  = Jekyll::Renderer.new(doc.site, doc).run
      end

      it "produces the noscript tag" do
        expect(output).to match(%r!<noscript><pre>contents of gist<\/pre><\/noscript>!)
      end

      context "with a filename" do
        before do
          stub_request(:get, "https://api.github.com/gists/1342013")
            .to_return(:status => 200, :body => fixture("multiple-files"), :headers => { "Content-Type" => "application/json" })
        end
        let(:content) { "{% gist 1342013 hello-world.rb %}" }

        it "produces the noscript tag" do
          expect(output).to match(%r!<noscript><pre>puts &#39;hello world&#39;<\/pre><\/noscript>!)
        end
      end
    end

    context "with noscript disabled" do
      let(:doc) { doc_with_content(content, { "gist" => { "noscript" => false } }) }
      let(:output) do
        doc.content = content
        doc.output  = Jekyll::Renderer.new(doc.site, doc).run
      end
      let(:gist) { "mattr-/24081a1d93d2898ecf0f" }

      it "does not produce the noscript tag" do
        expect(output).to_not match(%r!<noscript>!)
      end
    end
  end

  context "invalid gist" do
    context "no gist id present" do
      let(:gist) { "" }

      it "raises an error" do
        expect(-> { output }).to raise_error
      end
    end
  end
end