Skip to content

Commit

Permalink
ZKUI-395: Extract the common logic to function
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengYanJin committed Dec 1, 2023
1 parent ffe1a90 commit 28b4069
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
13 changes: 5 additions & 8 deletions src/js/useChainedMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useChainedMutations = ({
results: unknown[] = [],
) => {
const index = results.length;
mutationsWithRetry.current[index].retry = () => {
const mutateAndTriggerNext = () => {
mutations[index].mutate(computeVariablesForNext(results), {
onSuccess: (data) => {
if (index < mutations.length - 1) {
Expand All @@ -44,13 +44,10 @@ export const useChainedMutations = ({
},
});
};
mutations[index].mutate(computeVariablesForNext(results), {
onSuccess: (data) => {
if (index < mutations.length - 1) {
go(computeVariablesForNext, [...results, data]);
}
},
});
mutationsWithRetry.current[index].retry = () => {
mutateAndTriggerNext();
};
mutateAndTriggerNext();
};
return { mutate: go, mutationsWithRetry: mutationsWithRetry.current };
};
2 changes: 1 addition & 1 deletion src/react/ui-elements/Veeam/VeeamSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const VEEAM_STEPS = [
Component: VeeamConfiguration,
},
{
label: 'Action Status',
label: 'Apply Actions',
Component: VeeamTable,
},
{
Expand Down
50 changes: 19 additions & 31 deletions src/react/ui-elements/Veeam/VeeamTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ describe('VeeamTable', () => {
);
};

const expectInitialState = (status: 'Failed' | 'Success') => {
selectors.allRows().forEach((row, index) => {
const cells = within(row).getAllByRole('gridcell');
expect(cells).toHaveLength(3);
expect(cells[0]).toHaveTextContent(`${index + 1}`);
expect(cells[1]).toHaveTextContent(actions[index]);

if (index === 0) {
try {
expect(cells[2]).toHaveTextContent(/Pending.../i);
} catch (error) {
expect(cells[2]).toHaveTextContent(new RegExp(status, 'i'));
}
}
});
};

it('should retry the failed actions', async () => {
//Setup
server.use(...allFailHandlers);
Expand All @@ -93,21 +110,7 @@ describe('VeeamTable', () => {
});

//Verify
// initial state
selectors.allRows().forEach((row, index) => {
const cells = within(row).getAllByRole('gridcell');
expect(cells).toHaveLength(3);
expect(cells[0]).toHaveTextContent(`${index + 1}`);
expect(cells[1]).toHaveTextContent(actions[index]);

if (index === 0) {
try {
expect(cells[2]).toHaveTextContent(/Pending.../i);
} catch (error) {
expect(cells[2]).toHaveTextContent(/Failed/i);
}
}
});
expectInitialState('Failed');
server.events.on('response:mocked', ({ status }) => {
if (status !== 500) {
server.resetHandlers(...allFailHandlers);
Expand Down Expand Up @@ -157,22 +160,7 @@ describe('VeeamTable', () => {
expect(selectors.veeamConfigActionTable()).toBeInTheDocument();
});

// initial state
selectors.allRows().forEach(async (row, index) => {
const cells = within(row).getAllByRole('gridcell');
expect(cells).toHaveLength(3);
expect(cells[0]).toHaveTextContent(`${index + 1}`);
expect(cells[1]).toHaveTextContent(actions[index]);
if (index === 0) {
try {
await waitFor(() => {
expect(cells[2]).toHaveTextContent(/Pending.../i);
});
} catch (error) {
expect(cells[2]).toHaveTextContent(/Success/i);
}
}
});
expectInitialState('Success');
expect(selectors.allRows()).toHaveLength(actions.length);
expect(selectors.cancelButton()).toBeDisabled();
expect(selectors.continueButton()).toBeDisabled();
Expand Down

0 comments on commit 28b4069

Please sign in to comment.