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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
# frozen_string_literal: true
RSpec.shared_examples 'a GraphQL type with labels' do
it 'has label fields' do
expected_fields = %w[label labels]
expect(described_class).to include_graphql_fields(*expected_fields)
end
describe 'label field' do
subject { described_class.fields['label'] }
it { is_expected.to have_graphql_type(Types::LabelType) }
it { is_expected.to have_graphql_arguments(:title) }
end
describe 'labels field' do
subject { described_class.fields['labels'] }
it { is_expected.to have_graphql_type(Types::LabelType.connection_type) }
it { is_expected.to have_graphql_arguments(labels_resolver_arguments) }
end
end
RSpec.shared_examples 'querying a GraphQL type with labels' do
let_it_be(:current_user) { create(:user) }
let_it_be(:label_a) { create(label_factory, :described, **label_attrs) }
let_it_be(:label_b) { create(label_factory, :described, description: 'matching', **label_attrs) }
let_it_be(:label_c) do
create(label_factory, :described, :scoped, description: 'test', prefix: 'matching', **label_attrs)
end
let_it_be(:label_d) do
create(label_factory, :described, :scoped, description: 'test', prefix: 'matching', **label_attrs)
end
let(:label_title) { label_b.title }
let(:label_params) { { title: label_title } }
let(:labels_params) { nil }
let(:label_response) { graphql_data.dig(*path_prefix, 'label') }
let(:labels_response) { graphql_data.dig(*path_prefix, 'labels', 'nodes') }
let(:query) do
make_query(
[
query_graphql_field(:label, label_params, all_graphql_fields_for(Label)),
query_graphql_field(:labels, labels_params, [query_graphql_field(:nodes, nil, all_graphql_fields_for(Label))])
]
)
end
context 'running a query' do
before do
run_query(query)
end
context 'minimum required arguments' do
it 'returns the label information' do
expect(label_response).to include(
'title' => label_title,
'description' => label_b.description
)
end
it 'returns the labels information' do
expect(labels_response.pluck('title')).to contain_exactly(
label_a.title,
label_b.title,
label_c.title,
label_d.title
)
end
end
context 'with a search param' do
let(:labels_params) { { search_term: 'matching' } }
it 'finds the matching labels' do
expect(labels_response.pluck('title')).to contain_exactly(
label_b.title,
label_c.title,
label_d.title
)
end
context 'when searching only in the title' do
let(:labels_params) { { search_term: 'matching', search_in: [:TITLE] } }
it 'finds the matching labels' do
expect(labels_response.pluck('title')).to contain_exactly(
label_c.title,
label_d.title
)
end
end
context 'when searching only in the description' do
let(:labels_params) { { search_term: 'matching', search_in: [:DESCRIPTION] } }
it 'finds the matching labels' do
expect(labels_response.pluck('title')).to contain_exactly(label_b.title)
end
end
end
context 'title search' do
let(:labels_params) { { title: label_a.title } }
it 'finds the labels with exact title matching' do
expect(labels_response.pluck('title')).to contain_exactly(
label_a.title
)
end
end
context 'the label does not exist' do
let(:label_title) { 'not-a-label' }
it 'returns nil' do
expect(label_response).to be_nil
end
end
end
describe 'performance' do
def query_for(*labels)
selections = labels.map do |label|
%[#{label.title.gsub(/:+/, '_')}: label(title: "#{label.title}") { description }]
end
make_query(selections)
end
before do
run_query(query_for(label_a))
end
it 'batches queries for labels by title', :request_store do
multi_selection = query_for(label_b, label_c)
single_selection = query_for(label_d)
expect { run_query(multi_selection) }
.to issue_same_number_of_queries_as { run_query(single_selection) }.ignoring_cached_queries
end
end
# Run a known good query with the current user
def run_query(query)
post_graphql(query, current_user: current_user)
expect(graphql_errors).not_to be_present
end
end
|