Commit b3105d00 authored by Paul Slaughter's avatar Paul Slaughter

Check response errors in runner_delete_button update callback

- Also adds test that "evict" isn't called if errors are present
- Also calls gc which is recommended in the apollo docs
parent 47b9390a
...@@ -115,36 +115,37 @@ export default { ...@@ -115,36 +115,37 @@ export default {
// should only change back if the operation fails. // should only change back if the operation fails.
this.deleting = true; this.deleting = true;
try { try {
const { await this.$apollo.mutate({
data: {
runnerDelete: { errors },
},
} = await this.$apollo.mutate({
mutation: runnerDeleteMutation, mutation: runnerDeleteMutation,
variables: { variables: {
input: { input: {
id: this.runner.id, id: this.runner.id,
}, },
}, },
update: (cache) => { update: (cache, { data }) => {
const { errors } = data.runnerDelete;
if (errors?.length) {
this.onError(new Error(errors.join(' ')));
return;
}
this.$emit('deleted', {
message: sprintf(I18N_DELETED_TOAST, { name: this.runnerName }),
});
// Remove deleted runner from the cache // Remove deleted runner from the cache
const cacheId = cache.identify(this.runner); const cacheId = cache.identify(this.runner);
cache.evict({ id: cacheId }); cache.evict({ id: cacheId });
cache.gc();
}, },
}); });
if (errors && errors.length) {
throw new Error(errors.join(' '));
} else {
this.$emit('deleted', {
message: sprintf(I18N_DELETED_TOAST, { name: this.runnerName }),
});
}
} catch (e) { } catch (e) {
this.deleting = false;
this.onError(e); this.onError(e);
} }
}, },
onError(error) { onError(error) {
this.deleting = false;
const { message } = error; const { message } = error;
createAlert({ message }); createAlert({ message });
......
...@@ -29,6 +29,8 @@ jest.mock('~/runner/sentry_utils'); ...@@ -29,6 +29,8 @@ jest.mock('~/runner/sentry_utils');
describe('RunnerDeleteButton', () => { describe('RunnerDeleteButton', () => {
let wrapper; let wrapper;
let apolloProvider;
let apolloCache;
let runnerDeleteHandler; let runnerDeleteHandler;
const findBtn = () => wrapper.findComponent(GlButton); const findBtn = () => wrapper.findComponent(GlButton);
...@@ -43,13 +45,16 @@ describe('RunnerDeleteButton', () => { ...@@ -43,13 +45,16 @@ describe('RunnerDeleteButton', () => {
wrapper = mountFn(RunnerDeleteButton, { wrapper = mountFn(RunnerDeleteButton, {
propsData: { propsData: {
runner: { runner: {
// We need typename so that cache.identify works
// eslint-disable-next-line no-underscore-dangle
__typename: mockRunner.__typename,
id: mockRunner.id, id: mockRunner.id,
shortSha: mockRunner.shortSha, shortSha: mockRunner.shortSha,
...runner, ...runner,
}, },
...propsData, ...propsData,
}, },
apolloProvider: createMockApollo([[runnerDeleteMutation, runnerDeleteHandler]]), apolloProvider,
directives: { directives: {
GlTooltip: createMockDirective(), GlTooltip: createMockDirective(),
GlModal: createMockDirective(), GlModal: createMockDirective(),
...@@ -72,6 +77,11 @@ describe('RunnerDeleteButton', () => { ...@@ -72,6 +77,11 @@ describe('RunnerDeleteButton', () => {
}, },
}); });
}); });
apolloProvider = createMockApollo([[runnerDeleteMutation, runnerDeleteHandler]]);
apolloCache = apolloProvider.defaultClient.cache;
jest.spyOn(apolloCache, 'evict');
jest.spyOn(apolloCache, 'gc');
createComponent(); createComponent();
}); });
...@@ -149,6 +159,13 @@ describe('RunnerDeleteButton', () => { ...@@ -149,6 +159,13 @@ describe('RunnerDeleteButton', () => {
expect(deleted[0][0].message).toMatch(`#${mockRunnerId}`); expect(deleted[0][0].message).toMatch(`#${mockRunnerId}`);
expect(deleted[0][0].message).toMatch(`${mockRunner.shortSha}`); expect(deleted[0][0].message).toMatch(`${mockRunner.shortSha}`);
}); });
it('evicts runner from apollo cache', () => {
expect(apolloCache.evict).toHaveBeenCalledWith({
id: apolloCache.identify(mockRunner),
});
expect(apolloCache.gc).toHaveBeenCalled();
});
}); });
describe('When update fails', () => { describe('When update fails', () => {
...@@ -199,6 +216,11 @@ describe('RunnerDeleteButton', () => { ...@@ -199,6 +216,11 @@ describe('RunnerDeleteButton', () => {
it('error is shown to the user', () => { it('error is shown to the user', () => {
expect(createAlert).toHaveBeenCalledTimes(1); expect(createAlert).toHaveBeenCalledTimes(1);
}); });
it('does not evict runner from apollo cache', () => {
expect(apolloCache.evict).not.toHaveBeenCalled();
expect(apolloCache.gc).not.toHaveBeenCalled();
});
}); });
}); });
......
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