Skip to content

Commit

Permalink
test(retain-old-grading-data-option): further test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavrao145 committed Oct 18, 2024
1 parent dff85ca commit 738cd52
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import * as React from "react";
import {render, screen} from "@testing-library/react";
import {render, screen, fireEvent} from "@testing-library/react";
import {ManualCollectionForm} from "../repo_browser";

// workaround needed for using i18n in jest tests, see
// https://github.com/fnando/i18n/issues/26#issuecomment-1235751777
jest.mock("i18n-js", () => {
return jest.requireActual("i18n-js/dist/require/index");
});

jest.mock("@fortawesome/react-fontawesome", () => ({
FontAwesomeIcon: () => {
return null;
},
}));

describe("RepoBrowser's ManualCollectionForm", () => {
let props, component;

Expand Down Expand Up @@ -37,4 +49,26 @@ describe("RepoBrowser's ManualCollectionForm", () => {
expect(lblRecollectExistingSubmissions).not.toBeInTheDocument();
expect(chkRecollectExistingSubmissions).not.toBeInTheDocument();
});

it("should confirm with a full overwrite warning when retain existing grading option is checked", () => {
const confirmSpy = jest.spyOn(window, "confirm").mockImplementation(() => true);
const manualCollectionForm = component.getByTestId("form_manual_collection");

fireEvent.submit(manualCollectionForm);

expect(confirmSpy).toHaveBeenCalledWith(
I18n.t("submissions.collect.partial_overwrite_warning")
);
});

it("should confirm with a full overwrite warning when retain existing grading option is not checked", () => {
const confirmSpy = jest.spyOn(window, "confirm").mockImplementation(() => true);
const chkRecollectExistingSubmissions = screen.queryByTestId("chk_retain_existing_grading");
const manualCollectionForm = component.getByTestId("form_manual_collection");

fireEvent.click(chkRecollectExistingSubmissions);
fireEvent.submit(manualCollectionForm);

expect(confirmSpy).toHaveBeenCalledWith(I18n.t("submissions.collect.full_overwrite_warning"));
});
});
31 changes: 16 additions & 15 deletions app/assets/javascripts/Components/repo_browser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,21 @@ class ManualCollectionForm extends React.Component {
<legend>
<span>{I18n.t("submissions.collect.manual_collection")}</span>
</legend>
<form method="POST" action={action}>
<form
method="POST"
action={action}
data-testid="form_manual_collection"
onSubmit={event => {
if (
(!this.state.retainExistingGrading &&
!confirm(I18n.t("submissions.collect.full_overwrite_warning"))) ||
(this.state.retainExistingGrading &&
!confirm(I18n.t("submissions.collect.partial_overwrite_warning")))
) {
event.preventDefault();
}
}}
>
<input
type="hidden"
name="current_revision_identifier"
Expand Down Expand Up @@ -192,20 +206,7 @@ class ManualCollectionForm extends React.Component {
</label>
</div>
)}
<button
type="submit"
name="commit"
onClick={event => {
if (
(!this.state.retainExistingGrading &&
!confirm(I18n.t("submissions.collect.full_overwrite_warning"))) ||
(this.state.retainExistingGrading &&
!confirm(I18n.t("submissions.collect.partial_overwrite_warning")))
) {
event.preventDefault();
}
}}
>
<button type="submit" name="commit">
<FontAwesomeIcon icon="fa-solid fa-file-import" />
{I18n.t("submissions.collect.this_revision")}
</button>
Expand Down

0 comments on commit 738cd52

Please sign in to comment.