diff --git a/app/views/projects/graphs/charts.html.haml b/app/views/projects/graphs/charts.html.haml
index f3033e51a040b8442b224e3ddb9bf8466d78d567..7abf267c5f00148c890987745e243fb102ed0340 100644
--- a/app/views/projects/graphs/charts.html.haml
+++ b/app/views/projects/graphs/charts.html.haml
@@ -31,7 +31,7 @@
     .col-md-6
       .tree-ref-container
         .tree-ref-holder
-          #js-project-graph-ref-switcher{ data: { project_id: @project.id, graph_path: project_graph_path(@project, @id), project_branch: current_ref } }
+          #js-project-graph-ref-switcher{ data: { project_id: @project.id, graph_path: project_graph_path(@project, @ref), project_branch: current_ref } }
         %ul.breadcrumb.repo-breadcrumb
           = commits_breadcrumbs
 
diff --git a/spec/requests/projects/graphs_controller_spec.rb b/spec/requests/projects/graphs_controller_spec.rb
new file mode 100644
index 0000000000000000000000000000000000000000..66793e3e1ae7c7b3f13148c151e05962e76ae86e
--- /dev/null
+++ b/spec/requests/projects/graphs_controller_spec.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Projects::GraphsController, feature_category: :source_code_management do
+  let_it_be(:project) { create(:project, :repository, :private) }
+  let(:ref) { 'master' }
+
+  describe 'GET #charts' do
+    subject(:send_request) { get charts_project_graph_path(project, ref), params: params }
+
+    let(:params) { {} }
+
+    context 'when user is unauthorized' do
+      it 'shows 404' do
+        send_request
+        expect(response).to redirect_to(new_user_session_path)
+      end
+    end
+
+    context 'when user is authorized' do
+      let(:user) { project.creator }
+
+      before do
+        sign_in(user)
+      end
+
+      it 'renders content' do
+        send_request
+        expect(response).to be_successful
+      end
+
+      context 'when path includes a space' do
+        let(:params) { { path: 'a b' } }
+
+        it 'still renders the page' do
+          send_request
+
+          expect(response).to have_gitlab_http_status(:ok)
+        end
+      end
+    end
+  end
+end