1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe StartupjsHelper do
6  using RSpec::Parameterized::TableSyntax
7
8  describe '#page_startup_graphql_calls' do
9    let(:query_location) { 'repository/path_last_commit' }
10    let(:query_content) do
11      File.read(File.join(Rails.root, 'app/graphql/queries', "#{query_location}.query.graphql"))
12    end
13
14    it 'returns an array containing GraphQL Page Startup Calls' do
15      helper.add_page_startup_graphql_call(query_location, { ref: 'foo' })
16
17      startup_graphql_calls = helper.page_startup_graphql_calls
18
19      expect(startup_graphql_calls).to include({ query: query_content, variables: { ref: 'foo' } })
20    end
21  end
22
23  describe '#page_startup_graphql_headers' do
24    where(:csrf_token, :feature_category, :expected) do
25      'abc'       | 'web_ide' | { 'X-CSRF-Token' => 'abc', 'x-gitlab-feature-category' => 'web_ide' }
26      ''          | ''        | { 'X-CSRF-Token' => '', 'x-gitlab-feature-category' => '' }
27      'abc'       | nil       | { 'X-CSRF-Token' => 'abc', 'x-gitlab-feature-category' => '' }
28      'something' | '   '     | { 'X-CSRF-Token' => 'something', 'x-gitlab-feature-category' => '' }
29    end
30
31    with_them do
32      before do
33        allow(helper).to receive(:form_authenticity_token).and_return(csrf_token)
34        ::Gitlab::ApplicationContext.push(feature_category: feature_category)
35      end
36
37      it 'returns hash of headers for GraphQL requests' do
38        expect(helper.page_startup_graphql_headers).to eq(expected)
39      end
40    end
41  end
42end
43