aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/autocomplete_lines_controller_spec.rb
blob: 60dff538113723b2c270883e941e83e3f03fd74e (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
67
68
69
70
71
72
73
74
75
RSpec.describe AutocompleteLinesController, type: :controller do
  login_user

  describe "GET #index" do
    let(:referential) { Referential.first }
    let(:company) { create(:company, name: 'Standard Rail') }
    let!(:line) do
      create(
        :line,
        number: '15',
        name: 'Continent Express',
        company: company
      )
    end

    let!(:line_without_company) do
      create(
        :line,
        number: '15',
        name: 'Continent Express',
        company: nil
      )
    end

    before(:each) do
      excluded_company = create(:company, name: 'excluded company')
      create(
        :line,
        number: 'different',
        name: 'other',
        company: excluded_company
      )
    end

    it "filters by `number`" do
      get :index,
        referential_id: referential.id,
        q: '15'

      expect(assigns(:lines).order(:id)).to eq([line, line_without_company])
    end

    it "filters by `name`" do
      get :index,
        referential_id: referential.id,
        q: 'Continent'

      expect(assigns(:lines).order(:id)).to eq([line, line_without_company])
    end

    it "escapes the query" do
      get :index,
        referential_id: referential.id,
        q: 'Continent%'

      expect(assigns(:lines).order(:id)).to be_empty
    end

    it "filters by company `name`" do
      get :index,
        referential_id: referential.id,
        q: 'standard'

      expect(assigns(:lines).to_a).to eq([line])
    end

    it "doesn't error when no `q` is sent" do
      get :index,
        referential_id: referential.id

      expect(assigns(:lines).to_a).to be_empty
      expect(response).to be_success
    end
  end
end