diff --git a/app/assets/javascripts/Components/__tests__/repo_browser_manual_collection_form.test.jsx b/app/assets/javascripts/Components/__tests__/repo_browser_manual_collection_form.test.jsx index d7f1355b41..8a8d748abf 100644 --- a/app/assets/javascripts/Components/__tests__/repo_browser_manual_collection_form.test.jsx +++ b/app/assets/javascripts/Components/__tests__/repo_browser_manual_collection_form.test.jsx @@ -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; @@ -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")); + }); }); diff --git a/app/assets/javascripts/Components/repo_browser.jsx b/app/assets/javascripts/Components/repo_browser.jsx index a2659f2f86..8e49ed010e 100644 --- a/app/assets/javascripts/Components/repo_browser.jsx +++ b/app/assets/javascripts/Components/repo_browser.jsx @@ -157,7 +157,21 @@ class ManualCollectionForm extends React.Component { {I18n.t("submissions.collect.manual_collection")} -
+ { + 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(); + } + }} + > )} -