2018-07-30 11:45:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe OptionallySearch do
|
2020-03-24 11:08:44 -04:00
|
|
|
describe '.search' do
|
|
|
|
let(:model) do
|
|
|
|
Class.new do
|
|
|
|
include OptionallySearch
|
|
|
|
end
|
2018-07-30 11:45:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises NotImplementedError' do
|
|
|
|
expect { model.search('foo') }.to raise_error(NotImplementedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.optionally_search' do
|
2020-03-24 11:08:44 -04:00
|
|
|
let(:model) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = 'users'
|
|
|
|
|
|
|
|
include OptionallySearch
|
|
|
|
|
|
|
|
def self.search(query, **options)
|
|
|
|
[query, options]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-30 11:45:49 -04:00
|
|
|
context 'when a query is given' do
|
|
|
|
it 'delegates to the search method' do
|
|
|
|
expect(model)
|
|
|
|
.to receive(:search)
|
2020-10-23 14:08:31 -04:00
|
|
|
.with('foo')
|
2020-03-24 11:08:44 -04:00
|
|
|
.and_call_original
|
2018-07-30 11:45:49 -04:00
|
|
|
|
2020-03-24 11:08:44 -04:00
|
|
|
expect(model.optionally_search('foo')).to eq(['foo', {}])
|
2018-07-30 11:45:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
context 'when an option is provided' do
|
|
|
|
it 'delegates to the search method' do
|
|
|
|
expect(model)
|
|
|
|
.to receive(:search)
|
|
|
|
.with('foo', some_option: true)
|
2020-03-24 11:08:44 -04:00
|
|
|
.and_call_original
|
2020-03-23 08:09:47 -04:00
|
|
|
|
2020-03-24 11:08:44 -04:00
|
|
|
expect(model.optionally_search('foo', some_option: true)).to eq(['foo', { some_option: true }])
|
2020-03-23 08:09:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-30 11:45:49 -04:00
|
|
|
context 'when no query is given' do
|
|
|
|
it 'returns the current relation' do
|
|
|
|
expect(model.optionally_search).to be_a_kind_of(ActiveRecord::Relation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an empty query is given' do
|
|
|
|
it 'returns the current relation' do
|
|
|
|
expect(model.optionally_search(''))
|
|
|
|
.to be_a_kind_of(ActiveRecord::Relation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|