<commit_before># encoding: utf-8
require "spec_helper"

def expect_window_with_content(content, window: windows.last)
  page.within_window window do
    expect(page).to have_content(content)
  end
end

def expect_window_without_content(content, window: windows.last)
  page.within_window window do
    expect(page).not_to have_content(content)
  end
end

def switch_page_form_locale(locale)
  within "#switch_locale_picker" do
    find("a", text: locale.upcase).click
  end

  # make sure that the locale change has taken effect
  expect(page).to have_selector("#switch_locale_picker li.selected a##{locale.downcase}")
end


module Refinery
  module Admin
    describe "Pages", :type => :feature do
      refinery_login

      context "when no pages" do
        it "invites to create one" do
          visit refinery.admin_pages_path
          expect(page).to have_content(%q{There are no pages yet. Click "Add new page" to add your first page.})
        end
      end

      describe "action links" do
        it "shows add new page link" do
          visit refinery.admin_pages_path

          within "#actions" do
            expect(page).to have_content("Add new page")
            expect(page).to have_selector("a[href='/#{Refinery::Core.backend_route}/pages/new']")
          end
        end

        context "when no pages" do

          it "doesn't show reorder pages link" do
            visit refinery.admin_pages_path

            within "#actions" do
              expect(page).to have_no_content("Reorder pages")
              expect(page).to have_no_selector("a[href='/#{Refinery::Core.backend_route}/pages']")
            end
          end
        end

        context "when some pages exist" do
          let!(:page_0) { Page.create! title: 'Page 0' }
          let!(:page_1) { Page.create! title: 'Page 1' }

          it "shows reorder pages link" do
            visit refinery.admin_pages_path

            within "#actions" do
              expect(page).to have_content("Reorder pages")
              expect(page).to have_selector("a[href='/#{Refinery::Core.backend_route}/pages']")
            end
          end

          context "when reordering pages" do
            let(:reorder_url) { "/#{Refinery::Core.backend_route}/pages/update_positions" }
            before do
              visit refinery.admin_pages_path
              page.driver.post(reorder_url, {
                'ul[0][0][id]' => "page_#{page_1.id}",
                'ul[0][1][id]' => "page_#{page_0.id}",
              })
            end

            it "shows pages in the new order" do
              visit refinery.admin_pages_path
              expect(page.body).to match(/Page 1.*Page 0/m)
            end
          end
        end

        context "when sub pages exist" do
          let!(:company) { Page.create :title => 'Our Company' }
          let!(:team) { company.children.create :title => 'Our Team' }
          let!(:locations) { company.children.create :title => 'Our Locations' }
          let!(:location) { locations.children.create :title => 'New York' }

          context "with auto expand option turned off" do
            before do
              allow(Refinery::Pages).to receive(:auto_expand_admin_tree).and_return(false)

              visit refinery.admin_pages_path
            end

            it "show parent page" do

              expect(page).to have_content(company.title)
            end

            it "doesn't show children" do
              expect(page).not_to have_content(team.title)
              expect(page).not_to have_content(locations.title)
            end

            it "expands children", js: true do
              find("#page_#{company.id} .title.toggle").click

              expect(page).to have_content(team.title)
              expect(page).to have_content(locations.title)
            end

            it "expands children when nested multiple levels deep", js: true do
              find("#page_#{company.id} .title.toggle").click
              find("#page_#{locations.id} .title.toggle").click

              expect(page).to have_content("New York")
            end
          end

          context "with auto expand option turned on" do
            before do
              allow(Refinery::Pages).to receive(:auto_expand_admin_tree).and_return(true)
              Rails.cache.clear

              visit refinery.admin_pages_path
            end

            it "shows children" do
              expect(page).to have_content(team.title)
              expect(page).to have_content(locations.title)
            end

            it "refreshes the cache if sub_children change" do
              expect(page).to have_content(location.title)
              location.update(title: 'Las Vegas')
              visit refinery.admin_pages_path
              expect(page).to have_content(location.title)
            end
          end
        end
      end

      describe "new/create" do
        it "Creates a page", js:true do
          visit refinery.admin_pages_path

          find('a', text: 'Add new page').click

          fill_in "Title", :with => "My first page"
          expect { click_button "Save" }.to change(Refinery::Page, :count).from(0).to(1)<commit_msg>Fix flaky test in "refreshes the cache if sub_children change"<commit_after># encoding: utf-8
require "spec_helper"

