diff --git a/app/assets/javascripts/gpg_badges.js b/app/assets/javascripts/gpg_badges.js index 1c379e9bb67..7ac9dcd1112 100644 --- a/app/assets/javascripts/gpg_badges.js +++ b/app/assets/javascripts/gpg_badges.js @@ -1,12 +1,14 @@ export default class GpgBadges { static fetch() { + const badges = $('.js-loading-gpg-badge'); const form = $('.commits-search-form'); + badges.html(''); + $.get({ url: form.data('signatures-path'), data: form.serialize(), }).done((response) => { - const badges = $('.js-loading-gpg-badge'); response.signatures.forEach((signature) => { badges.filter(`[data-commit-sha="${signature.commit_sha}"]`).replaceWith(signature.html); }); diff --git a/app/assets/stylesheets/pages/commits.scss b/app/assets/stylesheets/pages/commits.scss index cd9f2d787c5..46fbfe5f91e 100644 --- a/app/assets/stylesheets/pages/commits.scss +++ b/app/assets/stylesheets/pages/commits.scss @@ -286,6 +286,10 @@ .gpg-status-box { + &:empty { + display: none; + } + &.valid { @include green-status-color; } diff --git a/app/views/projects/commit/_ajax_signature.html.haml b/app/views/projects/commit/_ajax_signature.html.haml index 22674b671c9..83821326aec 100644 --- a/app/views/projects/commit/_ajax_signature.html.haml +++ b/app/views/projects/commit/_ajax_signature.html.haml @@ -1,3 +1,2 @@ - if commit.has_signature? %button{ class: commit_signature_badge_classes('js-loading-gpg-badge'), data: { toggle: 'tooltip', placement: 'auto top', title: 'GPG signature (loading...)', 'commit-sha' => commit.sha } } - %i.fa.fa-spinner.fa-spin diff --git a/spec/javascripts/gpg_badges_spec.js b/spec/javascripts/gpg_badges_spec.js new file mode 100644 index 00000000000..7a826487bf9 --- /dev/null +++ b/spec/javascripts/gpg_badges_spec.js @@ -0,0 +1,48 @@ +import GpgBadges from '~/gpg_badges'; + +describe('GpgBadges', () => { + const dummyCommitSha = 'n0m0rec0ffee'; + const dummyBadgeHtml = 'dummy html'; + const dummyResponse = { + signatures: [{ + commit_sha: dummyCommitSha, + html: dummyBadgeHtml, + }], + }; + + beforeEach(() => { + setFixtures(` +
+
+
+ `); + }); + + it('displays a loading spinner', () => { + spyOn($, 'get').and.returnValue({ + done() { + // intentionally left blank + }, + }); + + GpgBadges.fetch(); + + expect(document.querySelector('.js-loading-gpg-badge:empty')).toBe(null); + const spinners = document.querySelectorAll('.js-loading-gpg-badge i.fa.fa-spinner.fa-spin'); + expect(spinners.length).toBe(1); + }); + + it('replaces the loading spinner', () => { + spyOn($, 'get').and.returnValue({ + done(callback) { + callback(dummyResponse); + }, + }); + + GpgBadges.fetch(); + + expect(document.querySelector('.js-loading-gpg-badge')).toBe(null); + const parentContainer = document.querySelector('.parent-container'); + expect(parentContainer.innerHTML.trim()).toEqual(dummyBadgeHtml); + }); +}); diff --git a/spec/support/gpg_helpers.rb b/spec/support/gpg_helpers.rb index 96ea6f28b30..65b38626a51 100644 --- a/spec/support/gpg_helpers.rb +++ b/spec/support/gpg_helpers.rb @@ -1,4 +1,6 @@ module GpgHelpers + SIGNED_COMMIT_SHA = '8a852d50dda17cc8fd1408d2fd0c5b0f24c76ca4'.freeze + module User1 extend self diff --git a/spec/views/projects/commits/_commit.html.haml_spec.rb b/spec/views/projects/commits/_commit.html.haml_spec.rb new file mode 100644 index 00000000000..4c247361bd7 --- /dev/null +++ b/spec/views/projects/commits/_commit.html.haml_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'projects/commits/_commit.html.haml' do + context 'with a singed commit' do + let(:project) { create(:project, :repository) } + let(:repository) { project.repository } + let(:ref) { GpgHelpers::SIGNED_COMMIT_SHA } + let(:commit) { repository.commit(ref) } + + it 'does not display a loading spinner for GPG status' do + render partial: 'projects/commits/commit', locals: { + project: project, + ref: ref, + commit: commit + } + + within '.gpg-status-box' do + expect(page).not_to have_css('i.fa.fa-spinner.fa-spin') + end + end + end +end