Files
growstuff/spec/requests/api/v1/crops_search_spec.rb
Daniel O'Connor 5ada7e7f77 Add crops search API endpoint (#4622)
* Add crops search API endpoint

- Added GET /api/v1/crops/search endpoint.
- Updated CropSearchService to support additional search options.
- Manually updated Swagger documentation in swagger/v1/swagger.json.
- Added request specs to verify the new endpoint.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* Add crops search API endpoint

- Added GET /api/v1/crops/search endpoint.
- Updated CropSearchService to support additional search options.
- Manually updated Swagger documentation in swagger/v1/swagger.json.
- Added request specs to verify the new endpoint.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-05-04 18:12:36 +09:30

36 lines
1.2 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Crops Search' do
subject { JSON.parse response.body }
let(:headers) { { 'Accept' => 'application/vnd.api+json' } }
let!(:cabbage) { create(:crop, name: 'Cabbage', approval_status: 'approved') }
let!(:apple) { create(:crop, name: 'Apple', approval_status: 'approved') }
describe 'GET /api/v1/crops/search' do
before do
Crop.reindex
end
it 'returns crops matching the search term' do
get '/api/v1/crops/search', params: { term: 'Cabbage' }, headers: headers
expect(response).to have_http_status(:ok)
expect(subject['data'].size).to eq(1)
expect(subject['data'].first['attributes']['name']).to eq('Cabbage')
end
it 'returns empty data if no crops match' do
get '/api/v1/crops/search', params: { term: 'NonExistent' }, headers: headers
expect(response).to have_http_status(:ok)
expect(subject['data']).to be_empty
end
it 'includes meta information' do
get '/api/v1/crops/search', params: { term: 'Cabbage' }, headers: headers
expect(subject['meta']).to include('record_count' => 1, 'page_count' => 1)
end
end
end