From 34ab712aae53494b64a62e75b4d73f8881ca61b2 Mon Sep 17 00:00:00 2001 From: Miguel Rincon <mrincon@gitlab.com> Date: Mon, 27 Jun 2022 18:08:03 +0200 Subject: [PATCH] Replace hardcoded mock job data with fixtures --- spec/frontend/fixtures/jobs.rb | 72 +++++-- .../components/table/cells/job_cell_spec.js | 19 +- spec/frontend/jobs/mock_data.js | 199 +----------------- 3 files changed, 66 insertions(+), 224 deletions(-) diff --git a/spec/frontend/fixtures/jobs.rb b/spec/frontend/fixtures/jobs.rb index 3cc874326553d..c76b06bd39ee3 100644 --- a/spec/frontend/fixtures/jobs.rb +++ b/spec/frontend/fixtures/jobs.rb @@ -2,40 +2,68 @@ require 'spec_helper' -RSpec.describe Projects::JobsController, '(JavaScript fixtures)', type: :controller do +RSpec.describe 'Jobs (JavaScript fixtures)' do + include ApiHelpers include JavaScriptFixturesHelpers + include GraphqlHelpers let(:namespace) { create(:namespace, name: 'frontend-fixtures' )} let(:project) { create(:project, :repository, namespace: namespace, path: 'builds-project') } let(:user) { project.first_owner } let(:pipeline) { create(:ci_empty_pipeline, project: project, sha: project.commit.id) } - let!(:build_with_artifacts) { create(:ci_build, :success, :artifacts, :trace_artifact, pipeline: pipeline, stage: 'test', artifacts_expire_at: Time.now + 18.months) } - let!(:failed_build) { create(:ci_build, :failed, pipeline: pipeline, stage: 'build') } - let!(:pending_build) { create(:ci_build, :pending, pipeline: pipeline, stage: 'deploy') } - let!(:delayed_job) do - create(:ci_build, :scheduled, - pipeline: pipeline, - name: 'delayed job', - stage: 'test') + + after do + remove_repository(project) end - render_views + describe Projects::JobsController, type: :controller do + let!(:delayed) { create(:ci_build, :scheduled, pipeline: pipeline, name: 'delayed job') } - before do - sign_in(user) - end + before do + sign_in(user) + end - after do - remove_repository(project) + it 'jobs/delayed.json' do + get :show, params: { + namespace_id: project.namespace.to_param, + project_id: project, + id: delayed.to_param + }, format: :json + + expect(response).to be_successful + end end - it 'jobs/delayed.json' do - get :show, params: { - namespace_id: project.namespace.to_param, - project_id: project, - id: delayed_job.to_param - }, format: :json + describe GraphQL::Query, type: :request do + let!(:build) { create(:ci_build, :success, name: 'build', pipeline: pipeline) } + let!(:created_by_tag) { create(:ci_build, :success, name: 'created_by_tag', tag: true, pipeline: pipeline) } + let!(:with_coverage) { create(:ci_build, :success, name: 'with_coverage', coverage: 40.0, pipeline: pipeline) } + let!(:stuck) { create(:ci_build, :pending, name: 'stuck', pipeline: pipeline) } + + fixtures_path = 'graphql/jobs/' + get_jobs_query = 'get_jobs.query.graphql' + + let_it_be(:query) do + get_graphql_query_as_string("jobs/components/table/graphql/queries/#{get_jobs_query}") + end + + it "#{fixtures_path}#{get_jobs_query}.json" do + post_graphql(query, current_user: user, variables: { + fullPath: 'frontend-fixtures/builds-project' + }) + + expect_graphql_errors_to_be_empty + end + + it "#{fixtures_path}#{get_jobs_query}.as_guest.json" do + guest = create(:user) + project.add_guest(guest) + + post_graphql(query, current_user: guest, variables: { + fullPath: 'frontend-fixtures/builds-project' + }) - expect(response).to be_successful + expect_graphql_errors_to_be_empty + end end end diff --git a/spec/frontend/jobs/components/table/cells/job_cell_spec.js b/spec/frontend/jobs/components/table/cells/job_cell_spec.js index fc4e5586349e7..e3bef17b6fa40 100644 --- a/spec/frontend/jobs/components/table/cells/job_cell_spec.js +++ b/spec/frontend/jobs/components/table/cells/job_cell_spec.js @@ -2,12 +2,9 @@ import { shallowMount } from '@vue/test-utils'; import { extendedWrapper } from 'helpers/vue_test_utils_helper'; import { getIdFromGraphQLId } from '~/graphql_shared/utils'; import JobCell from '~/jobs/components/table/cells/job_cell.vue'; -import { mockJobsInTable } from '../../../mock_data'; +import { mockJobsInTable, mockJobsAsGuestInTable } from '../../../mock_data'; -const mockJob = mockJobsInTable[0]; -const mockJobCreatedByTag = mockJobsInTable[1]; -const mockJobLimitedAccess = mockJobsInTable[2]; -const mockStuckJob = mockJobsInTable[3]; +const getMockJob = (name) => mockJobsInTable.find((job) => job.name === name); describe('Job Cell', () => { let wrapper; @@ -23,6 +20,8 @@ describe('Job Cell', () => { const findBadgeById = (id) => wrapper.findByTestId(id); + const mockJob = getMockJob('build'); + const createComponent = (jobData = mockJob) => { wrapper = extendedWrapper( shallowMount(JobCell, { @@ -49,9 +48,11 @@ describe('Job Cell', () => { }); it('display the job id with no link', () => { - createComponent(mockJobLimitedAccess); + const mockJobAsGuest = mockJobsAsGuestInTable[0]; + + createComponent(mockJobAsGuest); - const expectedJobId = `#${getIdFromGraphQLId(mockJobLimitedAccess.id)}`; + const expectedJobId = `#${getIdFromGraphQLId(mockJobAsGuest.id)}`; expect(findJobIdNoLink().text()).toBe(expectedJobId); expect(findJobIdNoLink().exists()).toBe(true); @@ -75,7 +76,7 @@ describe('Job Cell', () => { }); it('displays label icon when job is created by a tag', () => { - createComponent(mockJobCreatedByTag); + createComponent(getMockJob('created_by_tag')); expect(findLabelIcon().exists()).toBe(true); expect(findForkIcon().exists()).toBe(false); @@ -131,7 +132,7 @@ describe('Job Cell', () => { }); it('stuck icon is shown if job is stuck', () => { - createComponent(mockStuckJob); + createComponent(getMockJob('stuck')); expect(findStuckIcon().exists()).toBe(true); expect(findStuckIcon().attributes('name')).toBe('warning'); diff --git a/spec/frontend/jobs/mock_data.js b/spec/frontend/jobs/mock_data.js index 4676635cce01a..57ec1c7ef3f3f 100644 --- a/spec/frontend/jobs/mock_data.js +++ b/spec/frontend/jobs/mock_data.js @@ -1,8 +1,14 @@ +import mockJobs from 'test_fixtures/graphql/jobs/get_jobs.query.graphql.json'; +import mockJobsAsGuest from 'test_fixtures/graphql/jobs/get_jobs.query.graphql.as_guest.json'; import { TEST_HOST } from 'spec/test_constants'; const threeWeeksAgo = new Date(); threeWeeksAgo.setDate(threeWeeksAgo.getDate() - 21); +// Fixtures generated at spec/frontend/fixtures/jobs.rb +export const mockJobsInTable = mockJobs.data.project.jobs.nodes; +export const mockJobsAsGuestInTable = mockJobsAsGuest.data.project.jobs.nodes; + export const stages = [ { name: 'build', @@ -1283,199 +1289,6 @@ export const mockPipelineDetached = { }, }; -export const mockJobsInTable = [ - { - detailedStatus: { - icon: 'status_manual', - label: 'manual play action', - text: 'manual', - tooltip: 'manual action', - action: { - buttonTitle: 'Trigger this manual action', - icon: 'play', - method: 'post', - path: '/root/ci-project/-/jobs/2004/play', - title: 'Play', - __typename: 'StatusAction', - }, - detailsPath: '/root/ci-project/-/jobs/2004', - __typename: 'DetailedStatus', - }, - id: 'gid://gitlab/Ci::Build/2004', - refName: 'main', - refPath: '/root/ci-project/-/commits/main', - tags: [], - shortSha: '2d5d8323', - commitPath: '/root/ci-project/-/commit/2d5d83230bdea0e003d83ef4c16d2bf9a8808ebe', - pipeline: { - id: 'gid://gitlab/Ci::Pipeline/423', - path: '/root/ci-project/-/pipelines/423', - user: { - webPath: '/root', - avatarUrl: - 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - __typename: 'User', - }, - __typename: 'Pipeline', - }, - stage: { name: 'test', __typename: 'CiStage' }, - name: 'test_manual_job', - duration: null, - finishedAt: null, - coverage: null, - createdByTag: false, - retryable: false, - playable: true, - cancelable: false, - active: false, - stuck: false, - userPermissions: { readBuild: true, __typename: 'JobPermissions' }, - __typename: 'CiJob', - }, - { - detailedStatus: { - icon: 'status_skipped', - label: 'skipped', - text: 'skipped', - tooltip: 'skipped', - action: null, - __typename: 'DetailedStatus', - }, - id: 'gid://gitlab/Ci::Build/2021', - refName: 'main', - refPath: '/root/ci-project/-/commits/main', - tags: [], - shortSha: '2d5d8323', - commitPath: '/root/ci-project/-/commit/2d5d83230bdea0e003d83ef4c16d2bf9a8808ebe', - pipeline: { - id: 'gid://gitlab/Ci::Pipeline/425', - path: '/root/ci-project/-/pipelines/425', - user: { - webPath: '/root', - avatarUrl: - 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - __typename: 'User', - }, - __typename: 'Pipeline', - }, - stage: { name: 'test', __typename: 'CiStage' }, - name: 'coverage_job', - duration: null, - finishedAt: null, - coverage: null, - createdByTag: true, - retryable: false, - playable: false, - cancelable: false, - active: false, - stuck: false, - userPermissions: { readBuild: true, __typename: 'JobPermissions' }, - __typename: 'CiJob', - }, - { - detailedStatus: { - icon: 'status_success', - label: 'passed', - text: 'passed', - tooltip: 'passed', - action: { - buttonTitle: 'Retry this job', - icon: 'retry', - method: 'post', - path: '/root/ci-project/-/jobs/2015/retry', - title: 'Retry', - __typename: 'StatusAction', - }, - __typename: 'DetailedStatus', - }, - id: 'gid://gitlab/Ci::Build/2015', - refName: 'main', - refPath: '/root/ci-project/-/commits/main', - tags: [], - shortSha: '2d5d8323', - commitPath: '/root/ci-project/-/commit/2d5d83230bdea0e003d83ef4c16d2bf9a8808ebe', - pipeline: { - id: 'gid://gitlab/Ci::Pipeline/424', - path: '/root/ci-project/-/pipelines/424', - user: { - webPath: '/root', - avatarUrl: - 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - __typename: 'User', - }, - __typename: 'Pipeline', - }, - stage: { name: 'deploy', __typename: 'CiStage' }, - name: 'artifact_job', - duration: 2, - finishedAt: '2021-04-01T17:36:18Z', - coverage: 82.71, - createdByTag: false, - retryable: true, - playable: false, - cancelable: false, - active: false, - stuck: false, - userPermissions: { readBuild: false, __typename: 'JobPermissions' }, - __typename: 'CiJob', - }, - { - artifacts: { nodes: [], __typename: 'CiJobArtifactConnection' }, - allowFailure: false, - status: 'PENDING', - scheduledAt: null, - manualJob: false, - triggered: null, - createdByTag: false, - detailedStatus: { - detailsPath: '/root/ci-project/-/jobs/2391', - group: 'pending', - icon: 'status_pending', - label: 'pending', - text: 'pending', - tooltip: 'pending', - action: { - buttonTitle: 'Cancel this job', - icon: 'cancel', - method: 'post', - path: '/root/ci-project/-/jobs/2391/cancel', - title: 'Cancel', - __typename: 'StatusAction', - }, - __typename: 'DetailedStatus', - }, - id: 'gid://gitlab/Ci::Build/2391', - refName: 'master', - refPath: '/root/ci-project/-/commits/master', - tags: [], - shortSha: '916330b4', - commitPath: '/root/ci-project/-/commit/916330b4fda5dae226524ceb51c756c0ed26679d', - pipeline: { - id: 'gid://gitlab/Ci::Pipeline/482', - path: '/root/ci-project/-/pipelines/482', - user: { - webPath: '/root', - avatarUrl: - 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - __typename: 'UserCore', - }, - __typename: 'Pipeline', - }, - stage: { name: 'build', __typename: 'CiStage' }, - name: 'build_job', - duration: null, - finishedAt: null, - coverage: null, - retryable: false, - playable: false, - cancelable: true, - active: true, - stuck: true, - userPermissions: { readBuild: true, __typename: 'JobPermissions' }, - __typename: 'CiJob', - }, -]; - export const mockJobsQueryResponse = { data: { project: { -- GitLab