summaryrefslogtreecommitdiff
path: root/spec/models/concerns/optionally_search_spec.rb
blob: ff4212ddf18c5263305c27e114dc198bc521141c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

require 'spec_helper'

describe OptionallySearch do
  let(:model) do
    Class.new(ActiveRecord::Base) do
      self.table_name = 'users'

      include OptionallySearch
    end
  end

  describe '.search' do
    it 'raises NotImplementedError' do
      expect { model.search('foo') }.to raise_error(NotImplementedError)
    end
  end

  describe '.optionally_search' do
    context 'when a query is given' do
      it 'delegates to the search method' do
        expect(model)
          .to receive(:search)
          .with('foo')

        model.optionally_search('foo')
      end
    end

    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