File: metadata_drop_spec.rb

package info (click to toggle)
ruby-jekyll-github-metadata 2.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ruby: 2,355; javascript: 107; sh: 41; makefile: 6
file content (104 lines) | stat: -rw-r--r-- 2,912 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe(Jekyll::GitHubMetadata::MetadataDrop) do
  extend IntegrationHelper

  let(:overrides) { { "repository" => "jekyll/another-repo" } }
  let(:config) { Jekyll::Configuration.from(overrides) }
  let(:site) { Jekyll::Site.new config }
  subject { described_class.new(site) }
  before { stub_all_api_requests }

  context "in Liquid" do
    before(:each) do
      site.config.delete("repository")
      site.config["github"] = subject
    end

    it "renders as a pretty JSON object in Liquid" do
      require "json"
      payload = site.site_payload
      expect(payload["site"]["github"]).to be_instance_of(described_class)
      expect(
        Liquid::Template.parse("{{ site.github }}").render!(
          payload, :registers => { :site => site }
        )
      ).to eql(JSON.pretty_generate(subject.to_h))
    end
  end

  context "payload" do
    let!(:payload) { subject.to_h }

    expected_values.each do |key, value|
      it "contains the #{key} key" do
        expect(payload).to have_key(key)
      end

      it "contains the correct value for #{key}" do
        if value.is_a? Regexp
          expect(payload[key].to_s).to match value
        else
          expect(payload[key]).to eql value
        end
      end
    end

    # Test to ensure we're testing all the values in the drop payload
    # If this test fails, you likely need to update a value in spec_helper.rb
    it "validates all values" do
      expect(payload.keys).to match_array(expected_values.keys)
    end
  end

  context "returning values" do
    context "native methods" do
      it "returns a value via #[]" do
        expect(subject["url"]).to eql("http://jekyll.github.io/github-metadata")
      end

      it "returns a value via #invoke_drop" do
        expect(subject.invoke_drop("url")).to eql("http://jekyll.github.io/github-metadata")
      end

      it "responds to #key?" do
        expect(subject.key?("url")).to be_truthy
      end
    end

    context "with mutated values" do
      before { subject["url"] = "foo" }

      it "returns the mutated value via #[]" do
        expect(subject["url"]).to eql("foo")
      end

      it "returns the mutated  via #invoke_drop" do
        expect(subject.invoke_drop("url")).to eql("foo")
      end

      it "responds to #key?" do
        expect(subject.key?("url")).to be_truthy
      end
    end

    context "with fallback data" do
      let(:fallback_data) { { "foo" => "bar" } }
      before { subject.instance_variable_set("@fallback_data", fallback_data) }

      it "returns the mutated value via #[]" do
        expect(subject["foo"]).to eql("bar")
      end

      it "returns the mutated  via #invoke_drop" do
        expect(subject.invoke_drop("foo")).to eql("bar")
      end

      it "responds to #key?" do
        expect(subject.key?("foo")).to be_truthy
      end
    end
  end
end