def expect_window_with_content(content, window: windows.last)
  page.within_window window do
    expect(page).to have_content(content)
  end
end

def expect_window_without_content(content, window: windows.last)
  page.within_window window do
    expect(page).not_to have_content(content)
  end
end

def switch_page_form_locale(locale)
  within "#switch_locale_picker" do
    find("a", text: locale.upcase).click
  end

  # make sure that the locale change has taken effect
  expect(page).to have_selector("#switch_locale_picker li.selected a##{locale.downcase}")
end


module Refinery
  module Admin
    describe "Pages", :type => :feature do
      refinery_login

      context "when no pages" do
        it "invites to create one" do
          visit refinery.admin_pages_path
          expect(page).to have_content(%q{There are no pages yet. Click "Add new page" to add your first page.})
        end
      end

      describe "action links" do
        it "shows add new page link" do
          visit refinery.admin_pages_path

          within "#actions" do
            expect(page).to have_content("Add new page")
            expect(page).to have_selector("a[href='/#{Refinery::Core.backend_route}/pages/new']")
          end
        end

        context "when no pages" do

          it "doesn't show reorder pages link" do
            visit refinery.admin_pages_path

            within "#actions" do
              expect(page).to have_no_content("Reorder pages")
              expect(page).to have_no_selector("a[href='/#{Refinery::Core.backend_route}/pages']")
            end
          end
        end

        context "when some pages exist" do
          let!(:page_0) { Page.create! title: 'Page 0' }
          let!(:page_1) { Page.create! title: 'Page 1' }

          it "shows reorder pages link" do
            visit refinery.admin_pages_path

            within "#actions" do
              expect(page).to have_content("Reorder pages")
              expect(page).to have_selector("a[href='/#{Refinery::Core.backend_route}/pages']")
            end
          end

          context "when reordering pages" do
            let(:reorder_url) { "/#{Refinery::Core.backend_route}/pages/update_positions" }
            before do
              visit refinery.admin_pages_path
              page.driver.post(reorder_url, {
                'ul[0][0][id]' => "page_#{page_1.id}",
                'ul[0][1][id]' => "page_#{page_0.id}",
              })
            end

            it "shows pages in the new order" do
              visit refinery.admin_pages_path
              expect(page.body).to match(/Page 1.*Page 0/m)
            end
          end
        end

        context "when sub pages exist" do
          let!(:company) { Page.create :title => 'Our Company' }
          let!(:team) { company.children.create :title => 'Our Team' }
          let!(:locations) { company.children.create :title => 'Our Locations' }
          let!(:location) { locations.children.create :title => 'New York' }

          context "with auto expand option turned off" do
            before do
              allow(Refinery::Pages).to receive(:auto_expand_admin_tree).and_return(false)

              visit refinery.admin_pages_path
            end

            it "show parent page" do

              expect(page).to have_content(company.title)
            end

            it "doesn't show children" do
              expect(page).not_to have_content(team.title)
              expect(page).not_to have_content(locations.title)
            end

            it "expands children", js: true do
              find("#page_#{company.id} .title.toggle").click

              expect(page).to have_content(team.title)
              expect(page).to have_content(locations.title)
            end

            it "expands children when nested multiple levels deep", js: true do
              find("#page_#{company.id} .title.toggle").click
              find("#page_#{locations.id} .title.toggle").click

              expect(page).to have_content("New York")
            end
          end

          context "with auto expand option turned on" do
            before do
              allow(Refinery::Pages).to receive(:auto_expand_admin_tree).and_return(true)
              Rails.cache.clear

              visit refinery.admin_pages_path
            end

            it "shows children" do
              expect(page).to have_content(team.title)
              expect(page).to have_content(locations.title)
            end

            it "refreshes the cache if sub_children change" do
              expect(page).to have_content(location.title)

              find("#page_#{location.id} .edit_icon").click
              fill_in "Title", with: 'Las Vegas'
              click_button "Save"

              expect(page).to have_content('Las Vegas')
            end
          end
        end
      end

      describe "new/create" do
        it "Creates a page", js: true do
          visit refinery.admin_pages_path

          find('a', text: 'Add new page').click

          fill_in "Title", :with => "My first page"
          expect { click_button "Save" }.to change(Refinery::Page, :count).from(0).to(1)
{'fraction_non_alphanumeric': 0.1023030072325847, 'fraction_numerical': 0.002854967643700038, 'mean_word_length': 2.0060068649885583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6338363', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2881, 'n_words': 760}