Fix request specs

This commit is contained in:
Eugene Burmakin 2025-09-02 23:12:10 +02:00
parent 71bb224524
commit d4c0eaa549
2 changed files with 64 additions and 28 deletions

View file

@ -52,7 +52,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: search_query }, headers: headers
expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body)
expect(json_response['query']).to eq(search_query)
expect(json_response['locations']).to be_an(Array)
@ -150,7 +150,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: 'NonexistentPlace' }, headers: headers
expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body)
expect(json_response['locations']).to be_empty
expect(json_response['total_locations']).to eq(0)
@ -162,9 +162,9 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', headers: headers
expect(response).to have_http_status(:bad_request)
json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search query parameter (q) is required')
expect(json_response['error']).to eq('Search query parameter (q) or coordinates (lat, lon) are required')
end
end
@ -173,9 +173,9 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: ' ' }, headers: headers
expect(response).to have_http_status(:bad_request)
json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search query parameter (q) is required')
expect(json_response['error']).to eq('Search query parameter (q) or coordinates (lat, lon) are required')
end
end
@ -186,7 +186,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: long_query }, headers: headers
expect(response).to have_http_status(:bad_request)
json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search query too long (max 200 characters)')
end
@ -202,7 +202,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: 'test' }, headers: headers
expect(response).to have_http_status(:internal_server_error)
json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search failed. Please try again.')
end
@ -283,11 +283,11 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: 'Kaufland' }, headers: headers
expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_an(Array)
expect(json_response['suggestions'].length).to eq(2)
first_suggestion = json_response['suggestions'].first
expect(first_suggestion).to include(
'name' => 'Kaufland Mitte',
@ -307,7 +307,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
type: 'place'
}
end
allow_any_instance_of(LocationSearch::GeocodingService)
.to receive(:search).and_return(large_suggestions)
@ -323,7 +323,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: 'a' }, headers: headers
expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_empty
end
@ -334,7 +334,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: '' }, headers: headers
expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_empty
end
@ -350,7 +350,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: 'test' }, headers: headers
expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_empty
end
@ -365,4 +365,4 @@ RSpec.describe Api::V1::LocationsController, type: :request do
end
end
end
end
end

View file

@ -6,10 +6,11 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
let(:user) { create(:user) }
before do
driven_by(:selenium_headless_chrome)
sign_in user
driven_by(:selenium_chrome_headless)
# Alternative for debugging: driven_by(:rack_test)
login_as user, scope: :user
# Create some test points near Berlin
# Create some test points near Berlin with proper PostGIS point format
create(:point,
user: user,
latitude: 52.5200,
@ -27,6 +28,9 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
city: 'Berlin',
country: 'Germany'
)
# Ensure points are properly saved and accessible
expect(user.points.count).to eq(2)
# Mock the geocoding service to avoid external API calls
allow_any_instance_of(LocationSearch::GeocodingService).to receive(:search) do |_service, query|
@ -59,11 +63,23 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
describe 'Search Bar' do
before do
visit map_path
visit '/map'
# Wait for map to load
expect(page).to have_css('#map')
sleep(2) # Give time for JavaScript to initialize
# Debug: check what's actually on the page
puts "Page title: #{page.title}"
puts "Page body contains 'Map': #{page.has_content?('Map')}"
puts "Page has #map element: #{page.has_css?('#map')}"
puts "Page HTML preview: #{page.html[0..500]}..."
# Check if map container exists
if page.has_css?('#map')
puts "Map element found!"
else
puts "Map element NOT found - checking for any div elements:"
puts "Number of div elements: #{page.all('div').count}"
end
sleep(3) # Give time for JavaScript to initialize
end
it 'displays search toggle button on the map' do
@ -282,7 +298,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
describe 'Search API Integration' do
it 'makes authenticated requests to the search API' do
# Test that the frontend makes proper API calls
visit map_path
visit '/map'
fill_in 'location-search-input', with: 'Kaufland'
@ -298,7 +314,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
describe 'Real-world Search Scenarios' do
context 'with business name search' do
it 'finds visits to business locations' do
visit map_path
visit '/map'
fill_in 'location-search-input', with: 'Kaufland'
click_button '🔍'
@ -310,7 +326,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
context 'with address search' do
it 'handles street address searches' do
visit map_path
visit '/map'
fill_in 'location-search-input', with: 'Alexanderplatz 1'
click_button '🔍'
@ -323,7 +339,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
context 'with multiple search terms' do
it 'handles complex search queries' do
visit map_path
visit '/map'
fill_in 'location-search-input', with: 'Kaufland Berlin'
click_button '🔍'
@ -448,8 +464,28 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
def sign_in(user)
visit '/users/sign_in'
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
# Try different selectors for email field
if page.has_field?('Email')
fill_in 'Email', with: user.email
elsif page.has_field?('user_email')
fill_in 'user_email', with: user.email
elsif page.has_css('input[type="email"]')
find('input[type="email"]').fill_in with: user.email
else
raise "Could not find email field"
end
# Try different selectors for password field
if page.has_field?('Password')
fill_in 'Password', with: user.password
elsif page.has_field?('user_password')
fill_in 'user_password', with: user.password
elsif page.has_css('input[type="password"]')
find('input[type="password"]').fill_in with: user.password
else
raise "Could not find password field"
end
click_button 'Log in'
end
end