Welcome to mirror list, hosted at ThFree Co, Russian Federation.

optionally_search_spec.rb « concerns « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 71cf536db8960dfcbfad86e6d28ea8850c675fde (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
45
46
47
48
49
50
51
52
53
54
# 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 an option is provided' do
      it 'delegates to the search method' do
        expect(model)
          .to receive(:search)
          .with('foo', some_option: true)

        model.optionally_search('foo', some_option: true)
      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