Skip to content

Commit

Permalink
Add search functionality tests to DiaryEntriesController
Browse files Browse the repository at this point in the history
  • Loading branch information
kcne committed Sep 3, 2024
1 parent 9cfcbfd commit 734bc63
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/controllers/diary_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,47 @@ def set_map_location
end
end
end

def test_index_search
user = create(:user)
create(:diary_entry, :user => user, :title => "First Entry", :body => "This is the first diary entry.")
create(:diary_entry, :user => user, :title => "Second Entry", :body => "This is the second diary entry.")
create(:diary_entry, :user => user, :title => "Third Entry", :body => "Nothing related to the search term.")

# Test search by title
get diary_entries_path(:query => "First")
assert_response :success
assert_select "article.diary_post", 1
assert_select "h2", :text => /First Entry/

# Test search by body
get diary_entries_path(:query => "second")
assert_response :success
assert_select "article.diary_post", 1
assert_select "h2", :text => /Second Entry/

# Test no results
get diary_entries_path(:query => "Nonexistent")
assert_response :success
assert_select "h4", :text => /No entries/, :count => 1
end

def test_index_search_language
user = create(:user)
create(:language, :code => "en")
create(:language, :code => "de")

create(:diary_entry, :user => user, :title => "English Entry", :language_code => "en")
create(:diary_entry, :user => user, :title => "German Entry", :language_code => "de")

# Search within a specific language
get diary_entries_path(:query => "Entry", :language => "en")
assert_response :success
assert_select "article.diary_post", 1
assert_select "h2", :text => /English Entry/

get diary_entries_path(:query => "Entry", :language => "de")
assert_response :success
assert_select "article.diary_post", 1
assert_select "h2", :text => /German Entry/
end

0 comments on commit 734bc63

Please sign in to comment.