Skip to content

Commit

Permalink
add logic to check if a page is already been answered, add functional…
Browse files Browse the repository at this point in the history
…ity for when the request is coming from the summary page
  • Loading branch information
a-lor-cab committed Oct 12, 2023
1 parent e7d4af0 commit e37f552
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import {
getContext,
mockServiceMethod,
} from '../../../testUtils/unitTestHelpers';
import { generateRedirectUrlForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage';
import {
checkIfPageHaveAlreadyBeenAnswered,
generateRedirectUrlForMandatoryQuestionNextPage,
} from '../../../utils/mandatoryQuestionUtils';
import { routes } from '../../../utils/routes';
import getServerSideProps from './getServerSideProps';

jest.mock('next/dist/server/api-utils/node');
jest.mock('../../../utils/generateRedirectForMandatoryQuestionNextPage');
jest.mock('../../../utils/mandatoryQuestionUtils');

const spiedGrantMandatoryQuestionServiceGetMandatoryQuestion = jest.spyOn(
GrantMandatoryQuestionService.prototype,
Expand Down Expand Up @@ -51,6 +55,7 @@ describe('getServerSideProps', () => {
describe('when handling a GET request', () => {
const getDefaultContext = (): Optional<GetServerSidePropsContext> => ({
params: { mandatoryQuestionId: 'mandatoryQuestionId' },
query: {},
});

it('should return the right props', async () => {
Expand Down Expand Up @@ -102,11 +107,57 @@ describe('getServerSideProps', () => {
redirect: {
destination:
'/service-error?serviceErrorProps={"errorInformation":"Something went wrong while trying to get the page you requested","linkAttributes":{"href":"/testResolvedURL","linkText":"Please return","linkInformation":" and try again."}}',
permanent: false,
statusCode: 302,
},
});
});

it('should redirect to next not answered page if the page we are accessing has already been answered, and the url does not have the fromSummaryPage query param as true', async () => {
mockServiceMethod(
spiedGrantMandatoryQuestionServiceGetMandatoryQuestion,
getDefaultGrantMandatoryQuestion
);
(
generateRedirectUrlForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue('/nextpage');
(checkIfPageHaveAlreadyBeenAnswered as jest.Mock).mockReturnValue(true);

const response = await getServerSideProps(getContext(getDefaultContext));

expectObjectEquals(response, {
redirect: {
destination: '/nextpage',
statusCode: 302,
},
});
});
it('should not redirect to next not answered page if the page we are accessing has already been answered when the url have the fromSummaryPage query param as true', async () => {
const getDefaultContext = (): Optional<GetServerSidePropsContext> => ({
params: { mandatoryQuestionId: 'mandatoryQuestionId' },
query: { fromSummaryPage: 'true' },
});

mockServiceMethod(
spiedGrantMandatoryQuestionServiceGetMandatoryQuestion,
getDefaultGrantMandatoryQuestion
);
(
generateRedirectUrlForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue('/nextpage');
(checkIfPageHaveAlreadyBeenAnswered as jest.Mock).mockReturnValue(true);

const response = await getServerSideProps(getContext(getDefaultContext));

expectObjectEquals(response, {
props: {
csrfToken: 'testCSRFToken',
fieldErrors: [],
formAction: '/testResolvedURL',
defaultFields: getDefaultGrantMandatoryQuestion(),
mandatoryQuestion: getDefaultGrantMandatoryQuestion(),
},
});
});
});

describe('when handling a POST request', () => {
Expand All @@ -115,6 +166,7 @@ describe('getServerSideProps', () => {
method: 'POST',
},
params: { mandatoryQuestionId: 'mandatoryQuestionId' },
query: {},
});

beforeEach(() => {
Expand Down Expand Up @@ -151,6 +203,26 @@ describe('getServerSideProps', () => {
});
});

it('Should redirect to the summary page after successfully updating if query parameter fromSummaryPage is true', async () => {
const getDefaultContext = (): Optional<GetServerSidePropsContext> => ({
req: {
method: 'POST',
},
params: { mandatoryQuestionId: 'mandatoryQuestionId' },
query: { fromSummaryPage: 'true' },
});
const response = await getServerSideProps(getContext(getDefaultContext));

expectObjectEquals(response, {
redirect: {
destination: routes.mandatoryQuestions.summaryPage(
'mandatoryQuestionId'
),
statusCode: 302,
},
});
});

it('Should redirect to the error service page if there is an error when updating', async () => {
spiedGrantMandatoryQuestionServiceUpdateMandatoryQuestion.mockRejectedValueOnce(
'Error'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
} from '../../../services/GrantMandatoryQuestionService';
import { Optional } from '../../../testUtils/unitTestHelpers';
import callServiceMethod from '../../../utils/callServiceMethod';
import { generateRedirectUrlForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage';
import { getJwtFromCookies } from '../../../utils/jwt';
import {
checkIfPageHaveAlreadyBeenAnswered,
generateRedirectUrlForMandatoryQuestionNextPage,
} from '../../../utils/mandatoryQuestionUtils';
import { routes } from '../../../utils/routes';

export default async function getServerSideProps({
Expand All @@ -19,6 +22,7 @@ export default async function getServerSideProps({
resolvedUrl, //the url that the user requested
}: GetServerSidePropsContext) {
const { mandatoryQuestionId } = params as Record<string, string>;
const { fromSummaryPage = false } = query as Record<string, string>;
const jwt = getJwtFromCookies(req);
const { publicRuntimeConfig } = getConfig();

Expand All @@ -45,19 +49,29 @@ export default async function getServerSideProps({
return {
redirect: {
destination: routes.serviceError(serviceErrorProps),
permanent: false,
statusCode: 302,
},
};
}

//TODO only when someone access this page from the summary page,
// we want to show the default value
//otherwise we gonna send it to the next non filled page
const nextNotAnsweredPage = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
);
//only when someone access this page from the summary page,
// we want to show the default value
//otherwise we gonna send it to the next non filled page
if (
checkIfPageHaveAlreadyBeenAnswered(mandatoryQuestion, resolvedUrl) &&
!fromSummaryPage
) {
return {
redirect: {
destination: nextNotAnsweredPage,
statusCode: 302,
},
};
}

const response = await callServiceMethod(
req,
Expand All @@ -68,7 +82,10 @@ export default async function getServerSideProps({
mandatoryQuestionId,
body
),
nextNotAnsweredPage,
//where we want to go after we update the mandatory question
fromSummaryPage
? routes.mandatoryQuestions.summaryPage(mandatoryQuestionId)
: nextNotAnsweredPage,
{
errorInformation:
'Something went wrong while trying to update your organisation details',
Expand Down

This file was deleted.

Loading

0 comments on commit e37f552

Please sign in to comment.