Skip to content

Commit

Permalink
Disable statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Oct 3, 2022
1 parent 8d4421c commit 6af1146
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ describe('useStatusAction', () => {
}
);

expect(result.current.getActions([])).toMatchInlineSnapshot(`
expect(result.current.getActions([basicCase])).toMatchInlineSnapshot(`
Array [
Object {
"data-test-subj": "cases-bulk-action-status-open",
"disabled": false,
"disabled": true,
"icon": "empty",
"key": "cases-bulk-action-status-open",
"name": "Open",
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('useStatusAction', () => {
}
);

const multipleCasesTests = [
const multipleCasesTests: Array<[CaseStatuses, number, string]> = [
[CaseStatuses.open, 0, 'Opened 2 cases'],
[CaseStatuses['in-progress'], 1, 'Marked 2 cases as in progress'],
[CaseStatuses.closed, 2, 'Closed 2 cases'],
Expand Down Expand Up @@ -162,4 +162,37 @@ describe('useStatusAction', () => {
});
}
);

const disabledTests: Array<[CaseStatuses, number]> = [
[CaseStatuses.open, 0],
[CaseStatuses['in-progress'], 1],
[CaseStatuses.closed, 2],
];

it.each(disabledTests)('disables the status button correctly: %s', async (status, index) => {
const { result } = renderHook(
() => useStatusAction({ onAction, onActionSuccess, isDisabled: false }),
{
wrapper: appMockRender.AppWrapper,
}
);

const actions = result.current.getActions([{ ...basicCase, status }]);
expect(actions[index].disabled).toBe(true);
});

it.each(disabledTests)(
'disables the status button correctly if isDisabled=true: %s',
async (status, index) => {
const { result } = renderHook(
() => useStatusAction({ onAction, onActionSuccess, isDisabled: true }),
{
wrapper: appMockRender.AppWrapper,
}
);

const actions = result.current.getActions([basicCase]);
expect(actions[index].disabled).toBe(true);
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ interface UseStatusActionProps extends UseActionProps {
selectedStatus?: CaseStatuses;
}

const getCasesWithChanges = (cases: Case[], status: CaseStatuses): Case[] =>
cases.filter((theCase) => theCase.status !== status);

const disableStatus = (cases: Case[], status: CaseStatuses) =>
getCasesWithChanges(cases, status).length === 0;

export const useStatusAction = ({
onAction,
onActionSuccess,
Expand Down Expand Up @@ -70,23 +76,23 @@ export const useStatusAction = ({
name: statuses[CaseStatuses.open].label,
icon: getStatusIcon(CaseStatuses.open),
onClick: () => handleUpdateCaseStatus(selectedCases, CaseStatuses.open),
disabled: isDisabled,
disabled: isDisabled || disableStatus(selectedCases, CaseStatuses.open),
'data-test-subj': 'cases-bulk-action-status-open',
key: 'cases-bulk-action-status-open',
},
{
name: statuses[CaseStatuses['in-progress']].label,
icon: getStatusIcon(CaseStatuses['in-progress']),
onClick: () => handleUpdateCaseStatus(selectedCases, CaseStatuses['in-progress']),
disabled: isDisabled,
disabled: isDisabled || disableStatus(selectedCases, CaseStatuses['in-progress']),
'data-test-subj': 'cases-bulk-action-status-in-progress',
key: 'cases-bulk-action-status-in-progress',
},
{
name: statuses[CaseStatuses.closed].label,
icon: getStatusIcon(CaseStatuses.closed),
onClick: () => handleUpdateCaseStatus(selectedCases, CaseStatuses.closed),
disabled: isDisabled,
disabled: isDisabled || disableStatus(selectedCases, CaseStatuses.closed),
'data-test-subj': 'cases-bulk-action-status-closed',
key: 'cases-bulk-status-action',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('useActions', () => {
});

act(() => {
userEvent.click(res.getByTestId('cases-bulk-action-status-open'));
userEvent.click(res.getByTestId('cases-bulk-action-status-in-progress'));
});

await waitFor(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('useBulkActions', () => {
"items": Array [
Object {
"data-test-subj": "cases-bulk-action-status-open",
"disabled": false,
"disabled": true,
"icon": "empty",
"key": "cases-bulk-action-status-open",
"name": "Open",
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('useBulkActions', () => {
});

act(() => {
userEvent.click(res.getByTestId('cases-bulk-action-status-open'));
userEvent.click(res.getByTestId('cases-bulk-action-status-in-progress'));
});

await waitForHook(() => {
Expand Down

0 comments on commit 6af1146

Please sign in to comment.