# frozen_string_literal: true require "rails_helper" describe AdPlugin::HouseAd do let(:valid_attrs) do { name: "Find A Mechanic", html: '
Find A Mechanic!
', } end def create_anon_ad AdPlugin::HouseAd.create( name: "anon-ad", html: "
ANON
", visible_to_logged_in_users: false, visible_to_anons: true, ) end def create_logged_in_ad AdPlugin::HouseAd.create( name: "logged-in-ad", html: "
LOGGED IN
", visible_to_logged_in_users: true, visible_to_anons: false, ) end describe ".find" do let!(:ad) { AdPlugin::HouseAd.create(valid_attrs) } it "returns nil if no match" do expect(AdPlugin::HouseAd.find(100)).to be_nil end it "can retrieve by id" do r = AdPlugin::HouseAd.find(ad.id) expect(r&.name).to eq(valid_attrs[:name]) expect(r&.html).to eq(valid_attrs[:html]) end end describe ".all" do it "returns empty array if no records" do expect(AdPlugin::HouseAd.all).to eq([]) end it "returns an array of records" do AdPlugin::HouseAd.create(valid_attrs) AdPlugin::HouseAd.create(valid_attrs.merge(name: "Ad 2", html: "
Ad 2 Here
")) all = AdPlugin::HouseAd.all expect(all.size).to eq(2) expect(all.map(&:name)).to contain_exactly("Ad 2", valid_attrs[:name]) expect(all.map(&:html)).to contain_exactly("
Ad 2 Here
", valid_attrs[:html]) end end describe ".all_for_anons" do let!(:anon_ad) { create_anon_ad } let!(:logged_in_ad) { create_logged_in_ad } it "doesn't include ads for logged in users" do expect(AdPlugin::HouseAd.all_for_anons.map(&:id)).to contain_exactly(anon_ad.id) end end describe ".all_for_logged_in_users" do let!(:anon_ad) { create_anon_ad } let!(:logged_in_ad) { create_logged_in_ad } it "doesn't include ads for anonymous users" do expect(AdPlugin::HouseAd.all_for_logged_in_users.map(&:id)).to contain_exactly( logged_in_ad.id, ) end end describe "#save" do it "assigns an id and attrs for new record" do ad = AdPlugin::HouseAd.from_hash(valid_attrs) expect(ad.save).to eq(true) expect(ad.name).to eq(valid_attrs[:name]) expect(ad.html).to eq(valid_attrs[:html]) expect(ad.id.to_i > 0).to eq(true) ad2 = AdPlugin::HouseAd.from_hash(valid_attrs.merge(name: "Find Another Mechanic")) expect(ad2.save).to eq(true) expect(ad2.id).to_not eq(ad.id) end it "updates existing record" do ad = AdPlugin::HouseAd.create(valid_attrs) id = ad.id ad.name = "Sell Your Car" ad.html = '
Sell Your Car!
' expect(ad.save).to eq(true) ad = AdPlugin::HouseAd.find(id) expect(ad.name).to eq("Sell Your Car") expect(ad.html).to eq('
Sell Your Car!
') expect(ad).to be_valid end describe "errors" do it "blank name" do ad = AdPlugin::HouseAd.from_hash(valid_attrs.merge(name: "")) expect(ad.save).to eq(false) expect(ad).to_not be_valid expect(ad.errors.full_messages).to be_present expect(ad.errors[:name]).to be_present expect(ad.errors.count).to eq(1) end it "duplicate name" do existing = AdPlugin::HouseAd.create(valid_attrs) ad = AdPlugin::HouseAd.from_hash(valid_attrs) expect(ad.save).to eq(false) expect(ad).to_not be_valid expect(ad.errors.full_messages).to be_present expect(ad.errors[:name]).to be_present expect(ad.errors.count).to eq(1) end it "duplicate name, different case" do existing = AdPlugin::HouseAd.create(valid_attrs.merge(name: "mechanic")) ad = AdPlugin::HouseAd.create(valid_attrs.merge(name: "Mechanic")) expect(ad.save).to eq(false) expect(ad).to_not be_valid expect(ad.errors[:name]).to be_present expect(ad.errors.count).to eq(1) end it "blank html" do ad = AdPlugin::HouseAd.from_hash(valid_attrs.merge(html: "")) expect(ad.save).to eq(false) expect(ad).to_not be_valid expect(ad.errors.full_messages).to be_present expect(ad.errors[:html]).to be_present expect(ad.errors.count).to eq(1) end it "invalid name" do ad = AdPlugin::HouseAd.from_hash(valid_attrs.merge(name: "