Коммит 27b86a50 создал по автору Nicolas Dular's avatar Nicolas Dular
Просмотр файлов

Only subscribe when the mutation has been sent

владелец 7cc0fe81
......@@ -32,6 +32,7 @@ export default {
loading: false,
errorAlert: null,
aiCompletionResponse: {},
skipSubscription: true,
};
},
computed: {
......@@ -60,7 +61,13 @@ export default {
error(error) {
this.handleError(error);
},
skip() {
return this.skipSubscription;
},
result({ data }) {
if (!data.aiCompletionResponse) {
return;
}
this.loading = false;
if (this.timeout) {
......@@ -69,6 +76,7 @@ export default {
if (data.error) {
this.handleError(new Error(data.error));
this.skipSubscription = true;
return;
}
......@@ -77,6 +85,8 @@ export default {
const generatedByText = `${data.aiCompletionResponse.responseBody}\n***\n_${__(
'This comment was generated using OpenAI',
)}_`;
this.skipSubscription = true;
if (textArea) {
updateText({
textArea,
......@@ -110,11 +120,11 @@ export default {
this.$apollo
.mutate({ mutation: aiActionMutation, variables: { input } })
.then(({ data: { aiAction } }) => {
if (aiAction.errors.length > 0) {
if (aiAction.errors && aiAction.errors.length > 0) {
this.handleError(new Error(aiAction.errors));
return;
}
this.$apollo.subscriptions.aiCompletionResponse.start();
this.skipSubscription = false;
})
.catch(this.handleError);
},
......@@ -135,6 +145,7 @@ export default {
...alertOptions,
});
this.loading = false;
this.skipSubscription = true;
clearTimeout(this.timeout);
},
},
......
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { GlCollapsibleListbox, GlListboxItem, GlLoadingIcon } from '@gitlab/ui';
import { createMockSubscription } from 'mock-apollo-client';
import createMockApollo from 'helpers/mock_apollo_helper';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
......@@ -40,13 +39,12 @@ describe('AI actions dropdown component', () => {
const createWrapper = (props) => {
window.gon = { current_user_id: userId };
aiResponseSubscriptionHandler = createMockSubscription();
aiActionMutationHandler = jest.fn();
const mockApollo = createMockApollo([[aiActionMutation, aiActionMutationHandler]]);
mockApollo.defaultClient.setRequestHandler(
aiResponseSubscription,
() => aiResponseSubscriptionHandler,
);
aiResponseSubscriptionHandler = jest.fn();
const mockApollo = createMockApollo([
[aiResponseSubscription, aiResponseSubscriptionHandler],
[aiActionMutation, aiActionMutationHandler],
]);
wrapper = mountExtended(AiActionsDropdown, {
attachTo: '#root',
......@@ -65,6 +63,8 @@ describe('AI actions dropdown component', () => {
afterEach(() => {
resetHTMLFixture();
aiActionMutationHandler.mockRestore();
aiResponseSubscriptionHandler.mockRestore();
});
describe('summarize comments action', () => {
......@@ -95,17 +95,22 @@ describe('AI actions dropdown component', () => {
describe('success', () => {
beforeEach(async () => {
aiActionMutationHandler.mockResolvedValue({});
await selectSummariseComments();
aiResponseSubscriptionHandler.next({
aiResponseSubscriptionHandler.mockResolvedValue({
data: {
aiCompletionResponse: {
responseBody: 'yay',
},
},
});
aiActionMutationHandler.mockResolvedValue({
data: {
aiAction: {
errors: '',
},
},
});
await selectSummariseComments();
});
it('stops loading', () => {
......@@ -183,7 +188,10 @@ describe('AI actions dropdown component', () => {
it('shows an error and logs to Sentry when the AI subscription fails', async () => {
const mockError = new Error('ding');
aiResponseSubscriptionHandler.error(mockError);
aiResponseSubscriptionHandler.mockRejectedValue(mockError);
await selectSummariseComments();
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith(
expect.objectContaining({
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать