Skip to content

Commit

Permalink
Making tests for the validate application and the fetch company appli…
Browse files Browse the repository at this point in the history
…cation services

Making tests for the validate application and the fetch company application services
  • Loading branch information
FranciscoCardoso913 committed Aug 29, 2023
1 parent 95794d0 commit 3337414
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 143 deletions.
3 changes: 2 additions & 1 deletion src/components/Apply/Company/ApplicationConfirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const ApplicationConfirmation = () => {
<CardContent className={classes.content}>
<Typography variant="body2">
Application Submitted: You should receive an email containing a confirmation link for your application.
Please confirm it within 10 minutes; otherwise, the link will expire. If you have not received an email, please contact us at
Please confirm it within 10 minutes; otherwise, the link will expire. If you have not received an email,
please contact us at
{" "}
<Link color="secondary" href={`mailto:${Constants.CONTACT_US_EMAIL}`}>
{Constants.CONTACT_US_EMAIL}
Expand Down
30 changes: 19 additions & 11 deletions src/components/Offers/Form/form-components/OfferForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
FormControl,
Typography,
Collapse,
Button, makeStyles,
Button,
} from "@material-ui/core";
import React, {useState, useCallback, useContext, useEffect} from "react";
import React, { useState, useCallback, useContext, useEffect } from "react";
import { Redirect } from "react-router-dom";
import PropTypes from "prop-types";
import MultiOptionTextField from "../../../utils/form/MultiOptionTextField";
Expand Down Expand Up @@ -37,7 +37,7 @@ import { useMobile } from "../../../../utils/media-queries";
import "../editor.css";
import ApplyURLComponent from "./ApplyURLComponent";
import { Alert } from "../../../utils/Alert";
import { fetchCompanyApplicationState } from "../../../../services/companyService";
import { fetchCompanyApplication } from "../../../../services/companyService";
import useSession from "../../../../hooks/useSession.js";
import { addSnackbar } from "../../../../actions/notificationActions";

