Commit 92bd70eb authored by Sarah Groff Hennigh-Palermo's avatar Sarah Groff Hennigh-Palermo

Merge branch 'pipeline-editor-branch-switcher-functionality' into 'master'

Load correct data when switching branches in pipeline editor

See merge request gitlab-org/gitlab!57941
parents d6ee99f3 599b6c47
...@@ -44,6 +44,20 @@ export default { ...@@ -44,6 +44,20 @@ export default {
return this.branches?.length > 0; return this.branches?.length > 0;
}, },
}, },
methods: {
async selectBranch(newBranch) {
if (newBranch === this.currentBranch) {
return;
}
await this.$apollo.getClient().writeQuery({
query: getCurrentBranch,
data: { currentBranch: newBranch },
});
this.$emit('refetchContent');
},
},
}; };
</script> </script>
...@@ -57,6 +71,7 @@ export default { ...@@ -57,6 +71,7 @@ export default {
:key="branch.name" :key="branch.name"
:is-checked="currentBranch === branch.name" :is-checked="currentBranch === branch.name"
:is-check-item="true" :is-check-item="true"
@click="selectBranch(branch.name)"
> >
<gl-icon name="check" class="gl-visibility-hidden" /> <gl-icon name="check" class="gl-visibility-hidden" />
{{ branch.name }} {{ branch.name }}
......
<script> <script>
import { GlAlert, GlLoadingIcon } from '@gitlab/ui'; import { GlAlert, GlLoadingIcon } from '@gitlab/ui';
import { fetchPolicies } from '~/lib/graphql';
import httpStatusCodes from '~/lib/utils/http_status'; import httpStatusCodes from '~/lib/utils/http_status';
import { getParameterValues, removeParams } from '~/lib/utils/url_utility'; import { getParameterValues, removeParams } from '~/lib/utils/url_utility';
import { __, s__ } from '~/locale'; import { __, s__ } from '~/locale';
...@@ -51,8 +52,8 @@ export default { ...@@ -51,8 +52,8 @@ export default {
failureType: null, failureType: null,
failureReasons: [], failureReasons: [],
showStartScreen: false, showStartScreen: false,
isNewCiConfigFile: false,
initialCiFileContent: '', initialCiFileContent: '',
isNewCiConfigFile: false,
lastCommittedContent: '', lastCommittedContent: '',
currentCiFileContent: '', currentCiFileContent: '',
showFailureAlert: false, showFailureAlert: false,
...@@ -64,6 +65,7 @@ export default { ...@@ -64,6 +65,7 @@ export default {
apollo: { apollo: {
initialCiFileContent: { initialCiFileContent: {
fetchPolicy: fetchPolicies.NETWORK,
query: getBlobContent, query: getBlobContent,
// If it's a brand new file, we don't want to fetch the content. // If it's a brand new file, we don't want to fetch the content.
// Then when the user commits the first time, the query would run // Then when the user commits the first time, the query would run
...@@ -91,6 +93,11 @@ export default { ...@@ -91,6 +93,11 @@ export default {
error(error) { error(error) {
this.handleBlobContentError(error); this.handleBlobContentError(error);
}, },
watchLoading(isLoading) {
if (isLoading) {
this.setAppStatus(EDITOR_APP_STATUS_LOADING);
}
},
}, },
ciConfigData: { ciConfigData: {
query: getCiConfigData, query: getCiConfigData,
...@@ -223,6 +230,10 @@ export default { ...@@ -223,6 +230,10 @@ export default {
dismissSuccess() { dismissSuccess() {
this.showSuccessAlert = false; this.showSuccessAlert = false;
}, },
async refetchContent() {
this.$apollo.queries.initialCiFileContent.skip = false;
await this.$apollo.queries.initialCiFileContent.refetch();
},
reportFailure(type, reasons = []) { reportFailure(type, reasons = []) {
this.setAppStatus(EDITOR_APP_STATUS_ERROR); this.setAppStatus(EDITOR_APP_STATUS_ERROR);
...@@ -324,6 +335,7 @@ export default { ...@@ -324,6 +335,7 @@ export default {
@commit="updateOnCommit" @commit="updateOnCommit"
@resetContent="resetContent" @resetContent="resetContent"
@showError="showErrorAlert" @showError="showErrorAlert"
@refetchContent="refetchContent"
@updateCiConfig="updateCiConfig" @updateCiConfig="updateCiConfig"
/> />
<confirm-unsaved-changes-dialog :has-unsaved-changes="hasUnsavedChanges" /> <confirm-unsaved-changes-dialog :has-unsaved-changes="hasUnsavedChanges" />
......
...@@ -120,4 +120,35 @@ describe('Pipeline editor branch switcher', () => { ...@@ -120,4 +120,35 @@ describe('Pipeline editor branch switcher', () => {
]); ]);
}); });
}); });
describe('when switching branches', () => {
beforeEach(async () => {
mockAvailableBranchQuery.mockResolvedValue(mockProjectBranches);
createComponentWithApollo();
await waitForPromises();
});
it('emits the refetchContent event when selecting a different branch', async () => {
const branch = findDropdownItems().at(1);
expect(branch.text()).not.toBe(mockDefaultBranch);
expect(wrapper.emitted('refetchContent')).toBeUndefined();
await branch.vm.$emit('click');
expect(wrapper.emitted('refetchContent')).toBeDefined();
expect(wrapper.emitted('refetchContent')).toHaveLength(1);
});
it('does not emit the refetchContent event when selecting the current branch', async () => {
const branch = findDropdownItems().at(0);
expect(branch.text()).toBe(mockDefaultBranch);
expect(wrapper.emitted('refetchContent')).toBeUndefined();
await branch.vm.$emit('click');
expect(wrapper.emitted('refetchContent')).toBeUndefined();
});
});
}); });
...@@ -92,6 +92,11 @@ describe('Pipeline editor app component', () => { ...@@ -92,6 +92,11 @@ describe('Pipeline editor app component', () => {
const options = { const options = {
localVue, localVue,
data() {
return {
currentBranch: mockDefaultBranch,
};
},
mocks: {}, mocks: {},
apolloProvider: mockApollo, apolloProvider: mockApollo,
}; };
...@@ -116,9 +121,6 @@ describe('Pipeline editor app component', () => { ...@@ -116,9 +121,6 @@ describe('Pipeline editor app component', () => {
}); });
afterEach(() => { afterEach(() => {
mockBlobContentData.mockReset();
mockCiConfigData.mockReset();
wrapper.destroy(); wrapper.destroy();
}); });
...@@ -337,4 +339,22 @@ describe('Pipeline editor app component', () => { ...@@ -337,4 +339,22 @@ describe('Pipeline editor app component', () => {
}); });
}); });
}); });
describe('when refetching content', () => {
beforeEach(async () => {
await createComponentWithApollo();
jest
.spyOn(wrapper.vm.$apollo.queries.initialCiFileContent, 'refetch')
.mockImplementation(jest.fn());
});
it('refetches blob content', async () => {
expect(wrapper.vm.$apollo.queries.initialCiFileContent.refetch).toHaveBeenCalledTimes(0);
await wrapper.vm.refetchContent();
expect(wrapper.vm.$apollo.queries.initialCiFileContent.refetch).toHaveBeenCalledTimes(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