issues_resolver_spec.rb 4.46 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe Resolvers::IssuesResolver do
  include GraphqlHelpers

  let(:current_user) { create(:user) }
7

8 9 10 11 12 13 14 15 16 17 18 19
  context "with a project" do
    set(:project) { create(:project) }
    set(:issue1) { create(:issue, project: project, state: :opened, created_at: 3.hours.ago, updated_at: 3.hours.ago) }
    set(:issue2) { create(:issue, project: project, state: :closed, title: 'foo', created_at: 1.hour.ago, updated_at: 1.hour.ago, closed_at: 1.hour.ago) }
    set(:label1) { create(:label, project: project) }
    set(:label2) { create(:label, project: project) }

    before do
      project.add_developer(current_user)
      create(:label_link, label: label1, target: issue1)
      create(:label_link, label: label1, target: issue2)
      create(:label_link, label: label2, target: issue2)
20 21
    end

22 23 24 25
    describe '#resolve' do
      it 'finds all issues' do
        expect(resolve_issues).to contain_exactly(issue1, issue2)
      end
26

27 28 29
      it 'filters by state' do
        expect(resolve_issues(state: 'opened')).to contain_exactly(issue1)
        expect(resolve_issues(state: 'closed')).to contain_exactly(issue2)
30 31
      end

32 33 34
      it 'filters by labels' do
        expect(resolve_issues(label_name: [label1.title])).to contain_exactly(issue1, issue2)
        expect(resolve_issues(label_name: [label1.title, label2.title])).to contain_exactly(issue2)
35 36
      end

37 38 39 40 41 42 43 44
      describe 'filters by created_at' do
        it 'filters by created_before' do
          expect(resolve_issues(created_before: 2.hours.ago)).to contain_exactly(issue1)
        end

        it 'filters by created_after' do
          expect(resolve_issues(created_after: 2.hours.ago)).to contain_exactly(issue2)
        end
45 46
      end

47 48 49 50 51 52 53 54
      describe 'filters by updated_at' do
        it 'filters by updated_before' do
          expect(resolve_issues(updated_before: 2.hours.ago)).to contain_exactly(issue1)
        end

        it 'filters by updated_after' do
          expect(resolve_issues(updated_after: 2.hours.ago)).to contain_exactly(issue2)
        end
55 56
      end

57 58
      describe 'filters by closed_at' do
        let!(:issue3) { create(:issue, project: project, state: :closed, closed_at: 3.hours.ago) }
59

60 61 62 63 64 65 66
        it 'filters by closed_before' do
          expect(resolve_issues(closed_before: 2.hours.ago)).to contain_exactly(issue3)
        end

        it 'filters by closed_after' do
          expect(resolve_issues(closed_after: 2.hours.ago)).to contain_exactly(issue2)
        end
67 68
      end

69 70
      it 'searches issues' do
        expect(resolve_issues(search: 'foo')).to contain_exactly(issue2)
71
      end
72

73 74 75
      it 'sort issues' do
        expect(resolve_issues(sort: 'created_desc')).to eq [issue2, issue1]
      end
76

77 78
      it 'returns issues user can see' do
        project.add_guest(current_user)
79

80
        create(:issue, confidential: true)
81

82 83
        expect(resolve_issues).to contain_exactly(issue1, issue2)
      end
84

85 86 87
      it 'finds a specific issue with iid' do
        expect(resolve_issues(iid: issue1.iid)).to contain_exactly(issue1)
      end
88

89 90 91
      it 'finds a specific issue with iids' do
        expect(resolve_issues(iids: issue1.iid)).to contain_exactly(issue1)
      end
92

93 94 95 96
      it 'finds multiple issues with iids' do
        expect(resolve_issues(iids: [issue1.iid, issue2.iid]))
          .to contain_exactly(issue1, issue2)
      end
97

98 99 100 101 102 103 104
      it 'finds only the issues within the project we are looking at' do
        another_project = create(:project)
        iids = [issue1, issue2].map(&:iid)

        iids.each do |iid|
          create(:issue, project: another_project, iid: iid)
        end
105

106 107 108 109
        expect(resolve_issues(iids: iids)).to contain_exactly(issue1, issue2)
      end
    end
  end
110

111 112 113 114
  context "when passing a non existent, batch loaded project" do
    let(:project) do
      BatchLoader.for("non-existent-path").batch do |_fake_paths, loader, _|
        loader.call("non-existent-path", nil)
115
      end
116
    end
117

118 119
    it "returns nil without breaking" do
      expect(resolve_issues(iids: ["don't", "break"])).to be_empty
120
    end
121 122
  end

123 124 125 126 127 128 129
  it 'increases field complexity based on arguments' do
    field = Types::BaseField.new(name: 'test', type: GraphQL::STRING_TYPE, resolver_class: described_class, null: false, max_page_size: 100)

    expect(field.to_graphql.complexity.call({}, {}, 1)).to eq 4
    expect(field.to_graphql.complexity.call({}, { labelName: 'foo' }, 1)).to eq 8
  end

130 131 132 133
  def resolve_issues(args = {}, context = { current_user: current_user })
    resolve(described_class, obj: project, args: args, ctx: context)
  end
end