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: 8067ad503222f7ed81f8ed59b72382e7b7e72a35 (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
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe OptionallySearch do
  describe '.search' do
    let(:model) do
      Class.new do
        include OptionallySearch
      end
    end

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

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

        include OptionallySearch

        def self.search(query, **options)
          [query, options]
        end
      end
    end

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

        expect(model.optionally_search('foo')).to eq(['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)
          .and_call_original

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