Commit 5c9c86a3 authored by Frédéric Caplette's avatar Frédéric Caplette Committed by Natalia Tepluhina

Restructure client queries in Pipeline editor

parent 074698af
...@@ -60,6 +60,9 @@ export default { ...@@ -60,6 +60,9 @@ export default {
apollo: { apollo: {
currentBranch: { currentBranch: {
query: getCurrentBranch, query: getCurrentBranch,
update(data) {
return data.workBranches.current.name;
},
}, },
}, },
computed: { computed: {
......
...@@ -20,8 +20,8 @@ import { ...@@ -20,8 +20,8 @@ import {
} from '~/pipeline_editor/constants'; } from '~/pipeline_editor/constants';
import updateCurrentBranchMutation from '~/pipeline_editor/graphql/mutations/client/update_current_branch.mutation.graphql'; import updateCurrentBranchMutation from '~/pipeline_editor/graphql/mutations/client/update_current_branch.mutation.graphql';
import getAvailableBranchesQuery from '~/pipeline_editor/graphql/queries/available_branches.query.graphql'; import getAvailableBranchesQuery from '~/pipeline_editor/graphql/queries/available_branches.query.graphql';
import getCurrentBranchQuery from '~/pipeline_editor/graphql/queries/client/current_branch.query.graphql'; import getCurrentBranch from '~/pipeline_editor/graphql/queries/client/current_branch.query.graphql';
import getLastCommitBranchQuery from '~/pipeline_editor/graphql/queries/client/last_commit_branch.query.graphql'; import getLastCommitBranch from '~/pipeline_editor/graphql/queries/client/last_commit_branch.query.graphql';
export default { export default {
i18n: { i18n: {
...@@ -61,8 +61,8 @@ export default { ...@@ -61,8 +61,8 @@ export default {
}, },
data() { data() {
return { return {
branchSelected: null,
availableBranches: [], availableBranches: [],
branchSelected: null,
filteredBranches: [], filteredBranches: [],
isSearchingBranches: false, isSearchingBranches: false,
pageLimit: this.paginationLimit, pageLimit: this.paginationLimit,
...@@ -93,15 +93,25 @@ export default { ...@@ -93,15 +93,25 @@ export default {
}, },
}, },
currentBranch: { currentBranch: {
query: getCurrentBranchQuery, query: getCurrentBranch,
update(data) {
return data.workBranches.current.name;
},
}, },
lastCommitBranch: { lastCommitBranch: {
query: getLastCommitBranchQuery, query: getLastCommitBranch,
result({ data: { lastCommitBranch } }) { update(data) {
return data.workBranches.lastCommit.name;
},
result({ data }) {
if (data) {
const { name: lastCommitBranch } = data.workBranches.lastCommit;
if (lastCommitBranch === '' || this.availableBranches.includes(lastCommitBranch)) { if (lastCommitBranch === '' || this.availableBranches.includes(lastCommitBranch)) {
return; return;
} }
this.availableBranches.unshift(lastCommitBranch); this.availableBranches.unshift(lastCommitBranch);
}
}, },
}, },
}, },
...@@ -109,12 +119,12 @@ export default { ...@@ -109,12 +119,12 @@ export default {
branches() { branches() {
return this.searchTerm.length > 0 ? this.filteredBranches : this.availableBranches; return this.searchTerm.length > 0 ? this.filteredBranches : this.availableBranches;
}, },
isBranchesLoading() {
return this.$apollo.queries.availableBranches.loading || this.isSearchingBranches;
},
enableBranchSwitcher() { enableBranchSwitcher() {
return this.branches.length > 0 || this.searchTerm.length > 0; return this.branches.length > 0 || this.searchTerm.length > 0;
}, },
isBranchesLoading() {
return this.$apollo.queries.availableBranches.loading || this.isSearchingBranches;
},
}, },
watch: { watch: {
shouldLoadNewBranch(flag) { shouldLoadNewBranch(flag) {
......
...@@ -48,6 +48,9 @@ export default { ...@@ -48,6 +48,9 @@ export default {
apollo: { apollo: {
pipelineEtag: { pipelineEtag: {
query: getPipelineEtag, query: getPipelineEtag,
update(data) {
return data.etags.pipeline;
},
}, },
pipeline: { pipeline: {
context() { context() {
......
...@@ -43,6 +43,9 @@ export default { ...@@ -43,6 +43,9 @@ export default {
apollo: { apollo: {
appStatus: { appStatus: {
query: getAppStatus, query: getAppStatus,
update(data) {
return data.app.status;
},
}, },
}, },
computed: { computed: {
......
...@@ -91,6 +91,9 @@ export default { ...@@ -91,6 +91,9 @@ export default {
apollo: { apollo: {
appStatus: { appStatus: {
query: getAppStatus, query: getAppStatus,
update(data) {
return data.app.status;
},
}, },
}, },
computed: { computed: {
......
query getAppStatus { query getAppStatus {
appStatus @client app @client {
status
}
} }
query getCurrentBranch { query getCurrentBranch {
currentBranch @client workBranches @client {
current {
name
}
}
} }
query getLastCommitBranchQuery { query getLastCommitBranchQuery {
lastCommitBranch @client workBranches @client {
lastCommit {
name
}
}
} }
query getPipelineEtag { query getPipelineEtag {
pipelineEtag @client etags @client {
pipeline
}
} }
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import getAppStatus from './queries/client/app_status.query.graphql'; import getAppStatus from './queries/client/app_status.query.graphql';
import getCurrentBranchQuery from './queries/client/current_branch.query.graphql'; import getCurrentBranch from './queries/client/current_branch.query.graphql';
import getLastCommitBranchQuery from './queries/client/last_commit_branch.query.graphql'; import getLastCommitBranch from './queries/client/last_commit_branch.query.graphql';
import getPipelineEtag from './queries/client/pipeline_etag.query.graphql'; import getPipelineEtag from './queries/client/pipeline_etag.query.graphql';
export const resolvers = { export const resolvers = {
...@@ -35,25 +35,51 @@ export const resolvers = { ...@@ -35,25 +35,51 @@ export const resolvers = {
updateAppStatus: (_, { appStatus }, { cache }) => { updateAppStatus: (_, { appStatus }, { cache }) => {
cache.writeQuery({ cache.writeQuery({
query: getAppStatus, query: getAppStatus,
data: { appStatus }, data: {
app: {
__typename: 'PipelineEditorApp',
status: appStatus,
},
},
}); });
}, },
updateCurrentBranch: (_, { currentBranch }, { cache }) => { updateCurrentBranch: (_, { currentBranch }, { cache }) => {
cache.writeQuery({ cache.writeQuery({
query: getCurrentBranchQuery, query: getCurrentBranch,
data: { currentBranch }, data: {
workBranches: {
__typename: 'BranchList',
current: {
__typename: 'WorkBranch',
name: currentBranch,
},
},
},
}); });
}, },
updateLastCommitBranch: (_, { lastCommitBranch }, { cache }) => { updateLastCommitBranch: (_, { lastCommitBranch }, { cache }) => {
cache.writeQuery({ cache.writeQuery({
query: getLastCommitBranchQuery, query: getLastCommitBranch,
data: { lastCommitBranch }, data: {
workBranches: {
__typename: 'BranchList',
lastCommit: {
__typename: 'WorkBranch',
name: lastCommitBranch,
},
},
},
}); });
}, },
updatePipelineEtag: (_, { pipelineEtag }, { cache }) => { updatePipelineEtag: (_, { pipelineEtag }, { cache }) => {
cache.writeQuery({ cache.writeQuery({
query: getPipelineEtag, query: getPipelineEtag,
data: { pipelineEtag }, data: {
etags: {
__typename: 'EtagValues',
pipeline: pipelineEtag,
},
},
}); });
}, },
}, },
......
type BlobContent { type PipelineEditorApp {
rawData: String! status: String!
}
type BranchList {
current: WorkBranch!
lastCommit: WorkBranch!
}
type EtagValues {
pipeline: String!
}
type WorkBranch {
name: String!
commit: String
} }
extend type Query { extend type Query {
blobContent: BlobContent app: PipelineEditorApp
etags: EtagValues
workBranches: BranchList
} }
...@@ -7,7 +7,7 @@ import { EDITOR_APP_STATUS_LOADING } from './constants'; ...@@ -7,7 +7,7 @@ import { EDITOR_APP_STATUS_LOADING } from './constants';
import { CODE_SNIPPET_SOURCE_SETTINGS } from './components/code_snippet_alert/constants'; import { CODE_SNIPPET_SOURCE_SETTINGS } from './components/code_snippet_alert/constants';
import getCurrentBranch from './graphql/queries/client/current_branch.query.graphql'; import getCurrentBranch from './graphql/queries/client/current_branch.query.graphql';
import getAppStatus from './graphql/queries/client/app_status.query.graphql'; import getAppStatus from './graphql/queries/client/app_status.query.graphql';
import getLastCommitBranchQuery from './graphql/queries/client/last_commit_branch.query.graphql'; import getLastCommitBranch from './graphql/queries/client/last_commit_branch.query.graphql';
import getPipelineEtag from './graphql/queries/client/pipeline_etag.query.graphql'; import getPipelineEtag from './graphql/queries/client/pipeline_etag.query.graphql';
import { resolvers } from './graphql/resolvers'; import { resolvers } from './graphql/resolvers';
import typeDefs from './graphql/typedefs.graphql'; import typeDefs from './graphql/typedefs.graphql';
...@@ -68,28 +68,46 @@ export const initPipelineEditor = (selector = '#js-pipeline-editor') => { ...@@ -68,28 +68,46 @@ export const initPipelineEditor = (selector = '#js-pipeline-editor') => {
cache.writeQuery({ cache.writeQuery({
query: getAppStatus, query: getAppStatus,
data: { data: {
appStatus: EDITOR_APP_STATUS_LOADING, app: {
__typename: 'PipelineEditorApp',
status: EDITOR_APP_STATUS_LOADING,
},
}, },
}); });
cache.writeQuery({ cache.writeQuery({
query: getCurrentBranch, query: getCurrentBranch,
data: { data: {
currentBranch: initialBranchName || defaultBranch, workBranches: {
__typename: 'BranchList',
current: {
__typename: 'WorkBranch',
name: initialBranchName || defaultBranch,
},
},
}, },
}); });
cache.writeQuery({ cache.writeQuery({
query: getPipelineEtag, query: getLastCommitBranch,
data: { data: {
pipelineEtag, workBranches: {
__typename: 'BranchList',
lastCommit: {
__typename: 'WorkBranch',
name: '',
},
},
}, },
}); });
cache.writeQuery({ cache.writeQuery({
query: getLastCommitBranchQuery, query: getPipelineEtag,
data: { data: {
lastCommitBranch: '', etags: {
__typename: 'EtagValues',
pipeline: pipelineEtag,
},
}, },
}); });
......
...@@ -160,6 +160,9 @@ export default { ...@@ -160,6 +160,9 @@ export default {
}, },
appStatus: { appStatus: {
query: getAppStatus, query: getAppStatus,
update(data) {
return data.app.status;
},
}, },
commitSha: { commitSha: {
query: getLatestCommitShaQuery, query: getLatestCommitShaQuery,
...@@ -184,6 +187,9 @@ export default { ...@@ -184,6 +187,9 @@ export default {
}, },
currentBranch: { currentBranch: {
query: getCurrentBranch, query: getCurrentBranch,
update(data) {
return data.workBranches.current.name;
},
}, },
starterTemplate: { starterTemplate: {
query: getTemplate, query: getTemplate,
......
...@@ -51,6 +51,17 @@ RSpec.describe 'Pipeline Editor', :js do ...@@ -51,6 +51,17 @@ RSpec.describe 'Pipeline Editor', :js do
expect(page).not_to have_content(default_branch) expect(page).not_to have_content(default_branch)
end end
end end
it 'displays new branch as selected after commiting on a new branch' do
find('#target-branch-field').set('new_branch', clear: :backspace)
click_button 'Commit changes'
page.within('[data-testid="branch-selector"]') do
expect(page).to have_content('new_branch')
expect(page).not_to have_content(default_branch)
end
end
end end
context 'Editor content' do context 'Editor content' do
......
...@@ -22,7 +22,6 @@ import { ...@@ -22,7 +22,6 @@ import {
mockTotalBranches, mockTotalBranches,
mockTotalBranchResults, mockTotalBranchResults,
mockTotalSearchResults, mockTotalSearchResults,
mockNewBranch,
} from '../../mock_data'; } from '../../mock_data';
const localVue = createLocalVue(); const localVue = createLocalVue();
...@@ -32,18 +31,14 @@ describe('Pipeline editor branch switcher', () => { ...@@ -32,18 +31,14 @@ describe('Pipeline editor branch switcher', () => {
let wrapper; let wrapper;
let mockApollo; let mockApollo;
let mockAvailableBranchQuery; let mockAvailableBranchQuery;
let mockCurrentBranchQuery;
let mockLastCommitBranchQuery; const createComponent = ({
currentBranch = mockDefaultBranch,
const createComponent = ( isQueryLoading = false,
{ currentBranch, isQueryLoading, mountFn, options, props } = { mountFn = shallowMount,
currentBranch: mockDefaultBranch, options = {},
hasUnsavedChanges: false, props = {},
isQueryLoading: false, } = {}) => {
mountFn: shallowMount,
options: {},
},
) => {
wrapper = mountFn(BranchSwitcher, { wrapper = mountFn(BranchSwitcher, {
propsData: { propsData: {
...props, ...props,
...@@ -74,17 +69,7 @@ describe('Pipeline editor branch switcher', () => { ...@@ -74,17 +69,7 @@ describe('Pipeline editor branch switcher', () => {
const createComponentWithApollo = ({ mountFn = shallowMount, props = {} } = {}) => { const createComponentWithApollo = ({ mountFn = shallowMount, props = {} } = {}) => {
const handlers = [[getAvailableBranchesQuery, mockAvailableBranchQuery]]; const handlers = [[getAvailableBranchesQuery, mockAvailableBranchQuery]];
const resolvers = { mockApollo = createMockApollo(handlers);
Query: {
currentBranch() {
return mockCurrentBranchQuery();
},
lastCommitBranch() {
return mockLastCommitBranchQuery();
},
},
};
mockApollo = createMockApollo(handlers, resolvers);
createComponent({ createComponent({
mountFn, mountFn,
...@@ -104,22 +89,12 @@ describe('Pipeline editor branch switcher', () => { ...@@ -104,22 +89,12 @@ describe('Pipeline editor branch switcher', () => {
const findInfiniteScroll = () => wrapper.findComponent(GlInfiniteScroll); const findInfiniteScroll = () => wrapper.findComponent(GlInfiniteScroll);
const defaultBranchInDropdown = () => findDropdownItems().at(0); const defaultBranchInDropdown = () => findDropdownItems().at(0);
const setMockResolvedValues = ({ availableBranches, currentBranch, lastCommitBranch }) => { const setAvailableBranchesMock = (availableBranches) => {
if (availableBranches) {
mockAvailableBranchQuery.mockResolvedValue(availableBranches); mockAvailableBranchQuery.mockResolvedValue(availableBranches);
}
if (currentBranch) {
mockCurrentBranchQuery.mockResolvedValue(currentBranch);
}
mockLastCommitBranchQuery.mockResolvedValue(lastCommitBranch || '');
}; };
beforeEach(() => { beforeEach(() => {
mockAvailableBranchQuery = jest.fn(); mockAvailableBranchQuery = jest.fn();
mockCurrentBranchQuery = jest.fn();
mockLastCommitBranchQuery = jest.fn();
}); });
afterEach(() => { afterEach(() => {
...@@ -148,10 +123,7 @@ describe('Pipeline editor branch switcher', () => { ...@@ -148,10 +123,7 @@ describe('Pipeline editor branch switcher', () => {
describe('after querying', () => { describe('after querying', () => {
beforeEach(async () => { beforeEach(async () => {
setMockResolvedValues({ setAvailableBranchesMock(mockProjectBranches);
availableBranches: mockProjectBranches,
currentBranch: mockDefaultBranch,
});
createComponentWithApollo({ mountFn: mount }); createComponentWithApollo({ mountFn: mount });
await waitForPromises(); await waitForPromises();
}); });
...@@ -180,10 +152,7 @@ describe('Pipeline editor branch switcher', () => { ...@@ -180,10 +152,7 @@ describe('Pipeline editor branch switcher', () => {
describe('on fetch error', () => { describe('on fetch error', () => {
beforeEach(async () => { beforeEach(async () => {
setMockResolvedValues({ setAvailableBranchesMock(new Error());
availableBranches: new Error(),
currentBranch: mockDefaultBranch,
});
createComponentWithApollo(); createComponentWithApollo();
await waitForPromises(); await waitForPromises();
}); });
...@@ -200,10 +169,7 @@ describe('Pipeline editor branch switcher', () => { ...@@ -200,10 +169,7 @@ describe('Pipeline editor branch switcher', () => {
describe('when switching branches', () => { describe('when switching branches', () => {
beforeEach(async () => { beforeEach(async () => {
jest.spyOn(window.history, 'pushState').mockImplementation(() => {}); jest.spyOn(window.history, 'pushState').mockImplementation(() => {});
setMockResolvedValues({ setAvailableBranchesMock(mockProjectBranches);
availableBranches: mockProjectBranches,
currentBranch: mockDefaultBranch,
});
createComponentWithApollo({ mountFn: mount }); createComponentWithApollo({ mountFn: mount });
await waitForPromises(); await waitForPromises();
}); });
...@@ -271,10 +237,7 @@ describe('Pipeline editor branch switcher', () => { ...@@ -271,10 +237,7 @@ describe('Pipeline editor branch switcher', () => {
describe('when searching', () => { describe('when searching', () => {
beforeEach(async () => { beforeEach(async () => {
setMockResolvedValues({ setAvailableBranchesMock(mockProjectBranches);
availableBranches: mockProjectBranches,
currentBranch: mockDefaultBranch,
});
createComponentWithApollo({ mountFn: mount }); createComponentWithApollo({ mountFn: mount });
await waitForPromises(); await waitForPromises();
}); });
...@@ -374,10 +337,7 @@ describe('Pipeline editor branch switcher', () => { ...@@ -374,10 +337,7 @@ describe('Pipeline editor branch switcher', () => {
describe('when scrolling to the bottom of the list', () => { describe('when scrolling to the bottom of the list', () => {
beforeEach(async () => { beforeEach(async () => {
setMockResolvedValues({ setAvailableBranchesMock(mockProjectBranches);
availableBranches: mockProjectBranches,
currentBranch: mockDefaultBranch,
});
createComponentWithApollo(); createComponentWithApollo();
await waitForPromises(); await waitForPromises();
}); });
...@@ -433,35 +393,4 @@ describe('Pipeline editor branch switcher', () => { ...@@ -433,35 +393,4 @@ describe('Pipeline editor branch switcher', () => {
}); });
}); });
}); });
describe('when committing a new branch', () => {
const createNewBranch = async () => {
setMockResolvedValues({
currentBranch: mockNewBranch,
lastCommitBranch: mockNewBranch,
});
await wrapper.vm.$apollo.queries.currentBranch.refetch();
await wrapper.vm.$apollo.queries.lastCommitBranch.refetch();
};
beforeEach(async () => {
setMockResolvedValues({
availableBranches: mockProjectBranches,
currentBranch: mockDefaultBranch,
});
createComponentWithApollo({ mountFn: mount });
await waitForPromises();
await createNewBranch();
});
it('sets new branch as current branch', () => {
expect(defaultBranchInDropdown().text()).toBe(mockNewBranch);
expect(defaultBranchInDropdown().props('isChecked')).toBe(true);
});
it('adds new branch to branch switcher', () => {
expect(defaultBranchInDropdown().text()).toBe(mockNewBranch);
expect(findDropdownItems()).toHaveLength(mockTotalBranchResults + 1);
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment