# -*- encoding : utf-8 -*-

RSpec.describe Card::Set::All::Follow do
  def follow_view card_name
    render_card :follow_link, name: card_name
  end

  describe "follower_ids" do
    subject(:followers) { Card[cardname].follower_names.sort }

    context "when a new +*follow rule created" do
      it "contains id of a new follower" do
        Card::Auth.as_bot do
          Card["Joe User"].follow "No One Sees Me"
          expect(Card["No One Sees Me"].follower_ids)
            .to eq ::Set.new([Card["Joe User"].id])
        end
      end
    end

    context "when not followed" do
      let(:cardname) { "No One Sees Me" }

      it { expect(followers).to eq([]) }
    end

    context "when only field is followed" do
      let(:cardname) { "Magnifier" }

      it { expect(followers).to eq([]) }
    end

    context "when followed as a field" do
      let(:cardname) { "Magnifier+lens" }

      it { expect(followers).to eq ["Big Brother", "Narcissist"] }
    end

    context "with many followers" do
      let(:cardname) { "Sunglasses" }

      it do
        expect(followers).to eq ["Big Brother", "Narcissist", "Optic fan", "Sara",
                                 "Sunglasses fan"]
      end
    end

    context "with parent with many followers" do
      let(:cardname) { "Sunglasses+tint" }

      it do
        expect(followers).to eq ["Big Brother", "Narcissist", "Optic fan", "Sara",
                                 "Sunglasses fan"]
      end
    end

    context "when Google glass" do
      let(:cardname) { "Google glass" }

      it { expect(followers).to eq ["Big Brother", "Optic fan", "Sara"] }
    end
  end

  # doesn't exist anymore
  # didn't check if it can be adjusted to knew follow ui
  xdescribe "view: follow_link" do
    before do
      Card::Auth.signin "Big Brother"
    end

    def assert_following_view name, args
      assert_follow_view name,
                         args.reverse_merge(following: true, text: "unfollow")
    end

    #  href="/card/update/Home+*self+philipp+*follow?" \
    #       "card%5Bcontent%5D=%5B%5Bnever%5D%5D&" \
    #       "success%5Bid%5D=Home&success%5Bview%5D=follow"
    def assert_follow_view name, args
      args[:user] ||= "Big_Brother"
      #      href = "/card/update/#{args[:add_set].to_name.url_key}+" \
      #             "#{args[:user]}+*follow?"
      #      href += CGI.escape("card[content]") + '='
      #      href +=
      #        if args[:following]
      #          link_class = "follow-toggle-off"
      #          CGI.escape("[[*never]]")
      #        else
      #          link_class = "follow-toggle-on"
      #          CGI.escape("[[*always]]")
      #        end

      assert_view_select follow_view(name), "a[class~=follow-link][href*='']",
                         args[:text] || "follow"
    end

    context "when not following" do
      it "renders follow link" do
        assert_follow_view "No One Sees Me", add_set: "No One Sees Me+*self"
      end
    end

    context "when following *self" do
      it "renders following link" do
        assert_following_view "Look At Me", add_set: "Look at me+*self"
      end
    end

    context "when following *type" do
      it "renders following link" do
        assert_following_view "Sunglasses", add_set: "Sunglasses+*self"
      end
    end

    context "when following cardtype card" do
      it "renders following all link" do
        assert_following_view "Optic", add_set: "Optic+*type", text: "unfollow"
      end
    end

    context "when not following cardtype card" do
      it "renders 'follow all' link" do
        assert_follow_view "RichText", add_set: "RichText+*type", text: "follow"
      end
    end

    context "when following *right" do
      it "renders following link" do
        assert_following_view "Magnifier+lens", add_set: "Magnifier+lens+*self"
      end
    end

    context "when following content I created" do
      before { Card::Auth.signin "Narcissist" }

      it "renders following link" do
        assert_following_view "Sunglasses", add_set: "Sunglasses+*self",
                                            user: "Narcissist"
      end
    end

    context "when following content I edited" do
      before { Card::Auth.signin "Narcissist" }

      it "renders following link" do
        assert_following_view "Magnifier+lens",
                              add_set: "Magnifier+lens+*self",
                              user: "Narcissist"
      end
    end
  end
end
