Skip to content

Commit

Permalink
Tests for the alerts components in the company offer management page …
Browse files Browse the repository at this point in the history
…and the create offer form
  • Loading branch information
FranciscoCardoso913 committed Aug 19, 2023
1 parent 4f85e5f commit 95794d0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
3 changes: 0 additions & 3 deletions src/components/Offers/Form/form-components/OfferForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ const OfferForm = ({ context, title }) => {
key: `${Date.now()}-fetchCompanyApplicationsError`,
});
});
return () => {
request.cancel();
};
}
}, [addSnackbar, session.isValidating, session.isLoggedIn]);

Expand Down
46 changes: 45 additions & 1 deletion src/components/Offers/New/CreateOfferForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createTheme } from "@material-ui/core/styles";
import useComponentController from "../../../hooks/useComponentController";
import { CreateOfferController, CreateOfferControllerContext } from "./CreateOfferForm";
import { BrowserRouter } from "react-router-dom";
import { screen, fireEvent, renderWithStoreAndTheme } from "../../../test-utils";
import { screen, fireEvent, renderWithStoreAndTheme, render } from "../../../test-utils";
import useSession from "../../../hooks/useSession";
import CreateOfferPage from "../../../pages/CreateOfferPage";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
Expand All @@ -14,9 +14,17 @@ import { act } from "@testing-library/react";
import { DAY_IN_MS } from "../../../utils/TimeUtils";
import { PAID_OPTIONS } from "../Form/form-components/OfferForm";
import { HumanValidationReasons } from "../../../utils";
import { validateApplication } from "../../../services/companyApplicationService";
import { ThemeProvider } from "@material-ui/core";
import AppTheme from "../../../AppTheme";
import ValidationPage from "../../../pages/ValidationPage";
import { getValidationMessage } from "../../Apply/Company/CompanyApplicationUtils";
import { fetchCompanyApplicationState } from "../../../services/companyService";
import { Alert } from "../../utils/Alert";

jest.mock("../../../hooks/useSession");
jest.mock("../../../services/locationSearchService");
jest.mock("../../../services/companyService");

// eslint-disable-next-line react/prop-types
const CreateOfferWrapper = ({ children }) => {
Expand All @@ -40,6 +48,7 @@ describe("Create Offer Form", () => {

const initialState = {};
const theme = createTheme({});
fetchCompanyApplicationState.mockImplementation(async () =>"APPROVED");

// it("Should edit description", () => {
// As of today, it is not possible to test contenteditable elements (such as the awesome description editor)
Expand Down Expand Up @@ -217,6 +226,41 @@ describe("Create Offer Form", () => {
expect(element).toBeVisible();
});
});

it("Should render alert if company is not approved", async () => {
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { name: "Company Name" } } }));
fetchCompanyApplicationState.mockImplementation(async () =>"UNVERIFIED");

await renderWithStoreAndTheme(
<BrowserRouter>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<CreateOfferWrapper>
<CreateOfferPage />
</CreateOfferWrapper>
</MuiPickersUtilsProvider>
</BrowserRouter>,
{ initialState, theme }
);
expect( screen.queryByTestId( 'Alert')).toBeInTheDocument();

});

it("Should not render alert if company is approved", async () => {
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { name: "Company Name" } } }));
fetchCompanyApplicationState.mockImplementation(async () =>"APPROVED");

await renderWithStoreAndTheme(
<BrowserRouter>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<CreateOfferWrapper>
<CreateOfferPage />
</CreateOfferWrapper>
</MuiPickersUtilsProvider>
</BrowserRouter>,
{ initialState, theme }
);
expect(await screen.queryByTestId("Alert")).not.toBeInTheDocument();
});
});

describe("Should validate Form", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/utils/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useStyles = (props) => makeStyles((theme) => ({
export const Alert = ({type, title, fontSize = 1, children}) => {
const classes = useStyles({fontSize: fontSize})();
return (
<MUI_Alert severity={type} className={classes.content} icon={<WarningIcon />}>
<MUI_Alert severity={type} className={classes.content} icon={<WarningIcon />} data-testid="Alert">
{title ? <AlertTitle>{title}</AlertTitle> : null}
{children}
</MUI_Alert>
Expand All @@ -30,4 +30,4 @@ Alert.propTypes = {
title: PropTypes.string,
children: PropTypes.string,
fontSize: PropTypes.number,
}
}
3 changes: 0 additions & 3 deletions src/pages/CompanyOffersManagementPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ const CompanyOffersManagementPage = () => {
key: `${Date.now()}-fetchCompanyApplicationsError`,
});
});
return () => {
request.cancel();
};
}
}, [addSnackbar, session.isLoggedIn, session.isValidating]);

Expand Down
50 changes: 50 additions & 0 deletions src/pages/CompanyOffersManagementPage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from "react";
import { createTheme } from "@material-ui/core/styles";
import { BrowserRouter } from "react-router-dom";
import { renderWithStoreAndTheme, screen } from "../test-utils";
import useSession from "../hooks/useSession";
import { fetchCompanyApplicationState } from "../services/companyService";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
import DateFnsUtils from "@date-io/date-fns";
import CompanyOffersManagementPage from "./CompanyOffersManagementPage";

jest.mock("../hooks/useSession");
jest.mock("../services/offerService");
jest.mock("../services/companyService");
const theme = createTheme({});

// eslint-disable-next-line react/prop-types

describe("Company Offers Management Page", () => {

it("Should render alert if company is not approved", async () => {
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { name: "Company Name" } } }));
fetchCompanyApplicationState.mockImplementation(async () =>"UNVERIFIED");

await renderWithStoreAndTheme(
<BrowserRouter>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<CompanyOffersManagementPage />
</MuiPickersUtilsProvider>
</BrowserRouter>,
{ initialState: {}, theme }
);
expect( screen.queryByTestId( 'Alert')).toBeInTheDocument();

});

it("Should not render alert if company is approved", async () => {
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { name: "Company Name" } } }));
fetchCompanyApplicationState.mockImplementation(async () =>"APPROVED");

await renderWithStoreAndTheme(
<BrowserRouter>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<CompanyOffersManagementPage />
</MuiPickersUtilsProvider>
</BrowserRouter>,
{ initialState: {}, theme }
);
expect(await screen.queryByTestId("Alert")).not.toBeInTheDocument();
});
});

0 comments on commit 95794d0

Please sign in to comment.