Skip to content

A platform for community discussion. Free, open, simple.

4.3 KB
Raw Blame
# frozen_string_literal: true

describe "Admin House Ad" do
  fab!(:admin)

  let(:house_ads_page) { PageObjects::Pages::AdminHouseAds.new }
  let(:form) { PageObjects::Components::FormKit.new(".house-ad-form") }
  let(:dialog) { PageObjects::Components::Dialog.new }
  let(:toasts) { PageObjects::Components::Toasts.new }

  context "when plugin is enabled" do
    before do
      enable_current_plugin
      sign_in(admin)
    end

    it "navigates from plugins page to house ads without routing errors" do
      page.visit "/admin/plugins"
      expect(page).to have_no_css(".alert-error")

      find("tr[data-plugin-name='discourse-adplugin'] a.admin-plugins-list__name").click
      expect(page).to have_current_path(%r{/admin/plugins/discourse-adplugin})
      expect(page).to have_no_css(".alert-error")
    end

    [false, true].each do |lazy_load_categories|
      it "lets an admin search for a category and save it with lazy loading #{lazy_load_categories ? "enabled" : "disabled"}" do
        SiteSetting.lazy_load_categories_groups =
          lazy_load_categories ? Group::AUTO_GROUPS[:admins].to_s : ""
        category = Fabricate(:category, name: "OpenPanel")
        another_category = Fabricate(:category, name: "Security summary")
        Fabricate(:category_sidebar_section_link, user: admin, linkable: category)
        house_ads_page.visit_new

        form.field("name").fill_in("Category ad")
        form.field("html").fill_in("<p>Category ad</p>")
        form.field("categories").select(category.name)
        form.submit

        expect(toasts).to have_success(I18n.t("js.saved"))
        page.refresh
        expect(form.field("categories")).to have_value(category.id.to_s)
        expect(house_ads_page).to have_selected_category(category)

        form.field("categories").select(another_category.name)
        form.submit

        expect(toasts).to have_success(I18n.t("js.saved"))
        page.refresh
        expect(house_ads_page).to have_selected_category(category)
        expect(house_ads_page).to have_selected_category(another_category)
      end
    end

    it "supports the full house ad lifecycle" do
      house_ads_page.visit_page
      expect(house_ads_page).to have_empty_state

      # Create a new ad
      house_ads_page.click_new_ad
      expect(form.field("visible_to_logged_in_users")).to be_checked
      expect(form.field("visible_to_anons")).to be_checked

      form.field("name").fill_in("My Test Ad")
      form.field("html").fill_in("<h1>hello world</h1>")
      form.field("visible_to_anons").toggle
      form.submit

      expect(toasts).to have_success(I18n.t("js.saved"))
      ad = AdPlugin::HouseAd.last
      expect(ad).to have_attributes(
        name: "My Test Ad",
        visible_to_logged_in_users: true,
        visible_to_anons: false,
      )

      # Navigate back to list, verify ad appears
      house_ads_page.click_back
      expect(house_ads_page).to have_ad_listed("My Test Ad")

      # Click into the ad, verify fields populated
      house_ads_page.click_ad("My Test Ad")
      expect(form.field("name")).to have_value("My Test Ad")
      expect(form.field("visible_to_logged_in_users")).to be_checked
      expect(form.field("visible_to_anons")).to be_unchecked

      # Edit and save
      form.field("name").fill_in("Updated Ad")
      form.submit

      expect(toasts).to have_success(I18n.t("js.saved"))
      expect(ad.reload.name).to eq("Updated Ad")

      # Navigate back, verify updated name
      house_ads_page.click_back
      expect(house_ads_page).to have_ad_listed("Updated Ad")

      # Navigate back in and delete
      house_ads_page.click_ad("Updated Ad")
      house_ads_page.click_delete

      expect(dialog).to be_open
      dialog.click_yes

      expect(house_ads_page).to have_empty_state
      expect(AdPlugin::HouseAd.exists?(ad.id)).to eq(false)
    end
  end

  context "when plugin is toggled on from the plugins list" do
    before { sign_in(admin) }

    it "shows the House Ads nav tab after enabling and clicking into the plugin" do
      page.visit "/admin/plugins"

      find(
        "tr[data-plugin-name='discourse-adplugin'] .admin-plugins-list__enabled .d-toggle-switch__checkbox-slider",
      ).click

      find("tr[data-plugin-name='discourse-adplugin'] .admin-plugins-list__name").click

      expect(page).to have_css(".admin-plugin-config-page__top-nav-item", text: "House Ads")
    end
  end
end