Skip to content

Commit

Permalink
Add feature tests for resource scope-filtering, tag-filtering and pag…
Browse files Browse the repository at this point in the history
…ination
  • Loading branch information
robinboening committed Jul 7, 2021
1 parent 7b58c6e commit 20aad20
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spec/dummy/app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Event < ActiveRecord::Base

scope :starting_today, -> { where(starts_at: Time.current.at_midnight..Date.tomorrow.at_midnight) }
scope :future, -> { where("starts_at > ?", Date.tomorrow.at_midnight) }
scope :by_location_name, ->(name) { joins(:location).where(locations: { name: name }) }

def self.alchemy_resource_relations
{
Expand All @@ -22,6 +23,10 @@ def self.alchemy_resource_filters
name: :start,
values: %w(starting_today future),
},
{
name: :by_location_name,
values: Location.distinct.pluck(:name),
},
]
end

Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
en:
alchemy:
filters:
event:
start:
name: Start
by_location_name:
name: Location
element_names:
gallery_picture: Gallery picture
content_names:
Expand Down
127 changes: 127 additions & 0 deletions spec/features/admin/resources_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,133 @@
expect(page).to have_selector("div#archive_all table.list")
end

describe "pagination" do
before do
create_list(:event, 15)
end

it "should limit the number of items per page based on alchemy's general configuration" do
stub_alchemy_config(:items_per_page, 5)

visit "/admin/events"
expect(page).to have_selector("div#archive_all table.list tbody tr", count: 5)
expect(page).to have_selector("div#archive_all .pagination .page", count: 3)
end

context "params containing per_page" do
it "should limit the items per page based on the given value" do
visit "/admin/events?per_page=3"
expect(page).to have_selector("div#archive_all table.list tbody tr", count: 3)
expect(page).to have_selector("div#archive_all .pagination .page", count: 5)
end
end
end

describe "filters" do
let(:filter_count) { Event.alchemy_resource_filters.size }

context "resource model has alchemy_resource_filters defined" do
it "should show selectboxes for the filters" do
visit "/admin/events"

within "#library_sidebar #filter_bar" do
expect(page).to have_selector("select", count: filter_count)
expect(page).to have_selector("label", text: "Start")
expect(page).to have_selector("label", text: "Location")
end
end

context "selecting a filter option" do
let(:location) { create(:location, name: "berlin") }

before do
create(:event, name: "today 1", starts_at: Time.current)
create(:event, name: "today 2", starts_at: Time.current, location: location)
create(:event, name: "yesterday", starts_at: Time.current - 1.day)
end

it "should filter the list to only show matching items", js: true do
visit "/admin/events"

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 3)
expect(page).to have_content("today 1")
expect(page).to have_content("today 2")
expect(page).to have_content("yesterday")
end

within "#library_sidebar #filter_bar" do
select2("Starting today", from: "Start")
end

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 2)
expect(page).to have_content("today 1")
expect(page).to have_content("today 2")
expect(page).to_not have_content("yesterday")
end
end

it "can combine multiple filters" do
visit "/admin/events?filter[start]=starting_today&filter[by_location_name]=#{location.name}"

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 1)
expect(page).to have_content("today 2")
expect(page).to_not have_content("today 1")
end
end
end
end

context "with no tagged items" do
before do
create(:event, tag_list: nil)
end

it "should not show the tag list" do
visit "/admin/events"

within "#library_sidebar" do
expect(page).to_not have_selector(".tag-list")
end
end
end

context "with tagged items" do
before do
create_list(:event, 2, tag_list: ["remote"])
create(:event, name: "onsite event", tag_list: ["onsite"])
end

it "should list all tags including the number of tagged items" do
visit "/admin/events"

within "#library_sidebar" do
expect(page).to have_selector(".tag-list a", text: "remote (2)")
expect(page).to have_selector(".tag-list a", text: "onsite (1)")
end
end

context "selecting a tag from the list" do
it "should filter the list to only show matching items" do
visit "/admin/events"

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 3)
end

find("#library_sidebar .tag-list a", text: "onsite (1)").click

within "div#archive_all table.list tbody" do
expect(page).to have_selector("tr", count: 1)
expect(page).to have_selector("td", text: "onsite event")
end
end
end
end
end

describe "date fields" do
let(:yesterday) { Date.yesterday }
let(:tomorrow) { Date.tomorrow }
Expand Down

0 comments on commit 20aad20

Please sign in to comment.