From 33374146e2d856e9e98474888d6ec0d976e3ea15 Mon Sep 17 00:00:00 2001 From: FranciscoCardoso913 Date: Tue, 29 Aug 2023 12:42:45 +0100 Subject: [PATCH] Making tests for the validate application and the fetch company application services Making tests for the validate application and the fetch company application services --- .../Apply/Company/ApplicationConfirmation.js | 3 +- .../Offers/Form/form-components/OfferForm.js | 30 ++-- .../Form/form-components/offerStyles.js | 8 +- .../Offers/New/CreateOfferForm.spec.js | 21 ++- .../ApplicationsReviewWidget.spec.js | 4 +- src/components/utils/Alert.js | 36 ++--- src/pages/CompanyOffersManagementPage.js | 32 +++-- src/pages/CompanyOffersManagementPage.spec.js | 12 +- src/pages/ValidationPage.js | 27 ++-- src/pages/ValidationPage.spec.js | 29 ++-- .../companyApplicationService.spec.js | 130 ++++++++++++------ src/services/companyService.js | 13 +- src/services/companyService.spec.js | 50 +++++++ 13 files changed, 252 insertions(+), 143 deletions(-) create mode 100644 src/services/companyService.spec.js diff --git a/src/components/Apply/Company/ApplicationConfirmation.js b/src/components/Apply/Company/ApplicationConfirmation.js index 142367e6..51a3b03c 100644 --- a/src/components/Apply/Company/ApplicationConfirmation.js +++ b/src/components/Apply/Company/ApplicationConfirmation.js @@ -23,7 +23,8 @@ const ApplicationConfirmation = () => { 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 {" "} {Constants.CONTACT_US_EMAIL} diff --git a/src/components/Offers/Form/form-components/OfferForm.js b/src/components/Offers/Form/form-components/OfferForm.js index fa31f2b7..3277a878 100644 --- a/src/components/Offers/Form/form-components/OfferForm.js +++ b/src/components/Offers/Form/form-components/OfferForm.js @@ -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"; @@ -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"; @@ -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({ @@ -130,9 +130,17 @@ const OfferForm = ({ context, title }) => { ? :
- {(state !== "APPROVED") && session.isLoggedIn && {"This offer will stay hidden from the public until your account is approved!"}} - + {(state !== "APPROVED") && session.isLoggedIn && + + { + "This offer will stay hidden from the public until your account is approved!" + } + + } + 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", }, diff --git a/src/components/Offers/New/CreateOfferForm.spec.js b/src/components/Offers/New/CreateOfferForm.spec.js index 5aefdd77..1045ada7 100644 --- a/src/components/Offers/New/CreateOfferForm.spec.js +++ b/src/components/Offers/New/CreateOfferForm.spec.js @@ -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"; @@ -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"); @@ -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) @@ -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( @@ -241,13 +237,14 @@ describe("Create Offer Form", () => { , { 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( diff --git a/src/components/Review/Applications/ApplicationsReviewWidget.spec.js b/src/components/Review/Applications/ApplicationsReviewWidget.spec.js index d42b5a07..67d50630 100644 --- a/src/components/Review/Applications/ApplicationsReviewWidget.spec.js +++ b/src/components/Review/Applications/ApplicationsReviewWidget.spec.js @@ -286,10 +286,10 @@ describe("Application Review Widget", () => { , { 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"); diff --git a/src/components/utils/Alert.js b/src/components/utils/Alert.js index 76bbcbbe..5f4adf34 100644 --- a/src/components/utils/Alert.js +++ b/src/components/utils/Alert.js @@ -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 ( - } data-testid="Alert"> - {title ? {title} : null} + } data-testid="Alert"> + {title ? + + {title} + : null} {children} - - ) -} + + ); +}; Alert.propTypes = { type: PropTypes.oneOf(["error", "warning", "info", "success"]), title: PropTypes.string, children: PropTypes.string, fontSize: PropTypes.number, -} +}; diff --git a/src/pages/CompanyOffersManagementPage.js b/src/pages/CompanyOffersManagementPage.js index 09793a97..a7b18370 100644 --- a/src/pages/CompanyOffersManagementPage.js +++ b/src/pages/CompanyOffersManagementPage.js @@ -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"; @@ -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({ @@ -42,9 +37,16 @@ const CompanyOffersManagementPage = () => { return ( - {(state !== "APPROVED") && session.isLoggedIn && {"Your offers will stay hidden from the public until your account is approved!"}} - + { + (state !== "APPROVED") && session.isLoggedIn && + + {"Your offers will stay hidden from the public until your account is approved!"} + + } + ); }; diff --git a/src/pages/CompanyOffersManagementPage.spec.js b/src/pages/CompanyOffersManagementPage.spec.js index 6286e084..ee88d16c 100644 --- a/src/pages/CompanyOffersManagementPage.spec.js +++ b/src/pages/CompanyOffersManagementPage.spec.js @@ -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"; @@ -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( @@ -29,13 +30,14 @@ describe("Company Offers Management Page", () => { , { 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( @@ -45,6 +47,6 @@ describe("Company Offers Management Page", () => { , { initialState: {}, theme } ); - expect(await screen.queryByTestId("Alert")).not.toBeInTheDocument(); + expect(screen.queryByTestId("Alert")).not.toBeInTheDocument(); }); }); diff --git a/src/pages/ValidationPage.js b/src/pages/ValidationPage.js index bc70cfce..4c606a73 100644 --- a/src/pages/ValidationPage.js +++ b/src/pages/ValidationPage.js @@ -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: { @@ -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, @@ -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, - } + }, }, @@ -48,7 +45,7 @@ const ValidationPage = () => { const [error, setError] = useState(""); - useEffect( () => { + useEffect(() => { async function validate() { try { setLoading(false); @@ -71,10 +68,10 @@ const ValidationPage = () => { const { title, text } = success ? successMessage : getValidationMessage(error); return ( - + {title} - - + + {text} {!success ? "For more information contact us at " : ""} nijobs@aefeup.pt @@ -97,7 +94,7 @@ const ValidationPage = () => { ); } else { - return getMessageCard(error); + return getMessageCard(error); } }; diff --git a/src/pages/ValidationPage.spec.js b/src/pages/ValidationPage.spec.js index 9681fb45..57e4f6cd 100644 --- a/src/pages/ValidationPage.spec.js +++ b/src/pages/ValidationPage.spec.js @@ -25,53 +25,58 @@ describe("Validation Page", () => { const page = await render( - + , ); - const {title, text} = getValidationMessage("success"); + const { title } = getValidationMessage("success"); expect(page.queryByText(title)).toBeInTheDocument(); }); it("Should show error message if token does not exist", async () => { - validateApplication.mockImplementation(() =>{ throw [{ msg: "invalid-token" }]}); + validateApplication.mockImplementation(() => { + throw [{ msg: "invalid-token" }]; + }); const page = await render( - + , ); - const {title, text} = getValidationMessage("invalid-token"); + const { title } = getValidationMessage("invalid-token"); expect(page.queryByText(title)).toBeInTheDocument(); }); it("Should show error message if token has expired", async () => { - validateApplication.mockImplementation(() =>{ throw [{ msg: "expired-token" }]}); + validateApplication.mockImplementation(() => { + throw [{ msg: "expired-token" }]; + }); const page = await render( - + , ); - const {title, text} = getValidationMessage("expired-token"); + const { title } = getValidationMessage("expired-token"); expect(page.queryByText(title)).toBeInTheDocument(); }); it("Should show error message if application is already validated", async () => { - validateApplication.mockImplementation(() =>{ throw [{ msg: "application-already-validated" }]}); + validateApplication.mockImplementation(() => { + throw [{ msg: "application-already-validated" }]; + }); const page = await render( - + , ); - const {title, text} = getValidationMessage("application-already-validated"); + const { title } = getValidationMessage("application-already-validated"); expect(page.queryByText(title)).toBeInTheDocument(); }); }); - diff --git a/src/services/companyApplicationService.spec.js b/src/services/companyApplicationService.spec.js index 1be6d5f4..614620bf 100644 --- a/src/services/companyApplicationService.spec.js +++ b/src/services/companyApplicationService.spec.js @@ -1,6 +1,6 @@ import config from "../config"; -import { submitCompanyApplication } from "./companyApplicationService"; +import { submitCompanyApplication, validateApplication } from "./companyApplicationService"; import { setCompanyApplicationSending, setCompanyApplicationSubmissionError, @@ -10,65 +10,109 @@ import Constants from "../utils/Constants"; const { API_HOSTNAME } = config; describe("Company Application Service", () => { - it("should POST the API with the form data in JSON format and dispatch the correct actions", async () => { + describe("Submit Application", () => { + it("should POST the API with the form data in JSON format and dispatch the correct actions", async () => { - // Simulate request success - fetch.mockResponse(JSON.stringify({ mockData: true })); + // Simulate request success + fetch.mockResponse(JSON.stringify({ mockData: true })); - const dispatchMock = jest.fn(); + const dispatchMock = jest.fn(); - const formData = { field1: 1, field2: 2 }; - await submitCompanyApplication(formData)(dispatchMock); + const formData = { field1: 1, field2: 2 }; + await submitCompanyApplication(formData)(dispatchMock); - expect(fetch).toHaveBeenCalledWith(`${API_HOSTNAME}/apply/company`, expect.objectContaining({ - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(formData), - })); + expect(fetch).toHaveBeenCalledWith(`${API_HOSTNAME}/apply/company`, expect.objectContaining({ + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(formData), + })); - expect(dispatchMock).toHaveBeenNthCalledWith(1, setCompanyApplicationSending(true)); - expect(dispatchMock).toHaveBeenNthCalledWith(2, setCompanyApplicationSubmissionError([])); - expect(dispatchMock).toHaveBeenNthCalledWith(3, setCompanyApplication({ mockData: true })); - expect(dispatchMock).toHaveBeenNthCalledWith(4, setCompanyApplicationSending(false)); - }); + expect(dispatchMock).toHaveBeenNthCalledWith(1, setCompanyApplicationSending(true)); + expect(dispatchMock).toHaveBeenNthCalledWith(2, setCompanyApplicationSubmissionError([])); + expect(dispatchMock).toHaveBeenNthCalledWith(3, setCompanyApplication({ mockData: true })); + expect(dispatchMock).toHaveBeenNthCalledWith(4, setCompanyApplicationSending(false)); + }); - it("should handle network error", async () => { + it("should handle network error", async () => { - // Simulate network failure - fetch.mockAbort(); + // Simulate network failure + fetch.mockAbort(); - const dispatchMock = jest.fn(); + const dispatchMock = jest.fn(); - const formData = { field1: 1, field2: 2 }; - await submitCompanyApplication(formData)(dispatchMock); + const formData = { field1: 1, field2: 2 }; + await submitCompanyApplication(formData)(dispatchMock); - expect(dispatchMock).toHaveBeenNthCalledWith(1, setCompanyApplicationSending(true)); - expect(dispatchMock).toHaveBeenNthCalledWith(2, setCompanyApplicationSubmissionError([])); - expect(dispatchMock).toHaveBeenNthCalledWith(3, setCompanyApplicationSubmissionError([ - { msg: Constants.UNEXPECTED_ERROR_MESSAGE }, - ])); - expect(dispatchMock).toHaveBeenNthCalledWith(4, setCompanyApplicationSending(false)); - }); + expect(dispatchMock).toHaveBeenNthCalledWith(1, setCompanyApplicationSending(true)); + expect(dispatchMock).toHaveBeenNthCalledWith(2, setCompanyApplicationSubmissionError([])); + expect(dispatchMock).toHaveBeenNthCalledWith(3, setCompanyApplicationSubmissionError([ + { msg: Constants.UNEXPECTED_ERROR_MESSAGE }, + ])); + expect(dispatchMock).toHaveBeenNthCalledWith(4, setCompanyApplicationSending(false)); + }); - it("should handle not-ok response from API", async () => { + it("should handle not-ok response from API", async () => { - const errors = [{ msg: "error1" }, { msg: "error2" }]; + const errors = [{ msg: "error1" }, { msg: "error2" }]; - // Simulate request error - fetch.mockResponse(JSON.stringify({ errors }), { status: 422 }); + // Simulate request error + fetch.mockResponse(JSON.stringify({ errors }), { status: 422 }); - const dispatchMock = jest.fn(); + const dispatchMock = jest.fn(); - const formData = { field1: 1, field2: 2 }; - await submitCompanyApplication(formData)(dispatchMock); + const formData = { field1: 1, field2: 2 }; + await submitCompanyApplication(formData)(dispatchMock); - expect(dispatchMock).toHaveBeenNthCalledWith(1, setCompanyApplicationSending(true)); - expect(dispatchMock).toHaveBeenNthCalledWith(2, setCompanyApplicationSubmissionError([])); - expect(dispatchMock).toHaveBeenNthCalledWith(3, setCompanyApplicationSubmissionError(errors)); - expect(dispatchMock).toHaveBeenNthCalledWith(4, setCompanyApplicationSending(false)); + expect(dispatchMock).toHaveBeenNthCalledWith(1, setCompanyApplicationSending(true)); + expect(dispatchMock).toHaveBeenNthCalledWith(2, setCompanyApplicationSubmissionError([])); + expect(dispatchMock).toHaveBeenNthCalledWith(3, setCompanyApplicationSubmissionError(errors)); + expect(dispatchMock).toHaveBeenNthCalledWith(4, setCompanyApplicationSending(false)); + }); + }); + describe("Validate Application", () => { + it("should POST the API to validate application ", async () => { + + // Simulate request success + fetch.mockResponse(JSON.stringify({})); + + await validateApplication(0); + expect(fetch).toHaveBeenCalledWith( + `${API_HOSTNAME}/apply/company/${0}/validate`, + { + method: "POST", + credentials: "include", + } + ); + }); + + it("should handle network error", async () => { + + // Simulate network failure + fetch.mockAbort(); + try { + await validateApplication(0); + + } catch (err) { + expect(err).toStrictEqual([{ msg: Constants.UNEXPECTED_ERROR_MESSAGE }]); + } + }); + + it("should handle not-ok response from API", async () => { + + const errors = [{ msg: "error1" }, { msg: "error2" }]; + + // Simulate request error + fetch.mockResponse(JSON.stringify({ errors }), { status: 422 }); + + try { + await validateApplication(0); + } catch (e) { + expect(e).toStrictEqual(errors); + } + }); }); }); diff --git a/src/services/companyService.js b/src/services/companyService.js index f55bc35a..12d13b79 100644 --- a/src/services/companyService.js +++ b/src/services/companyService.js @@ -1,6 +1,6 @@ -import {buildCancelableRequest} from "../utils"; -import {createErrorEvent, createEvent, measureTime} from "../utils/analytics"; -import {EVENT_TYPES, TIMED_ACTIONS} from "../utils/analytics/constants"; +import { buildCancelableRequest } from "../utils"; +import { createErrorEvent, createEvent, measureTime } from "../utils/analytics"; +import { EVENT_TYPES, TIMED_ACTIONS } from "../utils/analytics/constants"; import ErrorTypes from "../utils/ErrorTypes"; import Constants from "../utils/Constants"; import config from "../config"; @@ -8,16 +8,15 @@ import config from "../config"; const COMPANY_APPLICATION_FETCH_METRIC_ID = "company_application/fetch"; const { API_HOSTNAME } = config; -export const fetchCompanyApplicationState = buildCancelableRequest(measureTime(TIMED_ACTIONS.COMPANY_APPLICATION_FETCH, async (companyId) => { +export const fetchCompanyApplication = buildCancelableRequest(measureTime(TIMED_ACTIONS.COMPANY_APPLICATION_FETCH, async (companyId) => { let isErrorRegistered = false; try { - const res = await fetch(`${API_HOSTNAME}/company/${companyId}/state`, { + const res = await fetch(`${API_HOSTNAME}/company/${companyId}/application`, { method: "GET", credentials: "include", }); const json = await res.json(); - console.log(json); if (!res.ok) { @@ -49,4 +48,4 @@ export const fetchCompanyApplicationState = buildCancelableRequest(measureTime(T throw errorArray; } -})); \ No newline at end of file +})); diff --git a/src/services/companyService.spec.js b/src/services/companyService.spec.js new file mode 100644 index 00000000..a54f8e50 --- /dev/null +++ b/src/services/companyService.spec.js @@ -0,0 +1,50 @@ +import Constants from "../utils/Constants"; +import { fetchCompanyApplication } from "./companyService"; +import config from "../config"; +const { API_HOSTNAME } = config; +describe("Company Service", () => { + describe("Fetch Company Application", () => { + const companyId = 0; + it("should GET the API to fetch the company application ", async () => { + + // Simulate request success + fetch.mockResponse(JSON.stringify({ mockData: true })); + + await fetchCompanyApplication(companyId); + expect(fetch).toHaveBeenCalledWith( + `${API_HOSTNAME}/company/${companyId}/application`, + { + method: "GET", + credentials: "include", + } + ); + }); + + it("should handle network error", async () => { + + // Simulate network failure + fetch.mockAbort(); + try { + await fetchCompanyApplication(companyId); + + } catch (err) { + expect(err).toStrictEqual([{ msg: Constants.UNEXPECTED_ERROR_MESSAGE }]); + } + }); + + it("should handle not-ok response from API", async () => { + + const errors = [{ msg: "error1" }, { msg: "error2" }]; + + // Simulate request error + fetch.mockResponse(JSON.stringify({ errors }), { status: 422 }); + + try { + await fetchCompanyApplication(companyId); + } catch (e) { + expect(e).toStrictEqual(errors); + } + }); + }); + +});