Skip to content

Commit

Permalink
refactor: Initialize form as empty array if undefined and add test sc…
Browse files Browse the repository at this point in the history
…enarios for it
  • Loading branch information
drikusroor committed Sep 6, 2024
1 parent f2e6858 commit 45bb321
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
61 changes: 61 additions & 0 deletions frontend/src/components/Trial/Trial.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,65 @@ describe('Trial', () => {
);
});
});

it("calls onResult when form is not defined", async () => {
const formless_feedback_form = {
...feedback_form,
form: undefined
};
render(<Trial
config={defaultConfig}
onNext={mockOnNext}
onResult={mockOnResult}
feedback_form={formless_feedback_form}
/>);
fireEvent.click(screen.getByTestId('mock-feedback-form'));
await waitFor(() => {
expect(mockOnResult).toHaveBeenCalled();
});
});

it("calls onResult and onNext when form is not defined and break_round_on is met", async () => {
const formless_feedback_form = {
...feedback_form,
form: undefined
};
const config = {
...defaultConfig,
break_round_on: { NOT: ['fast'] }
};
render(<Trial
config={config}
onNext={mockOnNext}
onResult={mockOnResult}
feedback_form={formless_feedback_form}
/>);
fireEvent.click(screen.getByTestId('mock-feedback-form'));
await waitFor(() => {
expect(mockOnResult).toHaveBeenCalled();
expect(mockOnNext).toHaveBeenCalledTimes(1);
});
});

it("calls only onResult when form is not defined and break_round_on is NOT met", async () => {
const formless_feedback_form = {
...feedback_form,
form: undefined
};
const config = {
...defaultConfig,
break_round_on: { NOT: ['slow'] }
};
render(<Trial
config={config}
onNext={mockOnNext}
onResult={mockOnResult}
feedback_form={formless_feedback_form}
/>);
fireEvent.click(screen.getByTestId('mock-feedback-form'));
await waitFor(() => {
expect(mockOnResult).toHaveBeenCalled();
expect(mockOnNext).not.toHaveBeenCalled();
});
});
});
4 changes: 3 additions & 1 deletion frontend/src/components/Trial/Trial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ const Trial = (props: TrialProps) => {
// Create result data
const makeResult = useCallback(
async (result: { type: 'time_passed' }) => {

// Prevent multiple submissions
if (submitted.current) {
return;
}

submitted.current = true;

if (!feedback_form) {
Expand All @@ -83,7 +85,7 @@ const Trial = (props: TrialProps) => {
return;
}

const { form } = feedback_form;
const { form = [] } = feedback_form;

if (result.type === "time_passed") {
form.map((formElement) => (formElement.value = "TIMEOUT"));
Expand Down

0 comments on commit 45bb321

Please sign in to comment.