summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/zentao/query_spec.rb
blob: f7495e640c32e2a9042ed12aa6881d323443c10d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Zentao::Query do
  let(:zentao_integration) { create(:zentao_integration) }
  let(:params) { {} }

  subject(:query) { described_class.new(zentao_integration, ActionController::Parameters.new(params)) }

  describe '#issues' do
    let(:response) { { 'page' => 1, 'total' => 0, 'limit' => 20, 'issues' => [] } }

    def expect_query_option_include(expected_params)
      expect_next_instance_of(Gitlab::Zentao::Client) do |client|
        expect(client).to receive(:fetch_issues)
          .with(hash_including(expected_params))
          .and_return(response)
      end

      query.issues
    end

    context 'when params are empty' do
      it 'fills default params' do
        expect_query_option_include(status: 'opened', order: 'lastEditedDate_desc', labels: '')
      end
    end

    context 'when params contain valid options' do
      let(:params) { { state: 'closed', sort: 'created_asc', labels: %w[Bugs Features] } }

      it 'fills params with standard of ZenTao' do
        expect_query_option_include(status: 'closed', order: 'openedDate_asc', labels: 'Bugs,Features')
      end
    end

    context 'when params contain invalid options' do
      let(:params) { { state: 'xxx', sort: 'xxx', labels: %w[xxx] } }

      it 'fills default params with standard of ZenTao' do
        expect_query_option_include(status: 'opened', order: 'lastEditedDate_desc', labels: 'xxx')
      end
    end
  end

  describe '#issue' do
    let(:response) { { 'issue' => { 'id' => 'story-1' } } }

    before do
      expect_next_instance_of(Gitlab::Zentao::Client) do |client|
        expect(client).to receive(:fetch_issue)
          .and_return(response)
      end
    end

    it 'returns issue object by client' do
      expect(query.issue).to include('id' => 'story-1')
    end
  end
end