Expand Down Expand Up @@ -97,14 +97,14 @@ const OfferForm = ({ context, title }) => {
const Content = isMobile ? DialogContent : CardContent;
const classes = useOfferFormStyles(isMobile)();

const [state, setState] = useState( "APPROVED");
const [state, setState] = useState("APPROVED");
const session = useSession();

useEffect(() => {
if(!session.isValidating && session.isLoggedIn) {
const request = fetchCompanyApplicationState(session.data?.company?._id)
.then((state) => {
setState(state);
if (!session.isValidating && session.isLoggedIn) {
fetchCompanyApplication(session.data?.company?._id)
.then((application) => {
setState(application.state);
})
.catch(() => {
addSnackbar({
Expand All @@ -130,9 +130,17 @@ const OfferForm = ({ context, title }) => {
? <Redirect to={`/offer/${offerId}`} push />
:
<div className={classes.formCard}>
{(state !== "APPROVED") && session.isLoggedIn && <Alert type={"warning"}
fontSize={1.2}>{"This offer will stay hidden from the public until your account is approved!"}</Alert>}
<CardHeader title={!isMobile && title}/>
{(state !== "APPROVED") && session.isLoggedIn &&
<Alert
type={"warning"}
fontSize={1.2}
>
{
"This offer will stay hidden from the public until your account is approved!"
}
</Alert>
}
<CardHeader title={!isMobile && title} />
<Content className={classes.formContent}>
<ConnectedLoginAlert
isLoggedIn={isLoggedIn}
Expand Down
8 changes: 4 additions & 4 deletions src/components/Offers/Form/form-components/offerStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export default (isMobile) => makeStyles((theme) => ({
paddingTop: theme.spacing(2),
marginTop: theme.spacing(2),
},
warning:{
fontSize:"1.2em",
warning: {
fontSize: "1.2em",
"& .MuiAlert-icon": {
fontSize: "1.5em"
fontSize: "1.5em",
},
marginBottom:"1em"
marginBottom: "1em",


},
Expand Down
21 changes: 9 additions & 12 deletions 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, render } from "../../../test-utils";
import { screen, fireEvent, renderWithStoreAndTheme } from "../../../test-utils";
import useSession from "../../../hooks/useSession";
import CreateOfferPage from "../../../pages/CreateOfferPage";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
Expand All @@ -14,13 +14,7 @@ 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";
import { fetchCompanyApplication } from "../../../services/companyService";

jest.mock("../../../hooks/useSession");
jest.mock("../../../services/locationSearchService");
Expand Down Expand Up @@ -48,7 +42,8 @@ describe("Create Offer Form", () => {

const initialState = {};
const theme = createTheme({});
fetchCompanyApplicationState.mockImplementation(async () =>"APPROVED");
// eslint-disable-next-line require-await
fetchCompanyApplication.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 @@ -229,7 +224,8 @@ describe("Create Offer Form", () => {

it("Should render alert if company is not approved", async () => {
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { name: "Company Name" } } }));
fetchCompanyApplicationState.mockImplementation(async () =>"UNVERIFIED");
// eslint-disable-next-line require-await
fetchCompanyApplication.mockImplementation(async () => ({ state: "PENDING" }));

await renderWithStoreAndTheme(
<BrowserRouter>
Expand All @@ -241,13 +237,14 @@ describe("Create Offer Form", () => {
</BrowserRouter>,
{ initialState, theme }
);
expect( screen.queryByTestId( 'Alert')).toBeInTheDocument();
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");
// eslint-disable-next-line require-await
fetchCompanyApplication.mockImplementation(async () => ({ state: "APPROVED" }));

await renderWithStoreAndTheme(
<BrowserRouter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ describe("Application Review Widget", () => {
</SnackbarProvider>
</MuiPickersUtilsProvider>, { initialState: {}, theme })
);

expect(screen.queryByLabelText("Approve Application")).not.toBeInTheDocument();
expect(screen.queryByLabelText("Reject Application")).not.toBeInTheDocument();
})
});

it("Should maintain state filter after rejecting an application", async () => {
const applications = generateApplications(1, "PENDING");
Expand Down
36 changes: 20 additions & 16 deletions src/components/utils/Alert.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import {Alert as MUI_Alert, AlertTitle} from "@material-ui/lab";
import {makeStyles} from "@material-ui/core";

import { Alert as MUIAlert, AlertTitle } from "@material-ui/lab";
import { makeStyles } from "@material-ui/core";
import PropTypes from "prop-types";
import { Warning as WarningIcon } from "@material-ui/icons";
import React from 'react';
import React from "react";

const useStyles = (props) => makeStyles((theme) => ({
const useStyles = (props) => makeStyles(() => ({

content:{
fontSize: props.fontSize+"em",
content: {
fontSize: `${props.fontSize}em`,
"& .MuiAlert-icon": {
fontSize: (props.fontSize+0.3)+"em"
fontSize: `${props.fontSize + 0.3}em`,
},
margin:"0.5em 0em"
margin: "0.5em 0em",
},
}));

export const Alert = ({type, title, fontSize = 1, children}) => {
const classes = useStyles({fontSize: fontSize})();
export const Alert = ({ type, title, fontSize = 1, children }) => {
const classes = useStyles({ fontSize: fontSize })();
return (
<MUI_Alert severity={type} className={classes.content} icon={<WarningIcon />} data-testid="Alert">
{title ? <AlertTitle>{title}</AlertTitle> : null}
<MUIAlert severity={type} className={classes.content} icon={<WarningIcon />} data-testid="Alert">
{title ?
<AlertTitle>
{title}
</AlertTitle> : null}
{children}
</MUI_Alert>
)
}
</MUIAlert>
);
};

Alert.propTypes = {
type: PropTypes.oneOf(["error", "warning", "info", "success"]),
title: PropTypes.string,
children: PropTypes.string,
fontSize: PropTypes.number,
}
};
32 changes: 17 additions & 15 deletions src/pages/CompanyOffersManagementPage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, {useEffect, useState} from "react";
import React, { useEffect, useState } from "react";
import CompanyOffersManagementWidget from "../components/Company/Offers/Manage/CompanyOffersManagementWidget";
import { CardContent, makeStyles } from "@material-ui/core";
import { useMobile } from "../utils/media-queries";
import { Alert } from "../components/utils/Alert";
import { fetchCompanyApplicationState } from "../services/companyService";
import { fetchCompanyApplication } from "../services/companyService";
import useSession from "../hooks/useSession";
import { addSnackbar } from "../actions/notificationActions";

Expand All @@ -17,19 +17,14 @@ const useStyles = (isMobile) => makeStyles((theme) => ({
const CompanyOffersManagementPage = () => {
const isMobile = useMobile();
const classes = useStyles(isMobile)();
const [state, setState_] = useState( "APPROVED");
const [state, setState_] = useState("APPROVED");
const session = useSession();

useEffect(() => {
if(!session.isValidating && session.isLoggedIn) {
const request = fetchCompanyApplicationState(session.data?.company?._id)
.then((state_) => {
console.log(state_);
console.log("Tipo");
console.log(typeof state_);
setState_(state_);
console.log(state);

if (!session.isValidating && session.isLoggedIn) {
fetchCompanyApplication(session.data?.company?._id)
.then((application) => {
setState_(application.state);
})
.catch(() => {
addSnackbar({
Expand All @@ -42,9 +37,16 @@ const CompanyOffersManagementPage = () => {

return (
<CardContent className={classes.content}>
{(state !== "APPROVED") && session.isLoggedIn && <Alert type={"warning"}
fontSize={1.2}>{"Your offers will stay hidden from the public until your account is approved!"}</Alert>}
<CompanyOffersManagementWidget isMobile={isMobile}/>
{
(state !== "APPROVED") && session.isLoggedIn &&
<Alert
type={"warning"}
fontSize={1.2}
>
{"Your offers will stay hidden from the public until your account is approved!"}
</Alert>
}
<CompanyOffersManagementWidget isMobile={isMobile} />
</CardContent>
);
};
Expand Down
12 changes: 7 additions & 5 deletions src/pages/CompanyOffersManagementPage.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 { BrowserRouter } from "react-router-dom";
import { renderWithStoreAndTheme, screen } from "../test-utils";
import useSession from "../hooks/useSession";
import { fetchCompanyApplicationState } from "../services/companyService";
import { fetchCompanyApplication } from "../services/companyService";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
import DateFnsUtils from "@date-io/date-fns";
import CompanyOffersManagementPage from "./CompanyOffersManagementPage";
Expand All @@ -19,7 +19,8 @@ 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");
// eslint-disable-next-line require-await
fetchCompanyApplication.mockImplementation(async () => ({ state: "PENDING" }));

await renderWithStoreAndTheme(
<BrowserRouter>
Expand All @@ -29,13 +30,14 @@ describe("Company Offers Management Page", () => {
</BrowserRouter>,
{ initialState: {}, theme }
);
expect( screen.queryByTestId( 'Alert')).toBeInTheDocument();
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");
// eslint-disable-next-line require-await
fetchCompanyApplication.mockImplementation(async () => ({ state: "APPROVED" }));

await renderWithStoreAndTheme(
<BrowserRouter>
Expand All @@ -45,6 +47,6 @@ describe("Company Offers Management Page", () => {
</BrowserRouter>,
{ initialState: {}, theme }
);
expect(await screen.queryByTestId("Alert")).not.toBeInTheDocument();
expect(screen.queryByTestId("Alert")).not.toBeInTheDocument();
});
});
27 changes: 12 additions & 15 deletions src/pages/ValidationPage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/* eslint-disable react/jsx-indent */
/* eslint-disable react/jsx-closing-tag-location */
/* eslint-disable indent */
import React, { useEffect, useState } from "react";
import {CardContent, CircularProgress, makeStyles, Button, Link, Typography} from "@material-ui/core";
import { CardContent, CircularProgress, makeStyles, Button, Link, Typography } from "@material-ui/core";
import { useMobile } from "../utils/media-queries";
import { useParams } from "react-router-dom";
import { validateApplication } from "../services/companyApplicationService";
import { getValidationMessage } from "../components/Apply/Company/CompanyApplicationUtils.js";
import {RouterLink} from "../utils";
import { RouterLink } from "../utils";

const useStyles = (isMobile) => makeStyles((theme) => ({
content: {
Expand All @@ -16,10 +13,10 @@ const useStyles = (isMobile) => makeStyles((theme) => ({
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
gap:"1em",
gap: "1em",
},
title: {
fontWeight:500,
fontWeight: 500,
},
text: {
fontSize: theme.typography.body1,
Expand All @@ -29,10 +26,10 @@ const useStyles = (isMobile) => makeStyles((theme) => ({
},
button: {
background: theme.palette.primary.main,
color:theme.palette.dark.contrastText,
'&:hover':{
color: theme.palette.dark.contrastText,
"&:hover": {
background: theme.palette.secondary.main,
}
},
},


Expand All @@ -48,7 +45,7 @@ const ValidationPage = () => {
const [error, setError] = useState("");


useEffect( () => {
useEffect(() => {
async function validate() {
try {
setLoading(false);
Expand All @@ -71,10 +68,10 @@ const ValidationPage = () => {
const { title, text } = success ? successMessage : getValidationMessage(error);
return (
<CardContent className={classes.content}>
<Typography variant="h5" className={classes.title} gutterBottom>
<Typography variant="h5" className={classes.title} gutterBottom>
{title}
</Typography>
<Typography variant="body1" gutterBottom align='center' >
</Typography>
<Typography variant="body1" gutterBottom align="center">
{text}
{!success ? "For more information contact us at " : ""}
<Link href={"mailto:[email protected]"}> [email protected]</Link>
Expand All @@ -97,7 +94,7 @@ const ValidationPage = () => {
</CardContent>
);
} else {
return getMessageCard(error);
return getMessageCard(error);
}

};
Expand Down
Loading

0 comments on commit 3337414

Please sign in to comment.