Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deletion button to archived runs #3285

Merged
merged 6 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion frontend/src/lib/Buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class Buttons {
// or recurring run config.
public delete(
getSelectedIds: () => string[],
resourceName: 'pipeline' | 'recurring run config' | 'pipeline version',
resourceName: 'pipeline' | 'recurring run config' | 'pipeline version' | 'run',
callback: (selectedIds: string[], success: boolean) => void,
useCurrentResource: boolean,
): Buttons {
Expand All @@ -158,6 +158,8 @@ export default class Buttons {
? this._deletePipeline(getSelectedIds(), useCurrentResource, callback)
: resourceName === 'pipeline version'
? this._deletePipelineVersion(getSelectedIds(), useCurrentResource, callback)
: resourceName === 'run'
? this._deleteRun(getSelectedIds(), useCurrentResource, callback)
: this._deleteRecurringRun(getSelectedIds()[0], useCurrentResource, callback),
disabled: !useCurrentResource,
disabledTitle: useCurrentResource
Expand Down Expand Up @@ -497,6 +499,22 @@ export default class Buttons {
);
}

private _deleteRun(
ids: string[],
useCurrentResource: boolean,
callback: (_: string[], success: boolean) => void,
): void {
this._dialogActionHandler(
ids,
'Do you want to delete the selected runs? This action cannot be undone.',
useCurrentResource,
id => Apis.runServiceApi.deleteRun(id),
callback,
'Delete',
'run',
);
}

private _dialogActionHandler(
selectedIds: string[],
content: string,
Expand Down
85 changes: 82 additions & 3 deletions frontend/src/pages/Archive.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import { PageProps } from './Page';
import { RunStorageState } from '../apis/run';
import { ShallowWrapper, shallow } from 'enzyme';
import { ButtonKeys } from '../lib/Buttons';
import { Apis } from '../lib/Apis';

describe('Archive', () => {
const updateBannerSpy = jest.fn();
const updateToolbarSpy = jest.fn();
const historyPushSpy = jest.fn();
const deleteRunSpy = jest.spyOn(Apis.runServiceApi, 'deleteRun');
const updateDialogSpy = jest.fn();
const updateSnackbarSpy = jest.fn();
let tree: ShallowWrapper;

function generateProps(): PageProps {
Expand All @@ -35,16 +39,19 @@ describe('Archive', () => {
{} as any,
historyPushSpy,
updateBannerSpy,
null,
updateDialogSpy,
updateToolbarSpy,
null,
updateSnackbarSpy,
);
}

beforeEach(() => {
updateBannerSpy.mockClear();
updateToolbarSpy.mockClear();
historyPushSpy.mockClear();
deleteRunSpy.mockClear();
updateDialogSpy.mockClear();
updateSnackbarSpy.mockClear();
});

afterEach(() => tree.unmount());
Expand All @@ -60,17 +67,29 @@ describe('Archive', () => {
expect(updateBannerSpy).toHaveBeenCalledWith({});
});

it('only enables restore button when at least one run is selected', () => {
it('enables restore and delete button when at least one run is selected', () => {
tree = shallow(<Archive {...generateProps()} />);
TestUtils.flushPromises();
tree.update();
expect(TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.RESTORE).disabled).toBeTruthy();
expect(
TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.DELETE_RUN).disabled,
).toBeTruthy();
tree.find('RunList').simulate('selectionChange', ['run1']);
expect(TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.RESTORE).disabled).toBeFalsy();
expect(
TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.DELETE_RUN).disabled,
).toBeFalsy();
tree.find('RunList').simulate('selectionChange', ['run1', 'run2']);
expect(TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.RESTORE).disabled).toBeFalsy();
expect(
TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.DELETE_RUN).disabled,
).toBeFalsy();
tree.find('RunList').simulate('selectionChange', []);
expect(TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.RESTORE).disabled).toBeTruthy();
expect(
TestUtils.getToolbarButton(updateToolbarSpy, ButtonKeys.DELETE_RUN).disabled,
).toBeTruthy();
});

it('refreshes the run list when refresh button is clicked', async () => {
Expand All @@ -85,4 +104,64 @@ describe('Archive', () => {
tree = shallow(<Archive {...generateProps()} />);
expect(tree.find('RunList').prop('storageState')).toBe(RunStorageState.ARCHIVED.toString());
});

it('cancells deletion when Cancel is clicked', async () => {
tree = shallow(<Archive {...generateProps()} />);

// Click delete button to delete selected ids.
const deleteBtn = (tree.instance() as Archive).getInitialToolbarState().actions[
ButtonKeys.DELETE_RUN
];
await deleteBtn!.action();

// Dialog pops up to confirm the deletion.
expect(updateDialogSpy).toHaveBeenCalledTimes(1);
expect(updateDialogSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
content: 'Do you want to delete the selected runs? This action cannot be undone.',
}),
);

// Cancel deletion.
const call = updateDialogSpy.mock.calls[0][0];
const cancelBtn = call.buttons.find((b: any) => b.text === 'Cancel');
await cancelBtn.onClick();
expect(deleteRunSpy).not.toHaveBeenCalled();
});

it('deletes selected ids when Confirm is clicked', async () => {
tree = shallow(<Archive {...generateProps()} />);
tree.setState({ selectedIds: ['id1', 'id2', 'id3'] });

// Mock the behavior where the deletion of id1 fails, the deletion of id2 and id3 succeed.
TestUtils.makeErrorResponseOnce(deleteRunSpy, 'woops');
deleteRunSpy.mockImplementation(() => Promise.resolve({}));

// Click delete button to delete selected ids.
const deleteBtn = (tree.instance() as Archive).getInitialToolbarState().actions[
ButtonKeys.DELETE_RUN
];
await deleteBtn!.action();

// Dialog pops up to confirm the deletion.
expect(updateDialogSpy).toHaveBeenCalledTimes(1);
expect(updateDialogSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
content: 'Do you want to delete the selected runs? This action cannot be undone.',
}),
);

// Confirm.
const call = updateDialogSpy.mock.calls[0][0];
const confirmBtn = call.buttons.find((b: any) => b.text === 'Delete');
await confirmBtn.onClick();
await deleteRunSpy;
await TestUtils.flushPromises();
tree.update();
expect(deleteRunSpy).toHaveBeenCalledTimes(3);
expect(deleteRunSpy).toHaveBeenCalledWith('id1');
expect(deleteRunSpy).toHaveBeenCalledWith('id2');
expect(deleteRunSpy).toHaveBeenCalledWith('id3');
expect(tree.state('selectedIds')).toEqual(['id1']); // id1 is left over since its deletion failed.
});
});
7 changes: 7 additions & 0 deletions frontend/src/pages/Archive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export default class Archive extends Page<{}, ArchiveState> {
actions: buttons
.restore(() => this.state.selectedIds, false, this._selectionChanged.bind(this))
.refresh(this.refresh.bind(this))
.delete(
() => this.state.selectedIds,
'run',
this._selectionChanged.bind(this),
false /* useCurrentResource */,
)
.getToolbarActionMap(),
breadcrumbs: [],
pageTitle: 'Archive',
Expand Down Expand Up @@ -76,6 +82,7 @@ export default class Archive extends Page<{}, ArchiveState> {
private _selectionChanged(selectedIds: string[]): void {
const toolbarActions = this.props.toolbarProps.actions;
toolbarActions[ButtonKeys.RESTORE].disabled = !selectedIds.length;
toolbarActions[ButtonKeys.DELETE_RUN].disabled = !selectedIds.length;
this.props.updateToolbar({
actions: toolbarActions,
breadcrumbs: this.props.toolbarProps.breadcrumbs,
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/pages/__snapshots__/Archive.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ exports[`Archive renders archived runs 1`] = `
toolbarProps={
Object {
"actions": Object {
"deleteRun": Object {
"action": [Function],
"disabled": true,
"disabledTitle": "Select at least one run to delete",
"id": "deleteBtn",
"title": "Delete",
"tooltip": "Delete",
},
"refresh": Object {
"action": [Function],
"id": "refreshBtn",
Expand All @@ -39,8 +47,8 @@ exports[`Archive renders archived runs 1`] = `
}
}
updateBanner={[MockFunction]}
updateDialog={null}
updateSnackbar={null}
updateDialog={[MockFunction]}
updateSnackbar={[MockFunction]}
updateToolbar={[MockFunction]}
/>
</div>
Expand Down